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