1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676
|
#!/usr/bin/perl
use strict;
#
# Version 3.0.1 19-September-2002
#
# External converter for htdig 3.1.4 or later (Perl5 or later)
# Usage: (in htdig.conf)
#
#external_parsers: application/rtf->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# text/rtf->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# application/pdf->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# application/postscript->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# application/msword->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# application/wordperfect5.1->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# application/wordperfect6.0->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# application/msexcel->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# application/vnd.ms-excel->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# application/vnd.ms-powerpoint->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl
# application/x-shockwave-flash->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl \
# application/x-shockwave-flash2-preview->text/html /opt/local/htdig-3.1.6/scripts/doc2html.pl
#
# Uses wp2html to convert Word and WordPerfect documents into HTML, and
# falls back to using Catdoc for Word and Catwpd for WordPerfect if
# Wp2html is unavailable or unable to convert.
#
# Uses range of other converters as available.
#
# If all else fails, attempts to read file without conversion.
#
########################################################################################
# Written by David Adams <d.j.adams@soton.ac.uk>.
# Based on conv_doc.pl written by Gilles Detillieux <grdetil@scrc.umanitoba.ca>,
# which in turn was based on the parse_word_doc.pl script, written by
# Jesse op den Brouw <MSQL_User@st.hhs.nl>.
########################################################################################
# Install Sys::AlarmCall if you can
eval "use Sys::AlarmCall";
######## Full paths of conversion utilities ##########
######## YOU MUST SET THESE ##########
######## (leave null those you don't have) ##########
# Wp2html converts Word & Wordperfect to HTML
# (get it from: http://www.res.bbsrc.ac.uk/wp2html/):
my $WP2HTML = '';
#Catwpd for WordPerfect to text conversion
# (you don't need this if you have wp2html)
# (get it from htdig site)
my $CATWPD = '';
# rtf2html converts Rich Text Font documents to HTML
# (get it from http://www.ice.ru/~vitus/catdoc/):
my $RTF2HTML = '';
# Catdoc converts Word (MicroSoft) to plain text
# (get it from: http://www.ice.ru/~vitus/catdoc/):
#version of catdoc for Word6, Word7 & Word97 files:
my $CATDOC = '';
#version of catdoc for Word2 files:
my $CATDOC2 = $CATDOC;
#version of catdoc for Word 5.1 for MAC:
my $CATDOCM = $CATDOC;
# PostScript to text converter
# (get it from the ghostscript 3.33 (or later) package):
my $CATPS = '';
# add to search path the directory which contains gs:
#$ENV{PATH} .= ":/usr/freeware/bin";
# PDF to HTML conversion script:
my $PDF2HTML = ''; # full pathname of pdf2html/pl script
# Excel (MicroSoft) to HTML converter
# (get it from www.xlhtml.org)
my $XLS2HTML = '';
# Excel (MicroSoft) to .CSV converter
# (you don't need this if you have xlhtml)
# (if you do want it, you can get it with catdoc)
my $CATXLS = '';
# Powerpoint (MicroSoft) to HTML converter
# (get it from www.xlhtml.org)
my $PPT2HTML = '';
# Shockwave Flash
# (extracts links from file)
my $SWF2HTML = ''; # full pathname of swf2html.pl script
# OpenOffice.org files
#my $OpenOffice2XML = '/usr/bin/unzip';
my $OpenOffice2XML = '';
# (remove multi-byte unicode from XML in OOo documents)
#my $strip_unicode = '| /usr/bin/iconv -c -s -f UTF-8 -t ISO-8859-1';
my $strip_unicode = '';
########################################################################
# Other Global Variables
my ($Success, $LOG, $Verbose, $CORE_MESS, $TMP, $RM, $ED, $Magic, $Time,
$Count, $Prog, $Input, $MIME_type, $URL, $Name, $Efile, $Maxerr,
$Redir, $Emark, $EEmark, $Method, $OP_Limit, $IP_Limit);
my (%HTML_Method, %TEXT_Method, %BAD_type);
&init; # initialise
my $size = -s $Input;
&quit("Input file size of $size at or above $IP_Limit limit" ) if $size >= $IP_Limit;
&store_methods; #
&read_magic; # Magic reveals type
&error_setup; # re-route standard error o/p from utilities
# see if a document -> HTML converter will work:
&run('&try_html');
if ($Success) { &quit(0) }
# try a document -> text converter:
&run('&try_text');
if ($Success) { &quit(0) }
# see if a known problem
my $fail = &cannot_do;
if ($fail) { &quit($fail) }
# last-ditch attempt, try copying document
&try_plain;
if ($Success) {&quit(0)}
&quit("UNABLE to convert");
#------------------------------------------------------------------------------
sub init {
# Doc2html log file
$LOG = $ENV{'DOC2HTML_LOG'} || '';
#
if ($LOG) {
open(STDERR,">>$LOG"); # ignore possible failure to open
} # else O/P really does go to STDERR
# Set to 1 for O/P to STDERR or Log file
$Verbose = exists($ENV{'DOC2HTML_LOG'}) ? 1 : 0;
# Limiting size of file doc2html.pl will try to process (default 20Mbyte)
$IP_Limit = $ENV{'DOC2HTML_IP_LIMIT'} || 20000000;
# Limit for O/P returned to htdig (default 10Mbyte)
$OP_Limit = $ENV{'DOC2HTML_OP_LIMIT'} || 10000000;
# Mark error message produced within doc2html script
$Emark = "!\t";
# Mark error message produced by conversion utility
$EEmark = "!!\t";
# Message to STDERR if core dump detected
$CORE_MESS = "CORE DUMPED";
# Directory for temporary files
$TMP = "/tmp/htdig";
if (! -d $TMP) {
mkdir($TMP,0700) or die "Unable to create directory \"$TMP\": $!";
}
# Current directory during run of script:
chdir $TMP or warn "Cannot change directory to $TMP\n";
# File for error output from utility
$Efile = 'doc_err.' . $$;
# Max. number of lines of error output from utility copied
$Maxerr = 10;
# System command to delete a file
$RM = "/bin/rm -f";
# Line editor to do substitution
$ED = "/bin/sed -e";
if ($^O eq "MSWin32") {$ED = "$^X -pe"}
$Time = 60; # allow 60 seconds for external utility to complete
$Success = 0;
$Count = 0;
$Method = '';
$Prog = $0;
$Prog =~ s#^.*/##;
$Prog =~ s/\..*?$//;
$Input = $ARGV[0] or die "No filename given\n";
$MIME_type = $ARGV[1] or die "No MIME-type given";
$URL = $ARGV[2] || '?';
$Name = $URL;
$Name =~ s#^.*/##;
$Name =~ s/%([A-F0-9][A-F0-9])/pack("C", hex($1))/gie;
if ($Verbose and not $LOG) { print STDERR "\n$Prog: [$MIME_type] " }
if ($LOG) { print STDERR "$URL [$MIME_type] " }
}
#------------------------------------------------------------------------------
sub store_methods {
# The method of dealing with each file type is set up here.
# Edit as necessary
my ($mime_type,$magic,$cmd,$cmdl,$type,$description);
my $name = quotemeta($Name);
####Document -> HTML converters####
# WordPerfect documents
if ($WP2HTML) {
$mime_type = "application/wordperfect|application/msword";
$cmd = $WP2HTML;
$cmdl = "($cmd -q -DTitle=\"[$name]\" -c doc2html.cfg -s doc2html.sty -i $Input -O; $RM CmdLine.ovr)";
$magic = '\377WPC';
&store_html_method('WordPerfect (wp2html)',$cmd,$cmdl,$mime_type,$magic);
}
# Word documents
if ($WP2HTML) {
$mime_type = "application/msword";
$cmd = $WP2HTML;
$cmdl = "($cmd -q -DTitle=\"[$name]\" -c doc2html.cfg -s doc2html.sty -i $Input -O; $RM CmdLine.ovr)";
$magic = '^\320\317\021\340';
&store_html_method('Word (wp2html)',$cmd,$cmdl,$mime_type,$magic);
}
# RTF documents
if ($RTF2HTML) {
$mime_type = "application/msword|application/rtf|text/rtf";
$cmd = $RTF2HTML;
# Rtf2html uses filename as title, change this:
$cmdl = "$cmd $Input | $ED \"s#^<TITLE>$Input</TITLE>#<TITLE>[$name]</TITLE>#\"";
$magic = '^{\134rtf';
&store_html_method('RTF (rtf2html)',$cmd,$cmdl,$mime_type,$magic);
}
# Microsoft Excel spreadsheet
if ($XLS2HTML) {
$mime_type = "application/msexcel|application/vnd.ms-excel";
$cmd = $XLS2HTML;
# xlHtml uses filename as title, change this:
$cmdl = "$cmd -fw $Input | $ED \"s#<TITLE>$Input</TITLE>#<TITLE>[$name]</TITLE>#\"";
$magic = '^\320\317\021\340';
&store_html_method('Excel (xlHtml)',$cmd,$cmdl,$mime_type,$magic);
}
# Microsoft Powerpoint Presentation
if ($PPT2HTML) {
$mime_type = "application/vnd.ms-powerpoint";
$cmd = $PPT2HTML;
# xlHtml uses filename as title, change this:
$cmdl = "$cmd $Input | $ED \"s#<TITLE>$Input</TITLE>#<TITLE>[$name]</TITLE>#\"";
$magic = '^\320\317\021\340';
&store_html_method('Powerpoint (pptHtml)',$cmd,$cmdl,$mime_type,$magic);
}
# Adobe PDF file using Perl script
if ($PDF2HTML) {
$mime_type = "application/pdf";
$cmd = $PDF2HTML;
# Replace default title (if used) with filename:
$cmdl = "$cmd $Input $mime_type $name";
$magic = '%PDF-|\0PDF CARO\001\000\377';
&store_html_method('PDF (pdf2html)',$cmd,$cmdl,$mime_type,$magic);
}
# Shockwave Flash file using Perl script
if ($SWF2HTML) {
$mime_type = "application/x-shockwave-flash";
$cmd = $SWF2HTML;
$cmdl = "$cmd $Input";
$magic = '^FWS[\001-\010]'; # versions 1 to 5, perhaps some later versions
&store_html_method('Shockwave-Flash (swf2html)',$cmd,$cmdl,$mime_type,$magic);
}
# OpenOffice Documents
if ($OpenOffice2XML) {
$mime_type = "application/vnd.sun.xml.writer|application/vnd.sun.xml.impress|application/vnd.sun.xml.calc|application/vnd.sun.xml.draw|application/vnd.sun.xml.math";
$cmd = $OpenOffice2XML;
$cmdl = "$cmd -p -qq $Input content.xml | /bin/sed -r 's/<[^>]*>/ /gi' $strip_unicode";
$magic = 'PK';
&store_html_method('OpenOffice XML (oo2xml)',$cmd,$cmdl,$mime_type,$magic);
}
####Document -> Text converters####
# Word6, Word7 & Word97 documents
if ($CATDOC) {
$mime_type = "application/msword";
$cmd = $CATDOC;
# -b option increases chance of success:
$cmdl = "$cmd -a -b -w $Input";
$magic = '^\320\317\021\340';
&store_text_method('Word (catdoc)',$cmd,$cmdl,$mime_type,$magic);
}
# Word2 documents
if ($CATDOC2) {
$mime_type = "application/msword";
$cmd = $CATDOC2;
$cmdl = "$cmd -a -b -w $Input";
$magic = '^\333\245-\000';
&store_text_method('Word2 (catdoc)',$cmd,$cmdl,$mime_type,$magic);
}
# Word 5.1 for MAC documents
if ($CATDOCM) {
$mime_type = "application/msword";
$cmd = $CATDOCM;
$cmdl = "$cmd -a -b -w $Input";
$magic = '^\3767\000#\000\000\000\000';
&store_text_method('MACWord (catdoc)',$cmd,$cmdl,$mime_type,$magic);
}
# PostScript files
if ($CATPS) {
$mime_type = "application/postscript";
$cmd = $CATPS;
# allow PS interpreter to give error messages
$cmdl = "($cmd; $RM _temp_.???) < $Input";
$magic = '^.{0,20}?%!|^\033%-12345.*\n%!';
&store_text_method('PostScript (ps2ascii)',$cmd,$cmdl,$mime_type,$magic);
}
# Microsoft Excel file
if ($CATXLS) {
$mime_type = "application/vnd.ms-excel";
$cmd = $CATXLS;
$cmdl = "$cmd $Input";
$magic = '^\320\317\021\340';
&store_text_method('MS Excel (xls2csv)',$cmd,$cmdl,$mime_type,$magic);
}
# WordPerfect document
if ($CATWPD) {
$mime_type = "application/wordperfect|application/msword";
$cmd = $CATWPD;
$cmdl = "$cmd $Input";
$magic = '\377WPC';
&store_text_method('WordPerfect (catwpd)',$cmd,$cmdl,$mime_type,$magic);
}
####Documents that cannot be converted####
# wrapped encapsulated Postscript
$type = "EPS";
$magic = '^\305\320\323\306 \0';
$description = 'wrapped Encapsulated Postscript';
&store_cannot_do($type,$magic,$description);
# Shockwave Flash version 6
$type = "SWF6";
$description = 'Shockwave-Flash Version 6';
$magic = '^CWS\006';
&store_cannot_do($type,$magic,$description);
#### Binary (data or whatever)
###$type = "BIN";
###$magic = '[\000-\007\016-\037\177]'; # rather crude test!
###$description = 'apparently binary';
###&store_cannot_do($type,$magic,$description);
return;
}
#------------------------------------------------------------------------------
sub read_magic {
# Read first bytes of file to check for file type
open(FILE, "< $Input") || die "Can't open file $Input\n";
read FILE,$Magic,256;
close FILE;
return;
}
#------------------------------------------------------------------------------
sub error_setup {
if ($Efile) {
open SAVERR, ">&STDERR";
if (open STDERR, "> $Efile") {
print SAVERR " Overwriting $Efile\n" if (-s $Efile);
$Redir = 1;
} else { close SAVERR }
}
}
#------------------------------------------------------------------------------
sub run {
my $routine = shift;
my $return;
if (defined &alarm_call) {
$return = alarm_call($Time, $routine);
} else {
eval $routine;
$return = $@ if $@;
}
if ($return) { &quit($return) }
}
#------------------------------------------------------------------------------
sub try_html {
my($set,$cmnd,$type);
$Success = 0;
foreach $type (keys %HTML_Method) {
$set = $HTML_Method{$type};
if (($MIME_type =~ m/$set->{'mime'}/i) and
($Magic =~ m/$set->{'magic'}/s)) { # found the method to use
$Method = $type;
my $cmnd = $set->{'cmnd'};
if (! -x $cmnd) {
warn "Unable to execute $cmnd for $type document\n";
return;
}
if (not open(CAT, "$set->{'command'} |")) {
warn "$cmnd doesn't want to be opened using pipe\n";
return;
}
while (<CAT>) {
# getting something, so it is working
$Success = 1;
if ($_ !~ m/^<!--/) { # skip comment lines inserted by converter
print;
$Count += length;
if ($Count > $OP_Limit) { last }
}
}
close CAT;
last;
}
}
return;
}
#------------------------------------------------------------------------------
sub try_text {
my($set,$cmnd,$type);
$Success = 0;
foreach $type (keys %TEXT_Method) {
$set = $TEXT_Method{$type};
if (($MIME_type =~ m/$set->{'mime'}/i) and
($Magic =~ m/$set->{'magic'}/s)) { # found the method to use
$Method = $type;
my $cmnd = $set->{'cmnd'};
if (! -x $cmnd) { die "Unable to execute $cmnd for $type document\n" }
# Open file via selected converter, output head, then its text:
open(CAT, "$set->{'command'} |") or
die "$cmnd doesn't want to be opened using pipe\n";
&head;
print "<BODY>\n<PRE>\n";
$Success = 1;
while (<CAT>) {
s/\255/-/g; # replace dashes with hyphens
# replace bell, backspace, tab. etc. with single space:
s/[\000-\040]+/ /g;
if (length > 1) { # if not just a single character, eg space
print &HTML($_), "\n";
$Count += length;
if ($Count > $OP_Limit) { last }
}
}
close CAT;
print "</PRE>\n</BODY>\n</HTML>\n";
last;
}
}
return;
}
#------------------------------------------------------------------------------
sub cannot_do {
my ($type,$set);
# see if known, unconvertable type
$Method = '';
foreach $type (keys %BAD_type) {
$set = $BAD_type{$type};
if ($Magic =~ m/$set->{'magic'}/s) { # known problem
return "CANNOT DO $set->{'desc'} ";
}
}
return 0;
}
#------------------------------------------------------------------------------
sub try_plain {
$Success = 0;
####### if ($Magic !~ m/^[\000-\007\016-\037\177]) {
if (-T $Input) { # Looks like text, so go for it:
$Method = 'Plain Text';
open(FILE, "<$Input") || die "Error reading $Input\n";
$Success = 1;
$Method = 'Plain Text';
&head;
print "<BODY>\n<PRE>\n";
while (<FILE>) {
# replace bell, backspace, tab. etc. with single space:
s/[\000-\040\177]+/ /g;
if (length > 1) {
print &HTML($_), "\n";
$Count += length;
if ($Count > $OP_Limit) { last }
}
}
close FILE;
print "</PRE>\n</BODY>\n</HTML>\n";
} else { $Method = '' }
return;
}
#------------------------------------------------------------------------------
sub HTML {
my $text = shift;
$text =~ s/\f/\n/gs; # replace form feed
$text =~ s/\s+/ /g; # replace multiple spaces, etc. with a single space
$text =~ s/\s+$//gm; # remove trailing spaces
$text =~ s/&/&/g;
$text =~ s/</</g;
$text =~ s/>/>/g;
return $text;
}
#------------------------------------------------------------------------------
sub store_html_method {
my $type = shift;
my $cmnd = shift;
my $cline = shift;
my $mime = shift;
my $magic = shift;
$HTML_Method{$type} = {
'mime' => $mime,
'magic' => $magic,
'cmnd' => $cmnd,
'command' => $cline,
};
return;
}
#------------------------------------------------------------------------------
sub store_text_method {
my $type = shift;
my $cmnd = shift;
my $cline = shift;
my $mime = shift;
my $magic = shift;
$TEXT_Method{$type} = {
'mime' => $mime,
'magic' => $magic,
'cmnd' => $cmnd,
'command' => $cline,
};
return;
}
#------------------------------------------------------------------------------
sub store_cannot_do {
my $type = shift;
my $magic = shift;
my $desc = shift;
$BAD_type{$type} = {
'magic' => $magic,
'desc' => $desc,
};
return;
}
#------------------------------------------------------------------------------
sub head {
print "<HTML>\n<HEAD>\n";
print "<TITLE>[" . $Name . "]</TITLE>\n";
print "</HEAD>\n";
}
#------------------------------------------------------------------------------
sub quit {
if ($Redir) { # end redirection of STDERR to temporary file
close STDERR;
open STDERR, ">&SAVERR";
}
if ($Verbose) {
print STDERR "$Method $Count" if ($Success);
print STDERR "\n";
}
if ($Count > $OP_Limit) {
print STDERR $Emark, "Output truncated after limit $OP_Limit reached\n";
}
my $return = shift;
if ($return) {
print STDERR $Emark, $return, "\n";
$return = 1;
}
chdir $TMP;
if ($Efile && -s $Efile) {
open EFILE, "<$Efile";
my $c = 0;
while (<EFILE>) {
$c++;
if ($c <= $Maxerr) {
print STDERR $EEmark, $_;
}
}
close EFILE;
print STDERR $Emark, " ... (total of $c lines of error messages)\n" if ($c > $Maxerr);
}
unlink $Efile if ($Efile && -e $Efile);
if (-e "core" && (-M "core" < 0)) {
print STDERR $Emark, "$CORE_MESS\n";
}
exit $return;
}
|