25 |
|
|
26 |
my $pdftotext = which('pdftotext'); |
my $pdftotext = which('pdftotext'); |
27 |
|
|
28 |
|
select(STDERR); $|=1; |
29 |
|
select(STDOUT); $|=1; |
30 |
|
|
31 |
print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose); |
print STDERR "using $pdftotext to convert pdf into html\n" if ($pdftotext && $verbose); |
32 |
|
|
33 |
find({ wanted => \&file, |
find({ wanted => \&file, |
35 |
no_chdir => 1 |
no_chdir => 1 |
36 |
}, $dir); |
}, $dir); |
37 |
|
|
38 |
|
sub dump_contents($$$) { |
39 |
|
my ($contents,$mtime,$path) = @_; |
40 |
|
|
41 |
|
return if (! $contents); # don't die on empty files |
42 |
|
|
43 |
|
use bytes; |
44 |
|
my $size = length $contents; |
45 |
|
|
46 |
|
print STDERR " [$size]" if ($verbose); |
47 |
|
|
48 |
|
# Output the document (to swish) |
49 |
|
print <<EOF; |
50 |
|
Path-Name: $path |
51 |
|
Content-Length: $size |
52 |
|
Last-Mtime: $mtime |
53 |
|
Document-Type: HTML |
54 |
|
|
55 |
|
EOF |
56 |
|
print $contents; |
57 |
|
|
58 |
|
} |
59 |
|
|
60 |
sub file { |
sub file { |
61 |
|
|
62 |
my $path = $_; |
my $path = $_; |
66 |
|
|
67 |
print STDERR "$path {converting}" if ($verbose); |
print STDERR "$path {converting}" if ($verbose); |
68 |
|
|
69 |
open(F,"$pdftotext -htmlmeta $path - |") || die "can't open $pdftotext with '$path'"; |
open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'"; |
70 |
my $html; |
my $html; |
71 |
while(<F>) { |
while(<F>) { |
72 |
# XXX why pdftotext barks if I try to use this is beyond me. |
# XXX why pdftotext barks if I try to use this is beyond me. |
76 |
} |
} |
77 |
close(F); |
close(F); |
78 |
|
|
79 |
$contents = "<!-- html from $path -->\n$html\n"; |
my ($pre_html,$pages,$post_html) = ('<html><head><title>$path :: page ##page_nr##</title></head><body><pre>',$html,'</pre></body></html>'); |
80 |
|
|
81 |
|
($pre_html,$pages,$post_html) = ($1,$2,$3) if ($html =~ m/^(<html>.+<pre>)(.+)(<\/pre>.+)$/si); |
82 |
|
|
83 |
|
$pre_html =~ s/<title>(.+?)<\/title>/<title>$1 :: page ##page_nr##<\/title>/si; |
84 |
|
|
85 |
|
my $page_nr = 1; |
86 |
|
foreach my $page (split(/\f/,$pages)) { |
87 |
|
my $pre_tmp = $pre_html; |
88 |
|
$pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s; |
89 |
|
dump_contents($pre_tmp . $page . $post_html,time(), $path); |
90 |
|
$page_nr++; |
91 |
|
} |
92 |
|
|
93 |
} else { |
} else { |
94 |
|
|
105 |
$contents .= "\n\n"; |
$contents .= "\n\n"; |
106 |
|
|
107 |
$contents = filter($contents,$collection); |
$contents = filter($contents,$collection); |
|
} |
|
108 |
|
|
109 |
# die "zero size content in '$path'" if (! $contents); |
# add optional components to path |
110 |
return if (! $contents); # don't die on empty files |
$path .= " $path_add" if ($path_add); |
111 |
|
|
112 |
my $mtime = time; |
dump_contents($contents,time(), $path); |
113 |
use bytes; |
} |
|
my $size = length $contents; |
|
|
|
|
|
print STDERR " [$size]\n" if ($verbose); |
|
|
|
|
|
# add optional components to path |
|
|
$path .= " $path_add" if ($path_add); |
|
|
|
|
|
# Output the document (to swish) |
|
|
print <<EOF; |
|
|
Path-Name: $path |
|
|
Content-Length: $size |
|
|
Last-Mtime: $mtime |
|
|
Document-Type: HTML |
|
114 |
|
|
115 |
EOF |
print STDERR "\n" if ($verbose); |
116 |
print $contents; |
# die "zero size content in '$path'" if (! $contents); |
117 |
|
|
118 |
} |
} |
119 |
|
|