source: main/tags/2.35b/gsdl/bin/script/gsConvert.pl@ 24541

Last change on this file since 24541 was 2512, checked in by sjboddie, 23 years ago

Moved wv's XML config file from packages/wv to etc/wv so that it is now
alway's installed (even if the source code isn't).

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 18.3 KB
Line 
1#!/usr/bin/perl -w
2
3###########################################################################
4#
5# gsConvert.pl -- convert documents to HTML or TEXT format
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Copyright (C) 1999 New Zealand Digital Library Project
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26#
27###########################################################################
28
29# gsConvert.pl converts documents in a range of formats to HTML or TEXT
30# by exploiting third-party programs. These are usually found in the
31# $GSDLHOME/packages directory.
32#
33# Currently, we can convert Microsoft Word and Adobe PDF using specialised
34# conversion utilities. We can convery any file to text with a perl
35# implementation of the UNIX strings command.
36#
37# We try to convert Postscript files to text using "gs" which is often on
38# *nix machines. If it isn't (or we're running on Windoze), we do some feeble
39# text extraction on it using regexps.
40
41BEGIN {
42 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
43 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
44}
45
46use parsargv;
47use util;
48use Cwd;
49use File::Basename;
50
51
52sub print_usage
53{
54 print STDERR "\n";
55 print STDERR "gsConvert.pl: Converts documents in a range of formats to html\n";
56 print STDERR " or text using third-party programs.\n\n";
57 print STDERR " usage: $0 [options] filename\n";
58 print STDERR " options:\n\t-type\tdoc|pdf|ps\n\t-output\thtml|text\n";
59 print STDERR "\t-timeout\t<max cpu seconds>\n";
60 exit(1);
61}
62
63
64sub main
65{
66 my (@ARGV) = @_;
67 my ($input_type,$output_type,$verbose,$timeout);
68
69 $timeout = 0;
70 # read command-line arguments
71 if (!parsargv::parse(\@ARGV,
72 'type/(doc|pdf|ps)/', \$input_type,
73 'output/(html|text)/', \$output_type,
74 'timeout/\d+/0',\$timeout,
75 'verbose/\d+/0', \$verbose))
76 {
77 print_usage();
78 }
79
80 # Make sure the input file exists and can be opened for reading
81 if (scalar(@ARGV!=1)) {
82 print_usage();
83 }
84
85 my $input_filename = $ARGV[0];
86 if (!-r $input_filename) {
87 print STDERR "Error: unable to open $input_filename for reading\n";
88 exit(1);
89 }
90
91 # Deduce filenames
92 my ($tailname,$dirname,$suffix)
93 = File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
94 my $output_filestem = &util::filename_cat($dirname, "$tailname");
95
96 if ($input_type eq "")
97 {
98 $input_type = lc (substr($suffix,1,length($suffix)-1));
99 }
100
101 # Change to temporary working directory
102 my $stored_dir = cwd();
103 chdir ($dirname) || die "Unable to change to directory $dirname";
104
105 # Select convert utility
106 if (!defined $input_type) {
107 print STDERR "Error: No filename extension or input type defined\n";
108 exit(1);
109 }
110 elsif ($input_type eq "doc") {
111 print &convertDOC($input_filename, $output_filestem, $output_type);
112 print "\n";
113 }
114 elsif ($input_type eq "rtf") {
115 print &convertRTF($input_filename, $output_filestem, $output_type);
116 print "\n";
117 }
118 elsif ($input_type eq "pdf") {
119 print &convertPDF($dirname, $input_filename, $output_filestem, $output_type);
120 print "\n";
121 }
122 elsif ($input_type eq "ps") {
123 print &convertPS($input_filename, $output_filestem, $output_type);
124 print "\n";
125 }
126 else {
127 print STDERR "Error: Unable to convert type '$input_type'\n";
128 exit(1);
129 }
130
131 # restore to original working directory
132 chdir ($stored_dir) || die "Unable to return to directory $stored_dir";
133
134}
135
136&main(@ARGV);
137
138
139
140# Document-type conversion functions
141#
142# The following functions attempt to convert documents from their
143# input type to the specified output type. If no output type was
144# given, then they first attempt HTML, and then TEXT.
145#
146# Each returns the output type ("html" or "text") or "fail" if no
147# conversion is possible.
148
149# Convert a Microsoft word document
150
151sub convertDOC {
152 ($input_filename, $output_filestem, $output_type) = @_;
153
154 # Many .doc files are not in fact word documents!
155 my $realtype = &find_docfile_type($input_filename);
156
157 if ($realtype eq "word6" || $realtype eq "word7" || $realtype eq "word8") {
158 return &convertWord678($input_filename, $output_filestem, $output_type);
159 } elsif ($realtype eq "rtf") {
160 return &convertRTF($input_filename, $output_filestem, $output_type);
161 } else {
162 return &convertAnything($input_filename, $output_filestem, $output_type);
163 }
164}
165
166# Convert a Microsoft word 6/7/8 document
167
168sub convertWord678 {
169 ($input_filename, $output_filestem, $output_type) = @_;
170
171 my $success = 0;
172
173 # Attempt specialised conversion to HTML
174 if (!$output_type || ($output_type =~ /html/i)) {
175 $success = &doc_to_html($input_filename, $output_filestem);
176 if ($success) {
177 return "html";
178 }
179 }
180
181 return &convertAnything($input_filename, $output_filestem, $output_type);
182}
183
184
185# Convert a Rich Text Format (RTF) file
186
187sub convertRTF {
188 ($input_filename, $output_filestem, $output_type) = @_;
189
190 my $success = 0;
191
192 # Attempt specialised conversion to HTML
193 if (!$output_type || ($output_type =~ /html/i)) {
194 $success = &rtf_to_html($input_filename, $output_filestem);
195 if ($success) {
196 return "html";
197 }
198 }
199
200 return &convertAnything($input_filename, $output_filestem, $output_type);
201}
202
203
204# Convert an unidentified file
205
206sub convertAnything {
207 ($input_filename, $output_filestem, $output_type) = @_;
208
209 my $success = 0;
210
211 # Attempt simple conversion to HTML
212 if (!$output_type || ($output_type =~ /html/i)) {
213 $success = &any_to_html($input_filename, $output_filestem);
214 if ($success) {
215 return "html";
216 }
217 }
218
219 # Convert to text
220 if (!$output_type || ($output_type =~ /text/i)) {
221 $success = &any_to_text($input_filename, $output_filestem);
222 if ($success) {
223 return "text";
224 }
225 }
226 return "fail";
227}
228
229
230
231# Convert an Adobe PDF document
232
233sub convertPDF {
234 ($dirname, $input_filename, $output_filestem, $output_type) = @_;
235
236 my $success = 0;
237
238 # Attempt conversion to HTML
239 if (!$output_type || ($output_type =~ /html/i)) {
240 $success = &pdf_to_html($dirname, $input_filename, $output_filestem);
241 if ($success) {
242 return "html";
243 }
244 }
245
246 # Attempt conversion to TEXT
247 if (!$output_type || ($output_type =~ /text/i)) {
248 $success = &pdf_to_text($dirname, $input_filename, $output_filestem);
249 if ($success) {
250 return "text";
251 }
252 }
253
254 return "fail";
255
256}
257
258
259# Convert an Adobe PostScript document
260
261sub convertPS {
262 ($input_filename, $output_filestem, $output_type) = @_;
263
264 my $success = 0;
265
266 # Attempt conversion to TEXT
267 if (!$output_type || ($output_type =~ /text/i)) {
268 $success = &ps_to_text($input_filename, $output_filestem);
269 if ($success) {
270 return "text";
271 }
272 }
273
274 return "fail";
275
276}
277
278
279# Find the real type of a .doc file
280#
281# We seem to have a lot of files with a .doc extension that are .rtf
282# files or Word 5 files. This function attempts to tell the difference.
283
284sub find_docfile_type {
285 ($input_filename) = @_;
286
287 open(CHK, "<$input_filename");
288 binmode(CHK);
289 my $line = "";
290 my $first = 1;
291
292 while (<CHK>) {
293
294 $line = $_;
295
296 if ($first) {
297 # check to see if this is an rtf file
298 if ($line =~ /^\{\\rtf/) {
299 close(CHK);
300 return "rtf";
301 }
302 }
303
304 # is this is a word 6/7/8 document?
305 if ($line =~ /Word\.Document\.([678])/) {
306 close(CHK);
307 return "word$1";
308 }
309
310 $first = 0;
311
312 }
313
314 return "unknown";
315}
316
317
318
319# Specific type-to-type conversions
320#
321# Each of the following functions attempts to convert a document from
322# a specific format to another. If they succeed yhey return 1 and leave
323# the output document(s) in the appropriate place; if they fail they
324# return 0 and delete any working files.
325
326
327# Attempt to convert a word document to html with the wv program
328
329sub doc_to_html {
330 ($input_filename, $output_filestem) = @_;
331
332 my $wvWare = &util::filename_cat($ENV{'GSDLHOME'}, "bin",
333 $ENV{'GSDLOS'}, "wvWare");
334
335 # don't include path on windows (to avoid having to play about
336 # with quoting when GSDLHOME might contain spaces) but assume
337 # that the PATH is set up correctly
338 $wvWare = "wvWare" if ($ENV{'GSDLOS'} =~ /^windows$/i);
339
340 my $wv_conf = &util::filename_cat($ENV{'GSDLHOME'}, "etc",
341 "wv", "wvHtml.xml");
342
343 my $cmd = "";
344 if ($timeout) {$cmd = "ulimit -t $timeout;";}
345 $cmd .= "$wvWare --charset utf-8 --config \"$wv_conf\"";
346 $cmd .= " \"$input_filename\" > \"$output_filestem.html\"";
347
348 # redirecting STDERR is a bad idea on windows 95/98
349 $cmd .= " 2> \"$output_filestem.err\""
350 if $ENV{'GSDLOS'} !~ /^windows$/i;
351
352 # execute the command
353 if (system($cmd)!=0)
354 {
355 print STDERR "Error executing wv converter: $!. Continuing...\n";
356 }
357
358 # Was the conversion successful?
359
360 if (-e "$output_filestem.html") {
361 open(TMP, "$output_filestem.html");
362 $line = <TMP>;
363 close(TMP);
364 if ($line && $line =~ /DOCTYPE HTML/) {
365 &util::rm("$output_filestem.err") if -e "$output_filestem.err";
366 return 1;
367 } else {
368 # An error of some sort occurred
369 &util::rm("$output_filestem.html");
370 &util::rm("$output_filestem.err") if -e "$output_filestem.err";
371 }
372 }
373
374 return 0;
375}
376
377
378# Attempt to convert an RTF document to html with rtftohtml
379#
380# rtf2html isn't distributed with Greenstone because it is not
381# distributed under the GPL. If you know of a better solution,
382# please let me know.
383
384sub rtf_to_html {
385 my ($input_filename, $output_filestem) = @_;
386
387 # we'll give up already if using Windows
388 return 0 if $ENV{'GSDLOS'} =~ /^windows$/i;
389
390 # formulate the command
391 my $r_cmd = &util::filename_cat($ENV{'GSDLHOME'}, "packages", "unix",
392 "rtf2html", "rtf2html", "rtf2html");
393 $r_cmd = "rtf2html" unless (-e "$r_cmd");
394 return 0 unless (-e "$r_cmd");
395 $cmd = "";
396 if ($timeout) {$cmd = "ulimit -t $timeout;";}
397 $cmd .= "$r_cmd";
398 $cmd .= " \"$input_filename\" > \"$output_filestem.html\" 2>\"$output_filestem.err\"";
399
400 # execute the command
401 if (system($cmd)!=0)
402 {
403 print STDERR "Error executing rtf converter: $!. Continuing...\n";
404 }
405
406 # Was the conversion successful?
407 if (-e "$output_filestem.html") {
408 open(TMP, "$output_filestem.html");
409 $line = <TMP>;
410 close(TMP);
411 if ($line && $line =~ /DOCTYPE HTML/) {
412 &util::rm("$output_filestem.err");
413 return 1;
414 } else {
415 # An error of some sort occurred
416 &util::rm("$output_filestem.html");
417 &util::rm("$output_filestem.err");
418 }
419 }
420 return 0;
421}
422
423
424# Convert a pdf file to html with the pdftohtml command
425
426sub pdf_to_html {
427 ($dirname, $input_filename, $output_filestem) = @_;
428
429 $cmd = "";
430 if ($timeout) {$cmd = "ulimit -t $timeout;";}
431 $cmd .= "perl -S pdftohtml.pl -F ";
432 $cmd .= " \"$input_filename\" \"$output_filestem\"";
433 $!=0;
434
435 if (system($cmd)!=0)
436 {
437 print STDERR "Error executing $cmd";
438 if ($!) {print STDERR ": $!";}
439 print STDERR "\n";
440 return 0;
441 }
442
443 # make sure the converter made something
444 if (! -e "$output_filestem.html")
445 {
446 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
447 # print out the converters std err, if any
448 if (-e "$output_filestem.err") {
449 open (ERRLOG, "$output_filestem.err") || die "$!";
450 print STDERR "pdftohtml:\n";
451 while (<ERRLOG>) {
452 print STDERR "$_";
453 }
454 close ERRLOG;
455 }
456 return 0;
457 }
458
459 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
460 return 1;
461}
462
463# Convert a PDF file to text with the pdftotext command
464
465sub pdf_to_text {
466 ($dirname, $input_filename, $output_filestem) = @_;
467
468 my $cmd = "pdftotext \"$input_filename\" \"$output_filestem.text\"";
469 $cmd .= " 2> \"$output_filestem.err\"";
470
471 if (system($cmd)!=0)
472 {
473 print STDERR "Error executing $cmd: $!\n";
474 &util::rm("$output_filestem.text") if (-e "$output_filestem.text");
475 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
476 return 0;
477 }
478
479 # make sure the converter made something
480 if (! -e "$output_filestem.text")
481 {
482 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
483 # print out the converters std err, if any
484 if (-e "$output_filestem.err") {
485 open (ERRLOG, "$output_filestem.err") || die "$!";
486 print STDERR "pdftotext:\n";
487 while (<ERRLOG>) {
488 print STDERR "$_";
489 }
490 close ERRLOG;
491 }
492 return 0;
493 }
494
495 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
496 return 1;
497}
498
499# Convert a PostScript document to text
500# note - just using "ps2ascii" isn't good enough, as it
501# returns 0 for a postscript interpreter error. ps2ascii is just
502# a wrapper to "gs" anyway, so we use that cmd here.
503
504sub ps_to_text {
505 my ($input_filename, $output_filestem) = @_;
506
507 my $error = "";
508
509 # if we're on windows we'll fall straight through without attempting
510 # to use gs
511 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
512 $error = "Windows does not support gs";
513
514 } else {
515 my $cmd = "gs -q -dNODISPLAY -dNOBIND -dWRITESYSTEMDICT -dSIMPLE -c save ";
516 $cmd .= "-f ps2ascii.ps \"$input_filename\" -c quit > \"$output_filestem.text\"";
517 $cmd .= " 2> $output_filestem.err";
518 $!=0;
519 my $retcode=system($cmd);
520 $retcode = $? >> 8; # see man perlfunc - system for this...
521 # if system returns -1 | 127 (couldn't start program), look at $! for message
522
523 if ($retcode!=0) {if ($!) {$error=$!;} else {$error="couldn't run.\n";}}
524 elsif (! -e "$output_filestem.text") {
525 $error="did not create output file.\n";
526 }
527 else
528 { # make sure the interpreter didn't get an error. It is technically
529 # possible for the actual text to start with this, but....
530 open PSOUT, "$output_filestem.text";
531 if (<PSOUT> =~ /^Error: (.*)/) {
532 $error="interpreter error - \"$1\"";
533 }
534 close PSOUT;
535 }
536 }
537
538 if ($error ne "")
539 {
540 print STDERR "PSPlug: WARNING: Error executing gs: $error\n";
541 &util::rm("$output_filestem.text") if (-e "$output_filestem.text");
542 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
543
544 # Fine then. We'll just do a lousy job by ourselves...
545 # Based on 5-line regexp sed script found at:
546 # http://snark.ptc.spbu.ru/mail-archives/lout/brown/msg00003.html
547 #
548 print STDERR "PSPlug: Stripping text from postscript\n";
549 my $errorcode=0;
550 open (IN, "$input_filename")
551 || ($errorcode=1, warn "Couldn't read file: $!");
552 open (OUT, ">$output_filestem.text")
553 || ($errorcode=1, warn "Couldn't write file: $!");
554 if ($errorcode) {print STDERR "errors\n";return 0;}
555
556 my $text=""; # this is for whole .ps file...
557 while (<IN>) {
558 $text.=$_;
559 }
560 close IN;
561
562 # Make sure this is a ps file...
563 if ($text !~ /^%!/) {
564 print STDERR "Bad postscript header: not %!\n";
565 return 0;
566 }
567
568 # if ps has Page data, then use it to delete all stuff before it.
569 $text =~ s/^.*?%%Page:.*?\n//s; # treat string as single line
570
571 # remove all leading non-data stuff
572 $text =~ s/^.*?\(//s;
573
574 # remove all newline chars for easier processing
575 $text =~ s/\n//g;
576
577 # Big assumption here - assume that if any co-ordinates are
578 # given, then we are at the end of a sentence.
579 $text =~ s/\)-?\d+\ -?\d+/\) \(\n\)/g;
580
581 # special characters--
582 $text =~ s/\(\|\)/\(\ - \)/g; # j -> em-dash?
583
584 # ? ps text formatting (eg italics?) ?
585 $text =~ s/Fn\(f\)/\(\{\)/g; # f -> {
586 $text =~ s/Fn\(g\)/\(\}\)/g; # g -> }
587 $text =~ s/Fn\(j\)/\(\|\)/g; # j -> |
588 # default - remove the rest
589 $text =~ s/\ ?F.\((.+?)\)/\($1\)/g;
590
591 # attempt to add whitespace between words...
592 # this is based purely on observation, and may be completely wrong...
593 $text =~ s/([^F])[defghijkuy]\(/$1 \( /g;
594 # eg I notice "b(" is sometimes NOT a space if preceded by a
595 # negative number.
596 $text =~ s/\)\d+ ?b\(/\) \( /g;
597
598 # change quoted braces to brackets
599 $text =~ s/([^\\])\\\(/$1\{/g;
600 $text =~ s/([^\\])\\\)/$1\}/g ;
601
602 # remove everything that is not between braces
603 $text =~ s/\)([^\(\)])+?\(//sg ;
604
605 # remove any Trailer eof stuff.
606 $text =~ s/\)[^\)]*$//sg;
607
608 ### ligatures have special characters...
609 $text =~ s/\\013/ff/g;
610 $text =~ s/\\014/fi/g;
611 $text =~ s/\\015/fl/g;
612 $text =~ s/\\016/ffi/g;
613 $text =~ s/\\214/fi/g;
614 $text =~ s/\\215/fl/g;
615 $text =~ s/\\017/\n\* /g; # asterisk?
616 $text =~ s/\\023/\023/g; # e acute ('e)
617 $text =~ s/\\177/\252/g; # u"
618# $text =~ s/ ?? /\344/g; # a"
619
620 print OUT "$text";
621 close OUT;
622 }
623 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
624 return 1;
625}
626
627
628# Convert any file to HTML with a crude perl implementation of the
629# UNIX strings command.
630
631sub any_to_html {
632 ($input_filename, $output_filestem) = @_;
633
634 # First generate a text file
635 return 0 unless (&any_to_text($input_filename, $output_filestem));
636
637 # create an HTML file from the text file
638 open(TEXT, "<$output_filestem.text");
639 open(HTML, ">$output_filestem.html");
640
641 print HTML "<html><head>\n";
642 print HTML "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html\">\n";
643 print HTML "<META NAME=\"GENERATOR\" CONTENT=\"Greenstone any_to_html\">\n";
644 print HTML "</head><body>\n\n";
645
646 while (<TEXT>) {
647 print HTML "<p> ", $_;
648 }
649 print HTML "\n</body></html>\n";
650
651 close HTML;
652 close TEXT;
653
654 &util::rm("$output_filestem.text") if (-e "$output_filestem.text");
655 return 1;
656}
657
658# Convert any file to TEXT with a crude perl implementation of the
659# UNIX strings command.
660
661sub any_to_text {
662 ($input_filename, $output_filestem) = @_;
663
664 open(IN, "<$input_filename");
665 binmode(IN);
666 open(OUT, ">$output_filestem.text");
667
668 my ($line);
669 my $dgcount = 0;
670 while (<IN>) {
671 $line = $_;
672
673 # delete anything that isn't a printable character
674 $line =~ s/[^\040-\176]+/\n/sg;
675
676 # delete any string less than 10 characters long
677 $line =~ s/^.{0,9}$/\n/mg;
678 while ($line =~ /^.{1,9}$/m) {
679 $line =~ s/^.{0,9}$/\n/mg;
680 $line =~ s/\n+/\n/sg;
681 }
682
683 # remove extraneous whitespace
684 $line =~ s/\n+/\n/gs;
685 $line =~ s/^\n//gs;
686
687 # output whatever is left
688 if ($line =~ /[^\n ]/) {
689 print OUT $line;
690 }
691 }
692
693 close OUT;
694 close IN;
695
696 return 1;
697}
Note: See TracBrowser for help on using the repository browser.