source: gsdl/trunk/bin/script/gsConvert.pl@ 15152

Last change on this file since 15152 was 15152, checked in by ak19, 16 years ago

Regular expression in make_links_to_assocdir_relative is corrected and a utility subroutine is added

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 46.0 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-2002 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. The sources of these are usually found
31# in the $GSDLHOME/packages directory, and the executables should live in
32# $GSDLHOME/bin/$GSDLOS (which is on the search path).
33#
34# Currently, we can convert the following formats by using external
35# conversion utilities:
36# Microsoft Word (versions 2,6,7 [==95?], 8[==97?], 9[==2000?]), RTF,
37# Adobe PDF, PostScript, MS PowerPoint (95 and 97), and MS Excel (95 and 97).
38#
39# We can try to convert any file to text with a perl implementation of the
40# UNIX strings command.
41#
42# We try to convert Postscript files to text using "gs" which is often on
43# *nix machines. We fall back to performing weak text extraction by using
44# regular expressions.
45
46BEGIN {
47 die "GSDLHOME not set\n" unless defined $ENV{'GSDLHOME'};
48 unshift (@INC, "$ENV{'GSDLHOME'}/perllib");
49}
50
51use parsargv;
52use util;
53use Cwd;
54use File::Basename;
55
56# Are we running on WinNT or Win2000 (or later)?
57my $is_winnt_2000=eval {require Win32; return (Win32::IsWinNT()); return 0;};
58if (!defined($is_winnt_2000)) {$is_winnt_2000=0;}
59
60my $use_strings;
61my $pdf_complex;
62my $pdf_nohidden;
63my $pdf_zoom;
64my $pdf_ignore_images;
65my $pdf_allow_images_only;
66my $windows_scripting;
67
68sub print_usage
69{
70 print STDERR "\n";
71 print STDERR "gsConvert.pl: Converts documents in a range of formats to html\n";
72 print STDERR " or text using third-party programs.\n\n";
73 print STDERR " usage: $0 [options] filename\n";
74 print STDERR " options:\n\t-type\tdoc|dot|pdf|ps|ppt|rtf|xls\t(input file type)\n";
75 print STDERR "\t-errlog\t<filename>\t(append err messages)\n";
76 print STDERR "\t-output\tauto|html|text|pagedimg-jpg|pagedimg-gif|pagedimg-png\t(output file type)\n";
77 print STDERR "\t-timeout\t<max cpu seconds>\t(ulimit on unix systems)\n";
78 print STDERR "\t-use_strings\tuse strings to extract text if conversion fails\n";
79 print STDERR "\t-windows_scripting\tuse windows script when converting Microsoft Word and PPT via VB script\n";
80 print STDERR "\t-pdf_complex\tuse complex output when converting PDF to HTML\n";
81 print STDERR "\t-pdf_nohidden\tDon't attempt to extract hidden text from PDF files\n";
82 print STDERR "\t-pdf_ignore_images\tdon't attempt to extract images when\n";
83 print STDERR "\t\tconverting PDF to HTML\n";
84 print STDERR "\t-pdf_allow_images_only\tallow images only (continue even if no text is present when converting to HTML)\n";
85 print STDERR "\t-pdf_zoom\tfactor by which to zoom PDF (only useful if\n";
86 print STDERR "\t\t-pdf_complex is set\n";
87 exit(1);
88}
89
90my $faillogfile="";
91my $timeout=0;
92
93sub main
94{
95 my (@ARGV) = @_;
96 my ($input_type,$output_type,$verbose);
97
98 # read command-line arguments
99 if (!parsargv::parse(\@ARGV,
100 'type/(doc|dot|pdf|ps|ppt|rtf|xls)/', \$input_type,
101 '/errlog/.*/', \$faillogfile,
102 'output/(auto|html|text|pagedimg).*/', \$output_type,
103 'timeout/\d+/0',\$timeout,
104 'verbose/\d+/0', \$verbose,
105 'use_strings', \$use_strings,
106 'windows_scripting',\$windows_scripting,
107 'pdf_complex', \$pdf_complex,
108 'pdf_ignore_images', \$pdf_ignore_images,
109 'pdf_allow_images_only', \$pdf_allow_images_only,
110 'pdf_nohidden', \$pdf_nohidden,
111 'pdf_zoom/\d+/2', \$pdf_zoom
112 ))
113 {
114 print_usage();
115 }
116
117 # Make sure the input file exists and can be opened for reading
118 if (scalar(@ARGV!=1)) {
119 print_usage();
120 }
121
122 my $input_filename = $ARGV[0];
123 if (!-r $input_filename) {
124 print STDERR "Error: unable to open $input_filename for reading\n";
125 exit(1);
126 }
127
128 # Deduce filenames
129 my ($tailname,$dirname,$suffix)
130 = File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
131 my $output_filestem = &util::filename_cat($dirname, "$tailname");
132
133 if ($input_type eq "")
134 {
135 $input_type = lc (substr($suffix,1,length($suffix)-1));
136 }
137
138 # Change to temporary working directory
139 my $stored_dir = cwd();
140 chdir ($dirname) || die "Unable to change to directory $dirname";
141
142 # Select convert utility
143 if (!defined $input_type) {
144 print STDERR "Error: No filename extension or input type defined\n";
145 exit(1);
146 }
147 elsif ($input_type eq "doc" || $input_type eq "dot") {
148 print &convertDOC($input_filename, $output_filestem, $output_type);
149 print "\n";
150 }
151 elsif ($input_type eq "rtf") {
152 print &convertRTF($input_filename, $output_filestem, $output_type);
153 print "\n";
154 }
155 elsif ($input_type eq "pdf") {
156 print &convertPDF($dirname, $input_filename, $output_filestem, $output_type);
157 print "\n";
158 }
159 elsif ($input_type eq "ps") {
160 print &convertPS($input_filename, $output_filestem, $output_type);
161 print "\n";
162 }
163 elsif ($input_type eq "ppt") {
164 print &convertPPT($input_filename, $output_filestem, $output_type);
165 print "\n";
166 }
167 elsif ($input_type eq "xls") {
168 print &convertXLS($input_filename, $output_filestem, $output_type);
169 print "\n";
170 }
171 else {
172 print STDERR "Error: Unable to convert type '$input_type'\n";
173 exit(1);
174 }
175
176 # restore to original working directory
177 chdir ($stored_dir) || die "Unable to return to directory $stored_dir";
178
179}
180
181&main(@ARGV);
182
183
184
185# Document-type conversion functions
186#
187# The following functions attempt to convert documents from their
188# input type to the specified output type. If no output type was
189# given, then they first attempt HTML, and then TEXT.
190#
191# Each returns the output type ("html" or "text") or "fail" if no
192# conversion is possible.
193
194# Convert a Microsoft word document
195
196sub convertDOC {
197 ($input_filename, $output_filestem, $output_type) = @_;
198
199 # Many .doc files are not in fact word documents!
200 my $realtype = &find_docfile_type($input_filename);
201
202 if ($realtype eq "word6" || $realtype eq "word7" || $realtype eq "word8") {
203 return &convertWord678($input_filename, $output_filestem, $output_type);
204 } elsif ($realtype eq "rtf") {
205 return &convertRTF($input_filename, $output_filestem, $output_type);
206 } else {
207 return &convertAnything($input_filename, $output_filestem, $output_type);
208 }
209}
210
211# Convert a Microsoft word 6/7/8 document
212
213sub convertWord678 {
214 ($input_filename, $output_filestem, $output_type) = @_;
215
216 my $success = 0;
217 if (!$output_type || ($output_type =~ /html/i)){
218 if ($windows_scripting) {
219 $success = &native_doc_to_html($input_filename, $output_filestem);
220 }
221 else {
222 $success = &doc_to_html($input_filename, $output_filestem);
223 }
224 if ($success) {
225 return "html";
226 }
227 }
228 return &convertAnything($input_filename, $output_filestem, $output_type);
229}
230
231
232# Convert a Rich Text Format (RTF) file
233
234sub convertRTF {
235 ($input_filename, $output_filestem, $output_type) = @_;
236
237 my $success = 0;
238
239 # Attempt specialised conversion to HTML
240 if (!$output_type || ($output_type =~ /html/i)) {
241
242 if ($windows_scripting) {
243 $success = &native_doc_to_html($input_filename, $output_filestem);
244 }
245 else {
246 $success = &rtf_to_html($input_filename, $output_filestem);
247 }
248 if ($success) {
249 return "html";
250 }
251 }
252
253# rtf is so ugly that's it's not worth running strings over.
254# One day I'll write some quick'n'dirty regexps to try to extract text - jrm21
255# return &convertAnything($input_filename, $output_filestem, $output_type);
256 return "fail";
257}
258
259
260# Convert an unidentified file
261
262sub convertAnything {
263 ($input_filename, $output_filestem, $output_type) = @_;
264
265 my $success = 0;
266
267 # Attempt simple conversion to HTML
268 if (!$output_type || ($output_type =~ /html/i)) {
269 $success = &any_to_html($input_filename, $output_filestem);
270 if ($success) {
271 return "html";
272 }
273 }
274
275 # Convert to text
276 if (!$output_type || ($output_type =~ /text/i)) {
277 $success = &any_to_text($input_filename, $output_filestem);
278 if ($success) {
279 return "text";
280 }
281 }
282 return "fail";
283}
284
285
286
287# Convert an Adobe PDF document
288
289sub convertPDF {
290 my ($dirname, $input_filename, $output_filestem, $output_type) = @_;
291
292 my $success = 0;
293 $output_type =~ s/.*\-(.*)/$1/i;
294 # Attempt coversion to Image
295 if ($output_type =~ /jp?g|gif|png/i) {
296 $success = &pdf_to_img($dirname, $input_filename, $output_filestem, $output_type);
297 if ($success){
298 return "item";
299 }
300 }
301
302 # Attempt conversion to HTML
303 if (!$output_type || ($output_type =~ /html/i)) {
304 $success = &pdf_to_html($dirname, $input_filename, $output_filestem);
305 if ($success) {
306 return "html";
307 }
308 }
309
310 # Attempt conversion to TEXT
311 if (!$output_type || ($output_type =~ /text/i)) {
312 $success = &pdf_to_text($dirname, $input_filename, $output_filestem);
313 if ($success) {
314 return "text";
315 }
316 }
317
318 return "fail";
319
320}
321
322
323# Convert an Adobe PostScript document
324
325sub convertPS {
326 ($input_filename, $output_filestem, $output_type) = @_;
327
328 my $success = 0;
329 $output_type =~ s/.*\-(.*)/$1/i;
330 # Attempt coversion to Image
331 if ($output_type =~ /jp?g|gif|png/i) {
332 $success = &ps_to_img($dirname, $input_filename, $output_filestem, $output_type);
333 if ($success){
334 return "item";
335 }
336 }
337
338 # Attempt conversion to TEXT
339 if (!$output_type || ($output_type =~ /text/i)) {
340 $success = &ps_to_text($input_filename, $output_filestem);
341 if ($success) {
342 return "text";
343 }
344 }
345 return "fail";
346}
347
348
349sub convertPPT {
350 my ($input_filename, $output_filestem, $output_type) = @_;
351 my $success = 0;
352
353 my $ppt_convert_type = "";
354 #if (!$output_type || $windows_scripting ||($output_type !~ /html/i) ||($output_type !~ /text/i)){
355 if ($windows_scripting && ($output_type !~ /html/i) && ($output_type !~ /text/i)){
356 if ($output_type =~ /gif/i) {
357 $ppt_convert_type = "-g";
358 } elsif ($output_type =~ /jp?g/i){
359 $ppt_convert_type = "-j";
360 } elsif ($output_type =~ /png/i){
361 $ppt_convert_type = "-p";
362 }
363 my $vbScript = &util::filename_cat($ENV{'GSDLHOME'}, "bin",
364 $ENV{'GSDLOS'}, "pptextract");
365 $vbScript = "pptextract" if ($ENV{'GSDLOS'} =~ /^windows$/i);
366
367 $cmd = "";
368 if ($timeout) {$cmd = "ulimit -t $timeout;";}
369 # if the converting directory has already existed
370 if (-d $output_filestem) {
371 print STDERR "**The conversion directory has existed\n";
372 return "item";
373 } else {
374 $cmd .= "$vbScript $ppt_convert_type \"$input_filename\" \"$output_filestem\"";
375 $cmd .= " 2>\"$output_filestem.err\""
376 if ($ENV{'GSDLOS'} !~ /^windows$/i || $is_winnt_2000);
377 if (system($cmd) !=0) {
378 print STDERR "Powerpoint VB Scripting convert failed\n";
379 } else {
380 return "item";
381 }
382 }
383 } elsif (!$output_type || ($output_type =~ /html/i)) {
384 # Attempt conversion to HTML
385 #if (!$output_type || ($output_type =~ /html/i)) {
386 # formulate the command
387 $cmd = "";
388 $cmd .= "perl -S ppttohtml.pl ";
389 $cmd .= " \"$input_filename\" \"$output_filestem.html\"";
390 $cmd .= " 2>\"$output_filestem.err\""
391 if ($ENV{'GSDLOS'} !~ /^windows$/i || $is_winnt_2000);
392
393 # execute the command
394 $!=0;
395 if (system($cmd)!=0)
396 {
397 print STDERR "Powerpoint 95/97 converter failed $!\n";
398 } else {
399 return "html";
400 }
401 }
402
403 $success = &any_to_text($input_filename, $output_filestem);
404 if ($success) {
405 return "text";
406 }
407
408 return "fail";
409}
410
411
412sub convertXLS {
413 my ($input_filename, $output_filestem, $output_type) = @_;
414
415 my $success = 0;
416
417 # Attempt conversion to HTML
418 if (!$output_type || ($output_type =~ /html/i)) {
419 # formulate the command
420 $cmd = "";
421 $cmd .= "perl -S xlstohtml.pl ";
422 $cmd .= " \"$input_filename\" \"$output_filestem.html\"";
423 $cmd .= " 2>\"$output_filestem.err\""
424 if ($ENV{'GSDLOS'} !~ /^windows$/i || $is_winnt_2000);
425
426
427 # execute the command
428 $!=0;
429 if (system($cmd)!=0)
430 {
431 print STDERR "Excel 95/97 converter failed $!\n";
432 } else {
433 return "html";
434 }
435 }
436
437 $success = &any_to_text($input_filename, $output_filestem);
438 if ($success) {
439 return "text";
440 }
441
442 return "fail";
443}
444
445
446
447# Find the real type of a .doc file
448#
449# We seem to have a lot of files with a .doc extension that are .rtf
450# files or Word 5 files. This function attempts to tell the difference.
451sub find_docfile_type {
452 ($input_filename) = @_;
453
454 open(CHK, "<$input_filename");
455 binmode(CHK);
456 my $line = "";
457 my $first = 1;
458
459 while (<CHK>) {
460
461 $line = $_;
462
463 if ($first) {
464 # check to see if this is an rtf file
465 if ($line =~ /^\{\\rtf/) {
466 close(CHK);
467 return "rtf";
468 }
469 $first = 0;
470 }
471
472 # is this is a word 6/7/8 document?
473 if ($line =~ /Word\.Document\.([678])/) {
474 close(CHK);
475 return "word$1";
476 }
477
478 }
479
480 return "unknown";
481}
482
483
484# Specific type-to-type conversions
485#
486# Each of the following functions attempts to convert a document from
487# a specific format to another. If they succeed they return 1 and leave
488# the output document(s) in the appropriate place; if they fail they
489# return 0 and delete any working files.
490
491
492# Attempt to convert a word document to html with the wv program
493sub doc_to_html {
494 ($input_filename, $output_filestem) = @_;
495
496 my $wvWare = &util::filename_cat($ENV{'GSDLHOME'}, "bin",
497 $ENV{'GSDLOS'}, "wvWare");
498
499 # don't include path on windows (to avoid having to play about
500 # with quoting when GSDLHOME might contain spaces) but assume
501 # that the PATH is set up correctly
502 $wvWare = "wvWare" if ($ENV{'GSDLOS'} =~ /^windows$/i);
503
504 my $wv_conf = &util::filename_cat($ENV{'GSDLHOME'}, "etc",
505 "packages", "wv", "wvHtml.xml");
506
507 # Added the following to work with replace_srcdoc_with_html.pl:
508 # Make wvWare put any associated (image) files of the word doc into
509 # folder docname-without-extention_files. This folder should be at
510 # the same level as the html file generated from the doc.
511 # wvWare will take care of proper interlinking.
512
513 # This step is necessary for replace_srcdoc_with_html.pl which will
514 # move the html and associated files into the import folder. We
515 # want to ensure that the associated files won't overwrite similarly
516 # named items already in import. Hence we put them in a folder first
517 # (to which the html links properly) and that will allow
518 # replace_srcdoc_with_html.pl to move them safely to /import.
519
520 # To do all this, we need to use wvWare's --dir and --basename options
521 # where dir is the full path to the image folder directory and
522 # basename is the full path to the image folder appended to the name
523 # which is to be prepended to every image file:
524 # eg. if the images were to have names like sample0.jpg to sampleN.jpg,
525 # then the basename is "/full/path/to/imgdir/sample".
526 # In this case, basename is the full path to and name of the document.
527 # HOWEVER: basename always takes full path, not relative url, so
528 # the greenstone browser is unable to display the images (absolute paths
529 # cause it to give an "external link" message)
530 # See http://osdir.com/ml/lib.wvware.devel/2002-11/msg00014.html
531 # and http://rpmfind.net/linux/RPM/freshmeat/rpms/wv/wv-0.5.44-1.i386.html
532 # "added --dir option to wvHtml so that pictures can be placed in
533 # a seperate directory"
534 # "running wvWare through IMP to view word documents as html. It gets
535 # invoked like this:
536 # wvWare --dir=/tmp-wvWare --basename=/tmp-wvWare/img$$- $tmp_word >$tmp_output"
537
538 # toppath is the folder where html is generated
539 # docname is the name (without extension) of the html to be generated
540 # suffix (extension) is thrown away
541 my ($docname, $toppath)
542 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
543
544 # We want the image folder generated to have the same name as windows
545 # would generate ($windows_scripting) when it converts from word to html.
546 # That is, foldername=docname_files
547 my $assoc_dir = &util::filename_cat($toppath, $docname."_files");
548 #print "assoc_dir: ".$assoc_dir."\n"; # same as "$output_filestem._files"
549
550 # ensure this image directory exists
551 # if it exists already, just delete and recreate
552 if(-e $assoc_dir) {
553 &util::rm_r($assoc_dir);
554 }
555 &util::mk_dir($assoc_dir);
556
557 # the images are all going to be called image0, image1,..., imageN
558 my $img_basenames = &util::filename_cat($assoc_dir, $docname);
559
560 #print STDERR "****toppath: $toppath\n****docname: $docname\n;
561 #print STDERR "****img_basenames: $img_basenames\n" if($img_basenames);
562 #print STDERR "****assoc_dir: $assoc_dir\n" if($assoc_dir);
563
564 my $cmd = "";
565 if ($timeout) {$cmd = "ulimit -t $timeout;";}
566 # wvWare's --dir and --basename options for image directory.
567 # Replaced the next line with the *2 lines* following it:
568 # $cmd .= "$wvWare --charset utf-8 --config \"$wv_conf\"";
569 $cmd .= "$wvWare --dir \"$assoc_dir\" --basename \"$img_basenames\"";
570 $cmd .= " --charset utf-8 --config \"$wv_conf\"";
571 $cmd .= " \"$input_filename\" > \"$output_filestem.html\"";
572
573 # redirecting STDERR is a bad idea on windows 95/98
574 $cmd .= " 2> \"$output_filestem.err\""
575 if ($ENV{'GSDLOS'} !~ /^windows$/i || $is_winnt_2000);
576 # execute the command
577 $!=0;
578 if (system($cmd)!=0)
579 {
580 print STDERR "Error executing wv converter:$!\n";
581 if (-s "$output_filestem.err") {
582 open (ERRFILE, "<$output_filestem.err");
583
584 my $write_to_fail_log=0;
585 if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile")))
586 {$write_to_fail_log=1;}
587
588 my $line;
589 while ($line=<ERRFILE>) {
590 if ($line =~ /\w/) {
591 print STDERR "$line";
592 print FAILLOG "$line" if ($write_to_fail_log);
593 }
594 if ($line !~ m/startup error/) {next;}
595 print STDERR " (given an invalid .DOC file?)\n";
596 print FAILLOG " (given an invalid .DOC file?)\n"
597 if ($write_to_fail_log);
598
599 } # while ERRFILE
600 close FAILLOG if ($write_to_fail_log);
601 }
602 return 0; # we can try any_to_text
603 }
604
605 # Was the conversion successful?
606
607 if (-s "$output_filestem.html") { # if file has non-zero size (i.e. it has contents)
608 open(TMP, "$output_filestem.html");
609 $line = <TMP>;
610 close(TMP);
611 if ($line && $line =~ /DOCTYPE HTML/) {
612 &util::rm("$output_filestem.err") if -e "$output_filestem.err";
613
614 # Inserted this code to remove the images directory if it was still empty after
615 # the html was generated (in case there were no images in the word document)
616 if(&is_dir_empty($assoc_dir)) {
617 #print STDERR "***gsConvert.pl: Image dir $assoc_dir is empty, removing***\n";
618 &util::rm_r($assoc_dir);
619 } else { # there was an image folder (it was generated)
620 # Therefore, the html file generated contains absolute links to the images
621 # If the folder contains images
622 # Replace them with relative links instead, so it can be moved elsewhere
623 &make_links_to_assocdir_relative($toppath, $docname, "$output_filestem.html", $assoc_dir, $docname."_files");
624 }
625 return 1;
626 }
627 }
628
629 # If here, an error of some sort occurred
630 &util::rm("$output_filestem.html") if -e "$output_filestem.html";
631 if (-e "$output_filestem.err") {
632 if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile"))) {
633 open (ERRLOG,"$output_filestem.err");
634 while (<ERRLOG>) {print FAILLOG $_;}
635 close FAILLOG;
636 close ERRLOG;
637 }
638 &util::rm("$output_filestem.err");
639 }
640
641 return 0;
642}
643
644
645# A method to check if a directory is empty (note that an empty directory still has non-zero size!!!)
646# Code is from http://episteme.arstechnica.com/eve/forums/a/tpc/f/6330927813/m/436007700831
647sub is_dir_empty
648{
649 my ($path) = @_;
650 opendir DIR, $path;
651 while(my $entry = readdir DIR) {
652 next if($entry =~ /^\.\.?$/);
653 closedir DIR;
654 return 0;
655 }
656 closedir DIR;
657 return 1;
658}
659
660# Method to work with doc_to_html - Word docs might contain images.
661# When such word docs are converted with wvWare, we make it generate a
662# <filename>_files folder with the associated images, while the html file
663# <filename> refers to the images using absolute paths to <filename>_files.
664# This method reads in that html file and replaces all the absolute paths to
665# the images in <filename>_files with the relative paths to the images from
666# that folder. (I.e. with <filename>_files/<imagename.ext>).
667sub make_links_to_assocdir_relative{
668 # toppath is the top-level folder in which the html file we're going to be fixing resides
669 # docname is just the name (without extension) of the html file
670 # html_file is the full path to the html file: /full/path/docname.html
671 # assoc_dir_path is toppath/docname_files
672 # assoc_dirname is the directory name of the folder with associated imgs: docname_files
673 my ($toppath, $docname, $html_file, $assoc_dir_path, $assoc_dirname) = @_;
674
675 # 1. Read all the contents of the html into a string
676 # open the original file for reading
677 unless(open(FIN, "<$html_file")) {
678 print STDERR "gsConvert.pl: Unable to open $html_file for reading absolute urls...ERROR\n";
679 return 0;
680 }
681 # From http://perl.plover.com/local.html
682 # "It's cheaper to read the file all at once, without all the splitting and reassembling.
683 # (Some people call this slurping the file.) Perl has a special feature to support this:
684 # If the $/ variable is undefined, the <...> operator will read the entire file all at once"
685 my $html_contents;
686 {
687 local $/ = undef; # Read entire file at once
688 $html_contents = <FIN>; # Now file is read in as one single 'line'
689 }
690 close(FIN); # close the file
691 #print STDERR $html_contents;
692
693 # 2. Replace (substitute) *all* ocurrences of the assoc_dir_path in a hrefs and img src
694 # values with assoc_dirname
695 # At the end: g means substitute all occurrences (global), while s at the end means treat
696 # all new lines as a regular space. This interacts with g to consider all the lines
697 # together as a single line so that multi-occurrences can be replaced.
698
699 # we can't just replace $assoc_dir_path with $assoc_dir
700 # $assoc_dir_path represents a regular expression that needs to be replaced
701 # if it contains ., -, [ or ] -- which all have special meaning in Perl regular expressions --
702 # we need to escape these first
703 my $safe_reg_expression = $assoc_dir_path;
704 $safe_reg_expression =~ s/\./\\./g;
705 $safe_reg_expression =~ s/\-/\\-/g;
706 $safe_reg_expression =~ s/\[/\\[/g;
707 $safe_reg_expression =~ s/\]/\\]/g;
708 $safe_reg_expression =~ s/ /%20/g; # wvWare put %20 in place of space, so we need to change our prefix to match
709
710 # The following regular expression substitution looks for <a or <image, followed by any other
711 # attributes and values until it comes to the FIRST (indicated by ?) href= or src=
712 # followed by " or ' no quotes at all around path, followed by the associated folder's pathname
713 # followed by characters (for the img filename), then finally the optional closing quotes
714 # in " or ' form, followed by any other attributes and values until the first > to end the tag.
715 # The substitution: all the parts preceding associated folder's pathname are retained,
716 # the associated folder path name is replaced by associated folder directory name
717 # and the rest upto and including the closing > tag is retained.
718 # The sg at the end of the pattern match treats all of html_contents as a single line (s)
719 # and performs a global replace (g) meaning that all occurrences that match in that single line
720 # are substituted.
721 $html_contents =~ s/(<(a|img).*?(href|src)=(\"|\')?)$safe_reg_expression(.*?(\"|\')?.*?>)/$1$assoc_dirname$5/sg;
722 #$html_contents =~ s/$safe_reg_expression/$assoc_dirname/gs; # this works, used as fall-back
723 # now replace any %20 chars in filenames of href or src attributes to use literal space ' '. Calls a function for this
724 $html_contents =~ s/(<(a|img).*?(href|src)=(\"|\')?)(.*)(.*?(\"|\')?.*?>)/&percent_twenty_to_space($1, $5, $6)/sge;
725
726 #print STDERR "assoc_dirname: ****$assoc_dirname***\n";
727 #print STDERR "safe_reg_expression: ****$safe_reg_expression***\n";
728
729 # delete the original file and recreate it
730 my $copy_of_filename = $html_file;
731 &util::rm($copy_of_filename); # deleted the file
732
733 # Recreate the original file for writing the updated contents
734 unless(open(FOUT, ">$html_file")) { # open it as a new file for writing
735 print STDERR "gsConvert.pl: Unable to open $html_file for writing relative links...ERROR\n";
736 return 0;
737 }
738 # write out the updated contents and close the file
739 print FOUT $html_contents;
740 close(FOUT);
741 return 1;
742}
743
744# Utility routine to convert all %20 introduced by wvWare in link pathnames into space again
745sub percent_twenty_to_space
746{
747 my ($pre, $text, $post) = @_;
748
749 $text =~ s/%20/ /g;
750
751 return "$pre$text$post";
752}
753
754# Attempt to convert a word document to html with the word2html scripting program
755sub native_doc_to_html {
756 ($input_filename, $output_filestem) = @_;
757
758 my $vbScript = &util::filename_cat($ENV{'GSDLHOME'}, "bin",
759 $ENV{'GSDLOS'}, "word2html");
760
761 $vbScript = "word2html" if ($ENV{'GSDLOS'} =~ /^windows$/i);
762 if (-e "$output_filestem.html") {
763 print STDERR "*** The conversion file has existed\n";
764 return 1;
765 }
766
767 my $cmd = "";
768 if ($timeout) {$cmd = "ulimit -t $timeout;";}
769 #$cmd .= "$vbScript \"$input_filename\" \"$output_filestem.html\"";
770 #$cmd .= "$vbScript $input_filename $output_filestem.html";
771 $cmd .= "$vbScript \"$input_filename\" \"$output_filestem.html\"";
772
773 # redirecting STDERR
774 $cmd .= " 2> \"$output_filestem.err\""
775 if ($ENV {'GSDLOS'} !~ /^windows$/i || $is_winnt_2000);
776
777 # execute the command
778 $!=0;
779 if (system($cmd)!=0)
780 {
781 print STDERR "Error executing word2Html converter:$!\n";
782 if (-s "$output_filestem.err") {
783 open (ERRFILE, "<$output_filestem.err");
784
785 my $write_to_fail_log=0;
786 if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile")))
787 {$write_to_fail_log=1;}
788
789 my $line;
790 while ($line=<ERRFILE>) {
791 if ($line =~ /\w/) {
792 print STDERR "$line";
793 print FAILLOG "$line" if ($write_to_fail_log);
794 }
795 if ($line !~ m/startup error/) {next;}
796 print STDERR " (given an invalid .DOC file?)\n";
797 print FAILLOG " (given an invalid .DOC file?)\n"
798 if ($write_to_fail_log);
799
800 } # while ERRFILE
801 close FAILLOG if ($write_to_fail_log);
802 }
803 return 0; # we can try any_to_text
804 }
805
806 # Was the conversion successful?
807 if (-s "$output_filestem.html") {
808 open(TMP, "$output_filestem.html");
809 $line = <TMP>;
810 close(TMP);
811 if ($line && $line =~ /html/) {
812 &util::rm("$output_filestem.err") if -e "$output_filestem.err";
813 return 1;
814 }
815 }
816
817 # If here, an error of some sort occurred
818 &util::rm("$output_filestem.html") if -e "$output_filestem.html";
819 if (-e "$output_filestem.err") {
820 if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile"))) {
821 open (ERRLOG,"$output_filestem.err");
822 while (<ERRLOG>) {print FAILLOG $_;}
823 close FAILLOG;
824 close ERRLOG;
825 }
826 &util::rm("$output_filestem.err");
827 }
828 return 0;
829}
830
831# Attempt to convert an RTF document to html with rtftohtml
832
833sub rtf_to_html {
834 my ($input_filename, $output_filestem) = @_;
835
836 # formulate the command
837 $cmd = "";
838 if ($timeout) {$cmd = "ulimit -t $timeout;";}
839 $cmd .= "rtftohtml";
840 #$cmd .= "rtf-converter";
841
842 $cmd .= " -o \"$output_filestem.html\" \"$input_filename\"";
843
844 $cmd .= " 2>\"$output_filestem.err\""
845 if ($ENV{'GSDLOS'} !~ /^windows$/i || $is_winnt_2000);
846
847
848 # execute the command
849 $!=0;
850 if (system($cmd)!=0)
851 {
852 print STDERR "Error executing rtf converter $!\n";
853 # don't currently bother printing out error log...
854 # keep going, in case it still created an HTML file...
855 }
856
857 # Was the conversion successful?
858 my $was_successful=0;
859 if (-s "$output_filestem.html") {
860 # make sure we have some content other than header
861 open (HTML, "$output_filestem.html"); # what to do if fail?
862 my $line;
863 my $past_header=0;
864 while ($line=<HTML>) {
865
866 if ($past_header == 0) {
867 if ($line =~ /<body>/) {$past_header=1;}
868 next;
869 }
870
871 $line =~ s/<[^>]+>//g;
872 if ($line =~ /\w/ && $past_header) { # we found some content...
873 $was_successful=1;
874 last;
875 }
876 }
877 close HTML;
878 }
879
880 if ($was_successful) {
881 &util::rm("$output_filestem.err")
882 if (-e "$output_filestem.err");
883 # insert the (modified) table of contents, if it exists.
884 if (-e "${output_filestem}_ToC.html") {
885 &util::mv("$output_filestem.html","$output_filestem.src");
886 my $open_failed=0;
887 open HTMLSRC, "$output_filestem.src" || ++$open_failed;
888 open TOC, "${output_filestem}_ToC.html" || ++$open_failed;
889 open HTML, ">$output_filestem.html" || ++$open_failed;
890
891 if ($open_failed) {
892 close HTMLSRC;
893 close TOC;
894 close HTML;
895 &util::mv("$output_filestem.src","$output_filestem.html");
896 return 1;
897 }
898
899 # print out header info from src html.
900 while (defined($_ = <HTMLSRC>) && $_ =~ /\w/) {
901 print HTML "$_";
902 }
903
904 # print out table of contents, making links relative
905 <TOC>; <TOC>; # ignore first 2 lines
906 print HTML scalar(<TOC>); # line 3 = "<ol>\n"
907 my $line;
908 while ($line=<TOC>) {
909 $line =~ s@</body></html>$@@ ; # only last line has this
910 # make link relative
911 $line =~ s@href=\"[^\#]+@href=\"@;
912 print HTML $line;
913 }
914 close TOC;
915
916 # rest of html src
917 while (<HTMLSRC>) {
918 print HTML $_;
919 }
920 close HTMLSRC;
921 close HTML;
922
923 &util::rm("${output_filestem}_ToC.html");
924 &util::rm("${output_filestem}.src");
925 }
926 # we don't yet do anything with footnotes ($output_filestem_fn.html) :(
927 return 1; # success
928 }
929
930 if (-e "$output_filestem.err") {
931 if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile")))
932 {
933 print FAILLOG "Error - rtftohtml - couldn't extract text\n";
934 #print FAILLOG "Error - rtf-converter - couldn't extract text\n";
935 print FAILLOG " (rtf file might be too recent):\n";
936 open (ERRLOG, "$output_filestem.err");
937 while (<ERRLOG>) {print FAILLOG $_;}
938 close ERRLOG;
939 close FAILLOG;
940 }
941 &util::rm("$output_filestem.err");
942 }
943
944 &util::rm("$output_filestem.html") if (-e "$output_filestem.html");
945
946 return 0;
947}
948
949
950# Convert a pdf file to html with the pdftohtml command
951
952sub pdf_to_html {
953 my ($dirname, $input_filename, $output_filestem) = @_;
954
955 $cmd = "";
956 if ($timeout) {$cmd = "ulimit -t $timeout;";}
957 $cmd .= "perl -S pdftohtml.pl -zoom $pdf_zoom";
958 $cmd .= " -c" if ($pdf_complex);
959 $cmd .= " -i" if ($pdf_ignore_images);
960 $cmd .= " -a" if ($pdf_allow_images_only);
961 $cmd .= " -hidden" unless ($pdf_nohidden);
962 $cmd .= " \"$input_filename\" \"$output_filestem\"";
963
964 if ($ENV{'GSDLOS'} !~ /^windows$/i || $is_winnt_2000) {
965 $cmd .= " > \"$output_filestem.out\" 2> \"$output_filestem.err\"";
966 } else {
967 $cmd .= " > \"$output_filestem.err\"";
968 }
969
970 $!=0;
971
972 my $retval=system($cmd);
973 if ($retval!=0)
974 {
975 print STDERR "Error executing pdftohtml.pl";
976 if ($!) {print STDERR ": $!";}
977 print STDERR "\n";
978 }
979
980 # make sure the converter made something
981 if ($retval!=0 || ! -s "$output_filestem.html")
982 {
983 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
984 # print out the converter's std err, if any
985 if (-s "$output_filestem.err") {
986 open (ERRLOG, "$output_filestem.err") || die "$!";
987 print STDERR "pdftohtml error log:\n";
988 while (<ERRLOG>) {
989 print STDERR "$_";
990 }
991 close ERRLOG;
992 }
993 &util::rm("$output_filestem.html") if (-e "$output_filestem.html");
994 if (-e "$output_filestem.err") {
995 if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile")))
996 {
997 open (ERRLOG, "$output_filestem.err");
998 while (<ERRLOG>) {print FAILLOG $_;}
999 close ERRLOG;
1000 close FAILLOG;
1001 }
1002 &util::rm("$output_filestem.err");
1003 }
1004 return 0;
1005 }
1006
1007 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
1008 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
1009 return 1;
1010}
1011
1012# Convert a pdf file to various types of image with the convert command
1013
1014sub pdf_to_img {
1015 my ($dirname, $input_filename, $output_filestem, $output_type) = @_;
1016
1017 # Check that ImageMagick is installed and available on the path (except for Windows 95/98)
1018 if (!($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT())) {
1019 my $result = `identify 2>&1`;
1020 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
1021 #ImageMagick is not installed, thus the convert utility is not available.
1022 print STDERR "*** ImageMagick is not installed, the convert utility is not available\n";
1023 return 0;
1024 }
1025 }
1026
1027 $cmd = "";
1028 if ($timeout) {$cmd = "ulimit -t $timeout;";}
1029 $output_type =~ s/.*\_(.*)/$1/i;
1030 $cmd .= "perl -S pdftoimg.pl -convert_to $output_type \"$input_filename\" \"$output_filestem\"";
1031 if ($ENV{'GSDLOS'} !~ /^windows$/i || $is_winnt_2000) {
1032 $cmd .= " > \"$output_filestem.out\" 2> \"$output_filestem.err\"";
1033 } else {
1034 $cmd .= " > \"$output_filestem.err\"";
1035 }
1036
1037 # don't include path on windows (to avoid having to play about
1038 # with quoting when GSDLHOME might contain spaces) but assume
1039 # that the PATH is set up correctly
1040 $!=0;
1041 my $retval=system($cmd);
1042 if ($retval!=0)
1043 {
1044 print STDERR "Error executing pdftoimg.pl";
1045 if ($!) {print STDERR ": $!";}
1046 print STDERR "\n";
1047 }
1048
1049 #make sure the converter made something
1050 #if ($retval !=0) || ! -s "$output_filestem")
1051 if ($retval !=0)
1052 {
1053 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
1054 #print out the converter's std err, if any
1055 if (-s "$output_filestem.err") {
1056 open (ERRLOG, "$output_filestem.err") || die "$!";
1057 print STDERR "pdftoimg error log:\n";
1058 while (<ERRLOG>) {
1059 print STDERR "$_";
1060 }
1061 close ERRLOG;
1062 }
1063 #&util::rm("$output_filestem.html") if (-e "$output_filestem.html");
1064 if (-e "$output_filestem.err") {
1065 if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile")))
1066 {
1067 open (ERRLOG, "$output_filestem.err");
1068 while (<ERRLOG>) {print FAILLOG $_;}
1069 close ERRLOG;
1070 close FAILLOG;
1071 }
1072 &util::rm("$output_filestem.err");
1073 }
1074 return 0;
1075 }
1076 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
1077 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
1078 return 1;
1079}
1080
1081# Convert a PDF file to text with the pdftotext command
1082
1083sub pdf_to_text {
1084 my ($dirname, $input_filename, $output_filestem) = @_;
1085
1086 my $cmd = "pdftotext \"$input_filename\" \"$output_filestem.text\"";
1087
1088 if ($ENV{'GSDLOS'} !~ /^windows$/i) {
1089 $cmd .= " > \"$output_filestem.out\" 2> \"$output_filestem.err\"";
1090 } else {
1091 $cmd .= " > \"$output_filestem.err\"";
1092 }
1093
1094 if (system($cmd)!=0)
1095 {
1096 print STDERR "Error executing $cmd: $!\n";
1097 &util::rm("$output_filestem.text") if (-e "$output_filestem.text");
1098 }
1099
1100 # make sure there is some extracted text.
1101 if (-e "$output_filestem.text") {
1102 open (EXTR_TEXT, "$output_filestem.text") || warn "open: $!";
1103 binmode(EXTR_TEXT); # just in case...
1104 my $line="";
1105 my $seen_text=0;
1106 while (($seen_text==0) && ($line=<EXTR_TEXT>)) {
1107 if ($line=~ /\w/) {$seen_text=1;}
1108 }
1109 close EXTR_TEXT;
1110 if ($seen_text==0) { # no text was extracted
1111 print STDERR "Error: pdftotext found no text\n";
1112 &util::rm("$output_filestem.text");
1113 }
1114 }
1115
1116 # make sure the converter made something
1117 if (! -s "$output_filestem.text")
1118 {
1119 # print out the converters std err, if any
1120 if (-s "$output_filestem.err") {
1121 open (ERRLOG, "$output_filestem.err") || die "$!";
1122 print STDERR "pdftotext error log:\n";
1123 while (<ERRLOG>) {
1124 print STDERR "$_";
1125 }
1126 close ERRLOG;
1127 }
1128 # does this converter create a .out file?
1129 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
1130 &util::rm("$output_filestem.text") if (-e "$output_filestem.text");
1131 if (-e "$output_filestem.err") {
1132 if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile")))
1133 {
1134 open (ERRLOG,"$output_filestem.err");
1135 while (<ERRLOG>) {print FAILLOG $_;}
1136 close ERRLOG;
1137 close FAILLOG;
1138 }
1139 &util::rm("$output_filestem.err");
1140 }
1141 return 0;
1142 }
1143 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
1144 return 1;
1145}
1146
1147# Convert a PostScript document to text
1148# note - just using "ps2ascii" isn't good enough, as it
1149# returns 0 for a postscript interpreter error. ps2ascii is just
1150# a wrapper to "gs" anyway, so we use that cmd here.
1151
1152sub ps_to_text {
1153 my ($input_filename, $output_filestem) = @_;
1154
1155 my $error = "";
1156
1157 # if we're on windows we'll fall straight through without attempting
1158 # to use gs
1159 if ($ENV{'GSDLOS'} =~ /^windows$/i) {
1160 $error = "Windows does not support gs";
1161
1162 } else {
1163 my $cmd = "";
1164 if ($timeout) {$cmd = "ulimit -t $timeout; ";}
1165 $cmd .= "gs -q -dNODISPLAY -dNOBIND -dWRITESYSTEMDICT -dSIMPLE -c save ";
1166 $cmd .= "-f ps2ascii.ps \"$input_filename\" -c quit > \"$output_filestem.text\"";
1167 #$cmd .= "pstotext -output \"$output_filestem.text\" $input_filename\"";
1168 $cmd .= " 2> $output_filestem.err";
1169 $!=0;
1170
1171 my $retcode=system($cmd);
1172 $retcode = $? >> 8; # see man perlfunc - system for this...
1173 # if system returns -1 | 127 (couldn't start program), look at $! for message
1174
1175 if ($retcode!=0) {if ($!) {$error=$!;} else {$error="couldn't run.\n";}}
1176 elsif (! -e "$output_filestem.text") {
1177 $error="did not create output file.\n";
1178 }
1179 else
1180 { # make sure the interpreter didn't get an error. It is technically
1181 # possible for the actual text to start with this, but....
1182 open PSOUT, "$output_filestem.text";
1183 if (<PSOUT> =~ /^Error: (.*)/) {
1184 $error="interpreter error - \"$1\"";
1185 }
1186 close PSOUT;
1187 }
1188 }
1189
1190 if ($error ne "")
1191 {
1192 print STDERR "Warning: Error executing gs: $error\n";
1193 &util::rm("$output_filestem.text") if (-e "$output_filestem.text");
1194
1195 if ("$faillogfile" ne "" && defined(open (FAILLOG, ">>$faillogfile")))
1196 {
1197 print FAILLOG "gs - $error\n";
1198 if (-e "$output_filestem.err") {
1199 open(ERRLOG, "$output_filestem.err");
1200 while (<ERRLOG>) {print FAILLOG $_;}
1201 close ERRLOG;
1202 }
1203 close FAILLOG;
1204 }
1205 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
1206
1207
1208 # Fine then. We'll just do a lousy job by ourselves...
1209 # Based on 5-line regexp sed script found at:
1210 # http://snark.ptc.spbu.ru/mail-archives/lout/brown/msg00003.html
1211 #
1212 print STDERR "Stripping text from postscript\n";
1213 my $errorcode=0;
1214 open (IN, "$input_filename")
1215 || ($errorcode=1, warn "Couldn't read file: $!");
1216 open (OUT, ">$output_filestem.text")
1217 || ($errorcode=1, warn "Couldn't write file: $!");
1218 if ($errorcode) {print STDERR "errors\n";return 0;}
1219
1220 my $text=""; # this is for whole .ps file...
1221 $text = join('', <IN>); # see man perlport, under "System Resources"
1222 close IN;
1223
1224 # Make sure this is a ps file...
1225 if ($text !~ /^%!/) {
1226 print STDERR "Bad postscript header: not '%!'\n";
1227 if ($faillogfile ne "" && defined(open(FAILLOG, ">>$faillogfile")))
1228 {
1229 print FAILLOG "Bad postscript header: not '%!'\n";
1230 close FAILLOG;
1231 }
1232 return 0;
1233 }
1234
1235 # if ps has Page data, then use it to delete all stuff before it.
1236 $text =~ s/^.*?%%Page:.*?\n//s; # treat string as single line
1237
1238 # remove all leading non-data stuff
1239 $text =~ s/^.*?\(//s;
1240
1241 # remove all newline chars for easier processing
1242 $text =~ s/\n//g;
1243
1244 # Big assumption here - assume that if any co-ordinates are
1245 # given, then we are at the end of a sentence.
1246 $text =~ s/\)-?\d+\ -?\d+/\) \(\n\)/g;
1247
1248 # special characters--
1249 $text =~ s/\(\|\)/\(\ - \)/g; # j -> em-dash?
1250
1251 # ? ps text formatting (eg italics?) ?
1252 $text =~ s/Fn\(f\)/\(\{\)/g; # f -> {
1253 $text =~ s/Fn\(g\)/\(\}\)/g; # g -> }
1254 $text =~ s/Fn\(j\)/\(\|\)/g; # j -> |
1255 # default - remove the rest
1256 $text =~ s/\ ?F.\((.+?)\)/\($1\)/g;
1257
1258 # attempt to add whitespace between words...
1259 # this is based purely on observation, and may be completely wrong...
1260 $text =~ s/([^F])[defghijkuy]\(/$1 \( /g;
1261 # eg I notice "b(" is sometimes NOT a space if preceded by a
1262 # negative number.
1263 $text =~ s/\)\d+ ?b\(/\) \( /g;
1264
1265 # change quoted braces to brackets
1266 $text =~ s/([^\\])\\\(/$1\{/g;
1267 $text =~ s/([^\\])\\\)/$1\}/g ;
1268
1269 # remove everything that is not between braces
1270 $text =~ s/\)([^\(\)])+?\(//sg ;
1271
1272 # remove any Trailer eof stuff.
1273 $text =~ s/\)[^\)]*$//sg;
1274
1275 ### ligatures have special characters...
1276 $text =~ s/\\013/ff/g;
1277 $text =~ s/\\014/fi/g;
1278 $text =~ s/\\015/fl/g;
1279 $text =~ s/\\016/ffi/g;
1280 $text =~ s/\\214/fi/g;
1281 $text =~ s/\\215/fl/g;
1282 $text =~ s/\\017/\n\* /g; # asterisk?
1283 $text =~ s/\\023/\023/g; # e acute ('e)
1284 $text =~ s/\\177/\252/g; # u"
1285# $text =~ s/ ?? /\344/g; # a"
1286
1287 print OUT "$text";
1288 close OUT;
1289 }
1290 # wrap the text - use a minimum length. ie, first space after this length.
1291 my $wrap_length=72;
1292 &util::mv("$output_filestem.text", "$output_filestem.text.tmp");
1293 open INFILE, "$output_filestem.text.tmp" ||
1294 die "Couldn't open file: $!";
1295 open OUTFILE, ">$output_filestem.text" ||
1296 die "Couldn't open file for writing: $!";
1297 my $line="";
1298 while ($line=<INFILE>) {
1299 while (length($line)>0) {
1300 if (length($line)>$wrap_length) {
1301 $line =~ s/^(.{$wrap_length}[^\s]*)\s*//;
1302 print OUTFILE "$1\n";
1303 } else {
1304 print OUTFILE "$line";
1305 $line="";
1306 }
1307 }
1308 }
1309 close INFILE;
1310 close OUTFILE;
1311 &util::rm("$output_filestem.text.tmp");
1312
1313 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
1314 return 1;
1315}
1316
1317
1318# Convert a PS file to various types of image with the convert utility
1319sub ps_to_img {
1320 my ($dirname, $input_filename, $output_filestem, $output_type) = @_;
1321
1322 # Check that ImageMagick is installed and available on the path (except for Windows 95/98)
1323 if (!($ENV{'GSDLOS'} eq "windows" && !Win32::IsWinNT())) {
1324 my $result = `identify 2>&1`;
1325 if ($? == -1 || $? == 256) { # Linux and Windows return different values for "program not found"
1326 #ImageMagick is not installed, thus the convert utility is not available.
1327 print STDERR "*** ImageMagick is not installed, the convert utility is not available\n";
1328 return 0;
1329 }
1330 }
1331
1332 $cmd = "";
1333 if ($timeout) {$cmd = "ulimit -t $timeout;";}
1334 $output_type =~ s/.*\_(.*)/$1/i;
1335 $cmd .= "perl -S pstoimg.pl -convert_to $output_type \"$input_filename\" \"$output_filestem\"";
1336 if ($ENV{'GSDLOS'} !~ /^windows$/i || $is_winnt_2000) {
1337 $cmd .= " > \"$output_filestem.out\" 2> \"$output_filestem.err\"";
1338 } else {
1339 $cmd .= " > \"$output_filestem.err\"";
1340 }
1341
1342 # don't include path on windows (to avoid having to play about
1343 # with quoting when GSDLHOME might contain spaces) but assume
1344 # that the PATH is set up correctly
1345 $!=0;
1346 my $retval=system($cmd);
1347 if ($retval!=0)
1348 {
1349 print STDERR "Error executing pstoimg.pl";
1350 if ($!) {print STDERR ": $!";}
1351 print STDERR "\n";
1352 }
1353
1354 #make sure the converter made something
1355 #if ($retval !=0) || ! -s "$output_filestem")
1356 if ($retval !=0)
1357 {
1358 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
1359 #print out the converter's std err, if any
1360 if (-s "$output_filestem.err") {
1361 open (ERRLOG, "$output_filestem.err") || die "$!";
1362 print STDERR "pstoimg error log:\n";
1363 while (<ERRLOG>) {
1364 print STDERR "$_";
1365 }
1366 close ERRLOG;
1367 }
1368 #&util::rm("$output_filestem.html") if (-e "$output_filestem.html");
1369 if (-e "$output_filestem.err") {
1370 if ($faillogfile ne "" && defined(open(FAILLOG,">>$faillogfile")))
1371 {
1372 open (ERRLOG, "$output_filestem.err");
1373 while (<ERRLOG>) {print FAILLOG $_;}
1374 close ERRLOG;
1375 close FAILLOG;
1376 }
1377 &util::rm("$output_filestem.err");
1378 }
1379 return 0;
1380 }
1381 &util::rm("$output_filestem.err") if (-e "$output_filestem.err");
1382 &util::rm("$output_filestem.out") if (-e "$output_filestem.out");
1383 return 1;
1384}
1385
1386# Convert any file to HTML with a crude perl implementation of the
1387# UNIX strings command.
1388
1389sub any_to_html {
1390 ($input_filename, $output_filestem) = @_;
1391
1392 # First generate a text file
1393 return 0 unless (&any_to_text($input_filename, $output_filestem));
1394
1395 # create an HTML file from the text file
1396 open(TEXT, "<$output_filestem.text");
1397 open(HTML, ">$output_filestem.html");
1398
1399 print HTML "<html><head>\n";
1400 print HTML "<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html\">\n";
1401 print HTML "<META NAME=\"GENERATOR\" CONTENT=\"Greenstone any_to_html\">\n";
1402 print HTML "</head><body>\n\n";
1403
1404 my $line;
1405 while ($line=<TEXT>) {
1406 $line =~ s/</&lt;/g;
1407 $line =~ s/>/&gt;/g;
1408 if ($line =~ /^\s*$/) {
1409 print HTML "<p>";
1410 } else {
1411 print HTML "<br> ", $line;
1412 }
1413 }
1414 print HTML "\n</body></html>\n";
1415
1416 close HTML;
1417 close TEXT;
1418
1419 &util::rm("$output_filestem.text") if (-e "$output_filestem.text");
1420 return 1;
1421}
1422
1423# Convert any file to TEXT with a crude perl implementation of the
1424# UNIX strings command.
1425# Note - this assumes ascii charsets :( (jrm21)
1426
1427sub any_to_text {
1428 ($input_filename, $output_filestem) = @_;
1429
1430 if (!$use_strings) {
1431 return 0;
1432 }
1433
1434 print STDERR "\n**** In any to text****\n\n";
1435 open(IN, "<$input_filename") || return 0;
1436 binmode(IN);
1437 open(OUT, ">$output_filestem.text") || return 0;
1438
1439 my ($line);
1440 my $output_line_count = 0;
1441 while (<IN>) {
1442 $line = $_;
1443
1444 # delete anything that isn't a printable character
1445 $line =~ s/[^\040-\176]+/\n/sg;
1446
1447 # delete any string less than 10 characters long
1448 $line =~ s/^.{0,9}$/\n/mg;
1449 while ($line =~ /^.{1,9}$/m) {
1450 $line =~ s/^.{0,9}$/\n/mg;
1451 $line =~ s/\n+/\n/sg;
1452 }
1453
1454 # remove extraneous whitespace
1455 $line =~ s/\n+/\n/gs;
1456 $line =~ s/^\n//gs;
1457
1458 # output whatever is left
1459 if ($line =~ /[^\n ]/) {
1460 print OUT $line;
1461 ++$output_line_count;
1462 }
1463 }
1464
1465 close OUT;
1466 close IN;
1467
1468 if ($output_line_count) { # try to protect against binary only formats
1469 return 1;
1470 }
1471
1472 &util::rm("$output_filestem.text");
1473 return 0;
1474
1475}
Note: See TracBrowser for help on using the repository browser.