1 |
#!/usr/local/bin/perl -w |
#!/usr/bin/perl -w |
2 |
use strict; |
use strict; |
3 |
use File::Find; |
use File::Find; |
4 |
use Getopt::Long; |
use Getopt::Long; |
5 |
|
use File::Which; |
6 |
|
|
7 |
my $collection; # name which will be inserted |
my $collection; # name which will be inserted |
8 |
my $path_add; # add additional info in path |
my $path_add; # add additional info in path |
19 |
|
|
20 |
my $dir = shift @ARGV || die "usage: $0 [dir]"; |
my $dir = shift @ARGV || die "usage: $0 [dir]"; |
21 |
|
|
|
|
|
22 |
my $basedir = $0; |
my $basedir = $0; |
23 |
$basedir =~ s,/[^/]+$,/,; |
$basedir =~ s,/[^/]+$,/,; |
24 |
require "$basedir/filter.pm"; |
require "$basedir/filter.pm"; |
25 |
|
|
26 |
|
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); |
32 |
|
|
33 |
find({ wanted => \&file, |
find({ wanted => \&file, |
34 |
follow => 1, |
follow => 1, |
35 |
no_chdir => 1 |
no_chdir => 1 |
36 |
}, $dir); |
}, $dir); |
37 |
|
|
38 |
sub file { |
sub dump_contents($$$) { |
39 |
|
my ($contents,$mtime,$path) = @_; |
|
return if (! -f || ! m/\.html*/i); |
|
|
|
|
|
# skip index files |
|
|
return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i); |
|
|
|
|
|
my $path = $_; |
|
|
|
|
|
open(F,"$path") || die "can't open file: $path"; |
|
|
print STDERR "$path" if ($verbose); |
|
|
my $contents; |
|
|
while(<F>) { |
|
|
$contents .= "$_"; |
|
|
} |
|
|
$contents .= "\n\n"; |
|
40 |
|
|
|
$contents = filter($contents,$collection); |
|
|
|
|
|
# die "zero size content in '$path'" if (! $contents); |
|
41 |
return if (! $contents); # don't die on empty files |
return if (! $contents); # don't die on empty files |
42 |
|
|
|
my $mtime = time; |
|
43 |
use bytes; |
use bytes; |
44 |
my $size = length $contents; |
my $size = length $contents; |
45 |
|
|
46 |
print STDERR " [$size]\n" if ($verbose); |
print STDERR " [$size]" if ($verbose); |
|
|
|
|
# add optional components to path |
|
|
$path .= " $path_add" if ($path_add); |
|
47 |
|
|
48 |
# Output the document (to swish) |
# Output the document (to swish) |
49 |
print <<EOF; |
print <<EOF; |
50 |
Path-Name: $path |
Path-Name: $path |
51 |
Content-Length: $size |
Content-Length: $size |
52 |
Last-Mtime: $mtime |
Last-Mtime: $mtime |
53 |
Document-Type: HTML |
Document-Type: html* |
54 |
|
|
55 |
EOF |
EOF |
56 |
print $contents; |
print $contents; |
57 |
|
|
58 |
} |
} |
59 |
|
|
60 |
|
sub file { |
61 |
|
|
62 |
|
my $path = $_; |
63 |
|
my $contents; |
64 |
|
|
65 |
|
if ($pdftotext && -f $path && $path =~ m/\.pdf$/i) { |
66 |
|
|
67 |
|
print STDERR "$path {converting}" if ($verbose); |
68 |
|
|
69 |
|
open(F,"$pdftotext -htmlmeta \"$path\" - |") || die "can't open $pdftotext with '$path'"; |
70 |
|
my $html; |
71 |
|
while(<F>) { |
72 |
|
# XXX why pdftotext barks if I try to use this is beyond me. |
73 |
|
#$contents .= $_; |
74 |
|
|
75 |
|
$html .= $_; |
76 |
|
} |
77 |
|
close(F); |
78 |
|
|
79 |
|
return if (! $html); |
80 |
|
|
81 |
|
my ($pre_html,$pages,$post_html) = ('<html><head><title>$path :: page ##page_nr##</title></head><body><pre>',$html,'</pre></body></html>'); |
82 |
|
|
83 |
|
($pre_html,$pages,$post_html) = ($1,$2,$3) if ($html =~ m/^(<html>.+?<pre>)(.+)(<\/pre>.+?)$/si); |
84 |
|
|
85 |
|
if ($collection) { |
86 |
|
$pre_html =~ s/<title>(.+?)<\/title>/<title>$collection :: page ##page_nr##<\/title>/si; |
87 |
|
} else { |
88 |
|
$pre_html =~ s/<title>(.+?)<\/title>/<title>$1 :: page ##page_nr##<\/title>/si; |
89 |
|
} |
90 |
|
|
91 |
|
my $page_nr = 1; |
92 |
|
foreach my $page (split(/\f/s,$pages)) { |
93 |
|
print STDERR " $page_nr" if ($verbose); |
94 |
|
my $pre_tmp = $pre_html; |
95 |
|
$pre_tmp =~ s/##page_nr##/$page_nr<\/title>/s; |
96 |
|
dump_contents($pre_tmp . $page . $post_html,time(), $path) if ($page !~ m/^\s*$/s); |
97 |
|
$page_nr++; |
98 |
|
} |
99 |
|
|
100 |
|
} else { |
101 |
|
|
102 |
|
return if (! -f $path || ! m/\.(html*|php|pl|txt|info|log|text)$/i); |
103 |
|
|
104 |
|
# skip index files |
105 |
|
return if (m/index_[a-z]\.html*/i || m/index_symbol\.html*/i); |
106 |
|
|
107 |
|
open(F,"$path") || die "can't open file: $path"; |
108 |
|
print STDERR "$path" if ($verbose); |
109 |
|
while(<F>) { |
110 |
|
$contents .= "$_"; |
111 |
|
} |
112 |
|
$contents .= "\n\n"; |
113 |
|
|
114 |
|
$contents = filter($contents,$collection); |
115 |
|
|
116 |
|
# add optional components to path |
117 |
|
$path .= " $path_add" if ($path_add); |
118 |
|
|
119 |
|
dump_contents($contents,time(), $path); |
120 |
|
} |
121 |
|
|
122 |
|
print STDERR "\n" if ($verbose); |
123 |
|
# die "zero size content in '$path'" if (! $contents); |
124 |
|
|
125 |
|
} |
126 |
|
|