1 | ###########################################################################
|
---|
2 | #
|
---|
3 | # HTMLPlug2.pm -- basic html plugin
|
---|
4 |
|
---|
5 |
|
---|
6 | # HTMLPlug2 is the same as HTMLPlug but has a -extract_subsections
|
---|
7 | # option. This option will be added to HTMLPlug and HTMLPlug2 will
|
---|
8 | # be removed once the wrinkles have been ironed out
|
---|
9 |
|
---|
10 |
|
---|
11 | #
|
---|
12 | # A component of the Greenstone digital library software
|
---|
13 | # from the New Zealand Digital Library Project at the
|
---|
14 | # University of Waikato, New Zealand.
|
---|
15 | #
|
---|
16 | # Copyright (C) 1999 New Zealand Digital Library Project
|
---|
17 | #
|
---|
18 | # This program is free software; you can redistribute it and/or modify
|
---|
19 | # it under the terms of the GNU General Public License as published by
|
---|
20 | # the Free Software Foundation; either version 2 of the License, or
|
---|
21 | # (at your option) any later version.
|
---|
22 | #
|
---|
23 | # This program is distributed in the hope that it will be useful,
|
---|
24 | # but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
25 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
26 | # GNU General Public License for more details.
|
---|
27 | #
|
---|
28 | # You should have received a copy of the GNU General Public License
|
---|
29 | # along with this program; if not, write to the Free Software
|
---|
30 | # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
31 | #
|
---|
32 | ###########################################################################
|
---|
33 |
|
---|
34 | #
|
---|
35 | # Note that this plugin handles frames only in a very simple way
|
---|
36 | # i.e. each frame is treated as a separate document. This means
|
---|
37 | # search results will contain links to individual frames rather
|
---|
38 | # than linking to the top level frameset.
|
---|
39 | # There may also be some problems caused by the _parent target
|
---|
40 | # (it's removed by this plugin)
|
---|
41 | # To use frames properly you'll need to use the WebPlug plugin.
|
---|
42 | #
|
---|
43 |
|
---|
44 |
|
---|
45 | package HTMLPlug2;
|
---|
46 |
|
---|
47 | use BasPlug;
|
---|
48 | use ghtml;
|
---|
49 | use unicode;
|
---|
50 | use util;
|
---|
51 | use parsargv;
|
---|
52 |
|
---|
53 | sub BEGIN {
|
---|
54 | @ISA = ('BasPlug');
|
---|
55 | }
|
---|
56 |
|
---|
57 | sub print_usage {
|
---|
58 | print STDERR "\n usage: plugin HTMLPlug [options]\n\n";
|
---|
59 | print STDERR " options:\n";
|
---|
60 | print STDERR " -nolinks Don't make any attempt to trap links (setting this flag may\n";
|
---|
61 | print STDERR " improve speed of building/importing but any relative links within\n";
|
---|
62 | print STDERR " documents will be broken).\n";
|
---|
63 | print STDERR " -keep_head Don't remove headers from html files.\n";
|
---|
64 | print STDERR " -no_metadata Don't attempt to extract any metadata from files.\n";
|
---|
65 | print STDERR " -metadata_fields Comma separated list of metadata fields to attempt to extract.\n";
|
---|
66 | print STDERR " Defaults to 'Title'.\n";
|
---|
67 | print STDERR " Use 'tag<tagname>' to have the contents of the first <tagname>\n";
|
---|
68 | print STDERR " pair put in a metadata element called 'tagname' Capitalise \n";
|
---|
69 | print STDERR " 'tagname' as you want the metadata capitalised in the GML \n";
|
---|
70 | print STDERR " file, since the tag extraction is case insensitive.\n";
|
---|
71 | print STDERR " -hunt_creator_metadata Find as much metadata as possible on authorship and place it in the\n ";
|
---|
72 | print STDERR " 'Creator' field. Requires the -metadata_fields flag.\n ";
|
---|
73 | print STDERR " -file_is_url Set if input filenames make up url of original source documents\n";
|
---|
74 | print STDERR " e.g. if a web mirroring tool was used to create the import\n";
|
---|
75 | print STDERR " directory structure\n";
|
---|
76 | print STDERR " -assoc_files Perl regular expression of file extensions to associate with\n";
|
---|
77 | print STDERR " html documents. Defaults to '(?i)\.(jpe?g|gif|png|css)\$'\n";
|
---|
78 | print STDERR " -rename_assoc_files Renames files associated with documents (e.g. images). Also\n";
|
---|
79 | print STDERR " creates much shallower directory structure (useful when creating\n";
|
---|
80 | print STDERR " collections to go on cd-rom).\n\n";
|
---|
81 | print STDERR " -title_sub Substitution expression to modify string stored as Title.\n";
|
---|
82 | print STDERR " Used by, for example, PDFHtml to remove Page 1 etc from text\n";
|
---|
83 | print STDERR " chosen to be used as the title.\n";
|
---|
84 | print STDERR " -extract_subsections Split document into subsections where <GSDLSection> tags are found.\n";
|
---|
85 | print STDERR " Note that by setting this option you implicitly set -no_metadata\n";
|
---|
86 | print STDERR " as all metadata should be included within the <GSDLSection> tags.\n";
|
---|
87 | print STDERR " Also, -keep_head will have no effect when this option is set.\n";
|
---|
88 | }
|
---|
89 |
|
---|
90 | sub new {
|
---|
91 | my $class = shift (@_);
|
---|
92 | my $self = new BasPlug ($class, @_);
|
---|
93 |
|
---|
94 | if (!parsargv::parse(\@_,
|
---|
95 | q^nolinks^, \$self->{'nolinks'},
|
---|
96 | q^keep_head^, \$self->{'keep_head'},
|
---|
97 | q^no_metadata^, \$self->{'no_metadata'},
|
---|
98 | q^metadata_fields/.*/Title^, \$self->{'metadata_fields'},
|
---|
99 | q^hunt_creator_metadata^, \$self->{'hunt_creator_metadata'},
|
---|
100 | q^w3mir^, \$self->{'w3mir'},
|
---|
101 | q^file_is_url^, \$self->{'file_is_url'},
|
---|
102 | q^assoc_files/.*/(?i)\.(jpe?g|gif|png|css)$^, \$self->{'assoc_files'},
|
---|
103 | q^rename_assoc_files^, \$self->{'rename_assoc_files'},
|
---|
104 | q^title_sub/.*/^, \$self->{'title_sub'},
|
---|
105 | q^extract_subsections^, \$self->{'extract_subsections'},
|
---|
106 | "allow_extra_options")) {
|
---|
107 |
|
---|
108 | print STDERR "\nIncorrect options passed to HTMLPlug, check your collect.cfg configuration file\n";
|
---|
109 | &print_usage();
|
---|
110 | die "\n";
|
---|
111 | }
|
---|
112 |
|
---|
113 | # retain this for backward compatibility (w3mir option was replaced by
|
---|
114 | # file_is_url)
|
---|
115 | if ($self->{'w3mir'}) {
|
---|
116 | $self->{'file_is_url'} = 1;
|
---|
117 | }
|
---|
118 |
|
---|
119 | $self->{'aux_files'} = {};
|
---|
120 | $self->{'dir_num'} = 0;
|
---|
121 | $self->{'file_num'} = 0;
|
---|
122 |
|
---|
123 | return bless $self, $class;
|
---|
124 | }
|
---|
125 |
|
---|
126 |
|
---|
127 | sub get_default_block_exp {
|
---|
128 | my $self = shift (@_);
|
---|
129 |
|
---|
130 | return q^(?i)\.(gif|jpe?g|png|css)$^;
|
---|
131 | }
|
---|
132 |
|
---|
133 | sub get_default_process_exp {
|
---|
134 | my $self = shift (@_);
|
---|
135 |
|
---|
136 | # the last option is an attempt to encode the concept of an html query ...
|
---|
137 | return q^(?i)(\.html?|\.shtml|\.shm|\.asp|\.php|\.cgi|.+\?.+=.*)$^;
|
---|
138 | }
|
---|
139 |
|
---|
140 |
|
---|
141 | # do plugin specific processing of doc_obj
|
---|
142 | sub process {
|
---|
143 | my $self = shift (@_);
|
---|
144 | my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
|
---|
145 | my $outhandle = $self->{'outhandle'};
|
---|
146 |
|
---|
147 | print $outhandle "HTMLPlug: processing $file\n"
|
---|
148 | if $self->{'verbosity'} > 1;
|
---|
149 |
|
---|
150 | my $cursection = $doc_obj->get_top_section();
|
---|
151 |
|
---|
152 | $self->extract_metadata ($textref, $metadata, $doc_obj, $cursection)
|
---|
153 | unless $self->{'no_metadata'} || $self->{'extract_subsections'};
|
---|
154 |
|
---|
155 | # Store URL for page as metadata - this can be used for an
|
---|
156 | # altavista style search interface. The URL won't be valid
|
---|
157 | # unless the file structure contains the domain name (i.e.
|
---|
158 | # like when w3mir is used to download a website).
|
---|
159 | my $web_url = "http://$file";
|
---|
160 | $web_url =~ s/\\/\//g; # for windows
|
---|
161 | $doc_obj->add_utf8_metadata($cursection, "URL", $web_url);
|
---|
162 |
|
---|
163 | # remove header and footer (if -extract_subsections was set the header
|
---|
164 | # and footer will be removed later)
|
---|
165 | unless ($self->{'keep_head'} || $self->{'extract_subsections'}) {
|
---|
166 | $$textref =~ s/^.*?<body[^>]*>//is;
|
---|
167 | $$textref =~ s/(<\/body[^>]*>|<\/html[^>]*>)//isg;
|
---|
168 | }
|
---|
169 |
|
---|
170 | if ($self->{'extract_subsections'}) {
|
---|
171 |
|
---|
172 | my $lt = '(?:<|<)';
|
---|
173 | my $gt = '(?:>|>)';
|
---|
174 | my $quot = '(?:"|”|"|\')';
|
---|
175 |
|
---|
176 | my @sections = split( /(${lt}GSDLSection.*?${gt}.*?${lt}\/GSDLSection${gt})/si, $$textref);
|
---|
177 | if (scalar(@sections) == 1) {
|
---|
178 | # appears to be a single section document (presumably we're
|
---|
179 | # processing an HTML file that has not been marked up with
|
---|
180 | # GSDLSection tags) - print warning and carry on
|
---|
181 | print $outhandle "HTMLPlug: Warning: $file appears to consist of only one section\n";
|
---|
182 | $self->process_section($textref, $base_dir, $file, $doc_obj, $cursection);
|
---|
183 |
|
---|
184 | } else {
|
---|
185 | # multiple section document (or single section document
|
---|
186 | # beginning with <GSDLSection> tags)
|
---|
187 | my $first = 1;
|
---|
188 | my $first_gsdlsection = 1;
|
---|
189 | my $first_level = 0;
|
---|
190 | my $lastlevel = 0;
|
---|
191 | foreach $section (@sections) {
|
---|
192 | if ($section =~ /${lt}GSDLSection(.*?)${gt}(.*?)${lt}\/GSDLSection${gt}/si) {
|
---|
193 | my $sec_attrs = $1;
|
---|
194 | my $sec_content = $2;
|
---|
195 |
|
---|
196 | my $thislevel = $lastlevel;
|
---|
197 |
|
---|
198 | if ($sec_attrs =~ /level\s*=\s*(${quot}?)(\d+)\1/si) {
|
---|
199 | $thislevel = $2;
|
---|
200 | } else {
|
---|
201 | $thislevel = 1 if $lastlevel == 0;
|
---|
202 | print $outhandle "HTMLPlug: Warning: $file contains a <GSDLSection> tag without\n";
|
---|
203 | print $outhandle " a 'level' attribute. Setting level to $thislevel\n";
|
---|
204 | }
|
---|
205 |
|
---|
206 | if ($first_gsdlsection) {
|
---|
207 | # top level section
|
---|
208 | $first_level = $thislevel;
|
---|
209 | $first_gsdlsection = 0;
|
---|
210 |
|
---|
211 | $self->add_gsdlsection_metadata($sec_content, $doc_obj, $cursection);
|
---|
212 |
|
---|
213 | } else {
|
---|
214 | if ($thislevel <= $first_level) {
|
---|
215 | $thislevel++;
|
---|
216 | print $outhandle "HTMLPlug: Warning: $file contains more than one top level (level $first_level\n";
|
---|
217 | print $outhandle " or lower) section. All those after the first will be reclassified as\n";
|
---|
218 | print $outhandle " level $thislevel sections\n";
|
---|
219 | } elsif ($thislevel > ($lastlevel+1)) {
|
---|
220 | print $outhandle "HTMLPlug: Warning: $file contains a jump in section levels from\n";
|
---|
221 | print $outhandle " $lastlevel to $thislevel. Level $thislevel section will be\n";
|
---|
222 | print $outhandle " reclassified as a level " . $lastlevel+1 . " section\n";
|
---|
223 | $thislevel = $lastlevel + 1;
|
---|
224 | }
|
---|
225 |
|
---|
226 | # close open sections if we're going down some levels
|
---|
227 | while ($thislevel <= $lastlevel) {
|
---|
228 | $cursection = $doc_obj->get_parent_section ($cursection);
|
---|
229 | $lastlevel--;
|
---|
230 | }
|
---|
231 | $cursection =
|
---|
232 | $doc_obj->insert_section($doc_obj->get_end_child($cursection));
|
---|
233 |
|
---|
234 | $self->add_gsdlsection_metadata($sec_content, $doc_obj, $cursection);
|
---|
235 | }
|
---|
236 |
|
---|
237 | $lastlevel = $thislevel;
|
---|
238 |
|
---|
239 | } else {
|
---|
240 | # remove headers and footers
|
---|
241 | $section =~ s/<head[^>]*>.*?<\/head>//isg;
|
---|
242 | $section =~ s/(<body[^>]*>|<\/body>)//isg;
|
---|
243 | $section =~ s/(<html[^>]*>|<\/html>)//isg;
|
---|
244 |
|
---|
245 | if ($first) {
|
---|
246 | # content before a GSDLSection tag is an error
|
---|
247 | if ($section =~ /\S/) {
|
---|
248 | print $outhandle "HTMLPlug: Warning: $file contains the following text before any\n";
|
---|
249 | print $outhandle " valid GSDLSection tag. This text will be ignored.\n";
|
---|
250 | print $outhandle "'$section'\n";
|
---|
251 | }
|
---|
252 | } else {
|
---|
253 | # this is the text of our current section
|
---|
254 | $self->process_section(\$section, $base_dir, $file, $doc_obj, $cursection);
|
---|
255 | }
|
---|
256 | }
|
---|
257 | $first = 0;
|
---|
258 | }
|
---|
259 | }
|
---|
260 | } else {
|
---|
261 | # single section document
|
---|
262 | $self->process_section($textref, $base_dir, $file, $doc_obj, $cursection);
|
---|
263 | }
|
---|
264 | return 1;
|
---|
265 | }
|
---|
266 |
|
---|
267 | sub add_gsdlsection_metadata {
|
---|
268 | my $self = shift (@_);
|
---|
269 | my ($metastring, $doc_obj, $cursection) = @_;
|
---|
270 |
|
---|
271 | my $lt = '(?:<|<)';
|
---|
272 | my $gt = '(?:>|>)';
|
---|
273 | my $quot = '(?:"|”|"|\')';
|
---|
274 |
|
---|
275 | $metastring =~ s/(${lt})metadata${gt}(.*?)${lt}\/metadata${gt}/$2/is;
|
---|
276 |
|
---|
277 | if ($1 ne "<") {
|
---|
278 | # this html file is the result of a conversion from something elese
|
---|
279 | # (i.e. <metadata> has become <metadata<) so we'll remove any
|
---|
280 | # excess html tags that the conversion process may have added for
|
---|
281 | # us
|
---|
282 | $metastring =~ s/<[^>]+>//gs;
|
---|
283 | }
|
---|
284 |
|
---|
285 | while ($metastring =~ s/\s*${lt}(\w+)(.*?)${gt}(.*?)${lt}\/\1${gt}//is) {
|
---|
286 | my $field = $1;
|
---|
287 | my $meta_attrs = $2 if defined $2;
|
---|
288 | my $value = $3;
|
---|
289 |
|
---|
290 | # mode attribute works in the same way as for RecPlug - i.e. the
|
---|
291 | # default is to override any previous values for this metadata
|
---|
292 | # field - if mode is set to accumulate we allow for multiple
|
---|
293 | # metadata values
|
---|
294 | if (defined $meta_attrs && $meta_attrs =~ /mode\s*=\s*(${quot}?)accumulate\1/is) {
|
---|
295 | $doc_obj->add_utf8_metadata($cursection, $field, $value);
|
---|
296 | } else {
|
---|
297 | $doc_obj->set_utf8_metadata_element($cursection, $field, $value);
|
---|
298 | }
|
---|
299 | }
|
---|
300 | }
|
---|
301 |
|
---|
302 | sub process_section {
|
---|
303 | my $self = shift (@_);
|
---|
304 | my ($textref, $base_dir, $file, $doc_obj, $cursection) = @_;
|
---|
305 |
|
---|
306 | # trap links
|
---|
307 | if (!$self->{'nolinks'}) {
|
---|
308 |
|
---|
309 | # usemap="./#index" not handled correctly => change to "#index"
|
---|
310 | $$textref =~ s/(<img[^>]*?usemap\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
|
---|
311 | $self->replace_usemap_links($1, $2, $3)/isge;
|
---|
312 |
|
---|
313 | $$textref =~ s/(<(?:a|area|frame|link)\s+[^>]*?\s*(?:href|src)\s*=\s*\"?)([^\">\s]+)(\"?[^>]*>)/
|
---|
314 | $self->replace_href_links ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
|
---|
315 | }
|
---|
316 |
|
---|
317 | # trap images
|
---|
318 |
|
---|
319 | # allow spaces if inside quotes - jrm21
|
---|
320 | $$textref =~ s/(<img[^>]*?src\s*=\s*)(\"[^\"]+\"|[^\s>]+)([^>]*>)/
|
---|
321 | $self->replace_images ($1, $2, $3, $base_dir, $file, $doc_obj, $cursection)/isge;
|
---|
322 |
|
---|
323 | # add text to document object
|
---|
324 | $doc_obj->add_utf8_text($cursection, $$textref);
|
---|
325 | }
|
---|
326 |
|
---|
327 |
|
---|
328 |
|
---|
329 |
|
---|
330 | sub replace_images {
|
---|
331 | my $self = shift (@_);
|
---|
332 | my ($front, $link, $back, $base_dir,
|
---|
333 | $file, $doc_obj, $section) = @_;
|
---|
334 | # remove quotes from link at start and end if necessary
|
---|
335 | if ($link=~/^\"/) {
|
---|
336 | $link=~s/^\"//;$link=~s/\"$//;
|
---|
337 | $front.='"';
|
---|
338 | $back="\"$back";
|
---|
339 | }
|
---|
340 |
|
---|
341 | $link =~ s/\n/ /g;
|
---|
342 | my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
|
---|
343 | return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back;
|
---|
344 | }
|
---|
345 |
|
---|
346 | sub replace_href_links {
|
---|
347 | my $self = shift (@_);
|
---|
348 | my ($front, $link, $back, $base_dir, $file, $doc_obj, $section) = @_;
|
---|
349 |
|
---|
350 | # attempt to sort out targets - frames are not handled
|
---|
351 | # well in this plugin and some cases will screw things
|
---|
352 | # up - e.g. the _parent target (so we'll just remove
|
---|
353 | # them all ;-)
|
---|
354 | $front =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
|
---|
355 | $back =~ s/(target=\"?)_top(\"?)/$1_gsdltop_$2/is;
|
---|
356 | $front =~ s/target=\"?_parent\"?//is;
|
---|
357 | $back =~ s/target=\"?_parent\"?//is;
|
---|
358 |
|
---|
359 | return $front . $link . $back if $link =~ /^\#/s;
|
---|
360 | $link =~ s/\n/ /g;
|
---|
361 |
|
---|
362 | my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
|
---|
363 | # href may use '\'s where '/'s should be on Windows
|
---|
364 | $href =~ s/\\/\//g;
|
---|
365 |
|
---|
366 | my ($filename) = $href =~ /^(?:.*?):(?:\/\/)?(.*)/;
|
---|
367 |
|
---|
368 | ##### leave all these links alone (they won't be picked up by intermediate
|
---|
369 | ##### pages). I think that's safest when dealing with frames, targets etc.
|
---|
370 | ##### (at least until I think of a better way to do it). Problems occur with
|
---|
371 | ##### mailto links from within small frames, the intermediate page is displayed
|
---|
372 | ##### within that frame and can't be seen. There is still potential for this to
|
---|
373 | ##### happen even with html pages - the solution seems to be to somehow tell
|
---|
374 | ##### the browser from the server side to display the page being sent (i.e.
|
---|
375 | ##### the intermediate page) in the top level window - I'm not sure if that's
|
---|
376 | ##### possible - the following line should probably be deleted if that can be done
|
---|
377 | return $front . $link . $back if $href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/is;
|
---|
378 |
|
---|
379 |
|
---|
380 | if (($rl == 0) || ($filename =~ /$self->{'process_exp'}/) ||
|
---|
381 | ($href =~ /\/$/) || ($href =~ /^(mailto|news|gopher|nntp|telnet|javascript):/i)) {
|
---|
382 | &ghtml::urlsafe ($href);
|
---|
383 | return $front . "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part . $back;
|
---|
384 |
|
---|
385 | } else {
|
---|
386 | # link is to some other type of file (eg image) so we'll
|
---|
387 | # need to associate that file
|
---|
388 | return $front . $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) . $back;
|
---|
389 | }
|
---|
390 | }
|
---|
391 |
|
---|
392 | sub add_file {
|
---|
393 | my $self = shift (@_);
|
---|
394 | my ($href, $rl, $hash_part, $base_dir, $doc_obj, $section) = @_;
|
---|
395 | my ($newname);
|
---|
396 |
|
---|
397 | my $filename = $href;
|
---|
398 | $filename =~ s/^[^:]*:\/\///;
|
---|
399 | $filename = &util::filename_cat($base_dir, $filename);
|
---|
400 |
|
---|
401 | my ($ext) = $filename =~ /(\.[^\.]*)$/;
|
---|
402 |
|
---|
403 | if ((!defined $ext) || ($ext !~ /$self->{'assoc_files'}/)) {
|
---|
404 | return "_httpextlink_&rl=" . $rl . "&href=" . $href . $hash_part;
|
---|
405 | }
|
---|
406 |
|
---|
407 | if ($self->{'rename_assoc_files'}) {
|
---|
408 | if (defined $self->{'aux_files'}->{$href}) {
|
---|
409 | $newname = $self->{'aux_files'}->{$href}->{'dir_num'} . "/" .
|
---|
410 | $self->{'aux_files'}->{$href}->{'file_num'} . $ext;
|
---|
411 | } else {
|
---|
412 | $newname = $self->{'dir_num'} . "/" . $self->{'file_num'} . $ext;
|
---|
413 | $self->{'aux_files'}->{$href} = {'dir_num' => $self->{'dir_num'}, 'file_num' => $self->{'file_num'}};
|
---|
414 | $self->inc_filecount ();
|
---|
415 | }
|
---|
416 | $doc_obj->associate_file($filename, $newname, undef, $section);
|
---|
417 | return "_httpcollimg_/$newname";
|
---|
418 |
|
---|
419 | } else {
|
---|
420 | ($newname) = $filename =~ /([^\/\\]*)$/;
|
---|
421 | $doc_obj->associate_file($filename, $newname, undef, $section);
|
---|
422 | return "_httpdocimg_/$newname";
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 |
|
---|
427 | sub format_link {
|
---|
428 | my $self = shift (@_);
|
---|
429 | my ($link, $base_dir, $file) = @_;
|
---|
430 |
|
---|
431 | my ($before_hash, $hash_part) = $link =~ /^([^\#]*)(\#?.*)$/;
|
---|
432 |
|
---|
433 | $hash_part = "" if !defined $hash_part;
|
---|
434 | if (!defined $before_hash || $before_hash !~ /[\w\.\/]/) {
|
---|
435 | my $outhandle = $self->{'outhandle'};
|
---|
436 | print $outhandle "HTMLPlug: ERROR - badly formatted tag ignored ($link)\n"
|
---|
437 | if $self->{'verbosity'};
|
---|
438 | return ($link, "", 0);
|
---|
439 | }
|
---|
440 |
|
---|
441 | if ($before_hash =~ s/^((?:http|ftp|file):\/\/)//i) {
|
---|
442 | my $type = $1;
|
---|
443 |
|
---|
444 | if ($link =~ /^(http|ftp):/i) {
|
---|
445 | # Turn url (using /) into file name (possibly using \ on windows)
|
---|
446 | my @http_dir_split = split('/', $before_hash);
|
---|
447 | $before_hash = &util::filename_cat(@http_dir_split);
|
---|
448 | }
|
---|
449 |
|
---|
450 | $before_hash = $self->eval_dir_dots($before_hash);
|
---|
451 |
|
---|
452 | my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
|
---|
453 |
|
---|
454 | my $rl = 0;
|
---|
455 | $rl = 1 if (-e $linkfilename);
|
---|
456 |
|
---|
457 | # make sure there's a slash on the end if it's a directory
|
---|
458 | if ($before_hash !~ /\/$/) {
|
---|
459 | $before_hash .= "/" if (-d $linkfilename);
|
---|
460 | }
|
---|
461 |
|
---|
462 | return ($type . $before_hash, $hash_part, $rl);
|
---|
463 |
|
---|
464 | } elsif ($link !~ /^(mailto|news|gopher|nntp|telnet|javascript):/i) {
|
---|
465 | if ($before_hash =~ s/^\/// || $before_hash =~ /\\/) {
|
---|
466 |
|
---|
467 | # the first directory will be the domain name if file_is_url
|
---|
468 | # to generate archives, otherwise we'll assume all files are
|
---|
469 | # from the same site and base_dir is the root
|
---|
470 | # This is not called in Windows
|
---|
471 | if ($self->{'file_is_url'}) {
|
---|
472 | my @dirs = split /[\/\\]/, $file;
|
---|
473 | my $domname = shift (@dirs);
|
---|
474 | $before_hash = &util::filename_cat($domname, $before_hash);
|
---|
475 | $before_hash =~ s/\\/\//g; # for windows
|
---|
476 | }
|
---|
477 | else
|
---|
478 | {
|
---|
479 | # see if link shares directory with source document
|
---|
480 | # => turn into relative link if this is so!
|
---|
481 | my $before_hash_fix = "";
|
---|
482 |
|
---|
483 | # filename_cat appends / or \ to the beginning of the
|
---|
484 | # file pathname but in Windows we don't want to do that
|
---|
485 | if ($ENV{'GSDLOS'} =~ /^windows/i) {
|
---|
486 | $before_hash_fix = $before_hash;
|
---|
487 | }
|
---|
488 | else {
|
---|
489 | $before_hash_fix = &util::filename_cat("",$before_hash);
|
---|
490 | }
|
---|
491 |
|
---|
492 | my $base_dir_win_match = $base_dir;
|
---|
493 | $base_dir_win_match =~ s/\\/\\\\/g;
|
---|
494 |
|
---|
495 | $before_hash_fix =~ s/^$base_dir_win_match(\\|\/)//;
|
---|
496 | $before_hash = $before_hash_fix;
|
---|
497 |
|
---|
498 | }
|
---|
499 |
|
---|
500 | } else {
|
---|
501 | # Turn relative file path into full path
|
---|
502 | my $dirname = &File::Basename::dirname($file);
|
---|
503 | $before_hash = &util::filename_cat($dirname, $before_hash);
|
---|
504 | $before_hash = $self->eval_dir_dots($before_hash);
|
---|
505 | }
|
---|
506 |
|
---|
507 | my $linkfilename = &util::filename_cat ($base_dir, $before_hash);
|
---|
508 |
|
---|
509 | # make sure there's a slash on the end if it's a directory
|
---|
510 | if ($before_hash !~ /\/$/) {
|
---|
511 | $before_hash .= "/" if (-d $linkfilename);
|
---|
512 | }
|
---|
513 |
|
---|
514 | return ("http://" . $before_hash, $hash_part, 1);
|
---|
515 |
|
---|
516 | } else {
|
---|
517 | # mailto, news, nntp, telnet, javascript or gopher link
|
---|
518 | return ($before_hash, "", 0);
|
---|
519 | }
|
---|
520 | }
|
---|
521 |
|
---|
522 | sub extract_first_NNNN_characters {
|
---|
523 | my $self = shift (@_);
|
---|
524 | my ($textref, $doc_obj, $thissection) = @_;
|
---|
525 |
|
---|
526 | foreach my $size (split /,/, $self->{'first'}) {
|
---|
527 | my $tmptext = $$textref;
|
---|
528 | $tmptext =~ s/.*<body[^>]*>//i;
|
---|
529 | $tmptext =~ s/$self->{'title_sub'}// if (defined $self->{'title_sub'});
|
---|
530 | $tmptext =~ s/<[^>]*>/ /g;
|
---|
531 | $tmptext =~ s/ / /g;
|
---|
532 | $tmptext =~ s/^\s+//;
|
---|
533 | $tmptext =~ s/\s+$//;
|
---|
534 | $tmptext =~ s/\s+/ /gs;
|
---|
535 | $tmptext = substr ($tmptext, 0, $size);
|
---|
536 | $tmptext =~ s/\s\S*$/…/;
|
---|
537 | $doc_obj->add_utf8_metadata ($thissection, "First$size", $tmptext);
|
---|
538 | }
|
---|
539 | }
|
---|
540 |
|
---|
541 | sub extract_metadata {
|
---|
542 | my $self = shift (@_);
|
---|
543 | my ($textref, $metadata, $doc_obj, $section) = @_;
|
---|
544 | my $outhandle = $self->{'outhandle'};
|
---|
545 |
|
---|
546 | # if we don't want metadata, we may as well not be here ...
|
---|
547 | return if (!defined $self->{'metadata_fields'});
|
---|
548 |
|
---|
549 | # hunt for an author look in the metadata elements:
|
---|
550 | if (defined $self->{'hunt_creator_metadata'}) {
|
---|
551 | for my $name (split /,/, "AUTHOR,AUTHOR.EMAIL,CREATOR,DC.CREATOR,DC.CREATOR.CORPORATENAME") {
|
---|
552 | if ($$textref =~ /<meta(\s*?)(?:name|http-equiv)\s*=\s*\"?$name\"?([^>]*)/is) {
|
---|
553 | my $content = $1 . $2;
|
---|
554 | if ($content =~ /content\s*=\s*\"?(.*)\"?/is) {
|
---|
555 | if (defined $1) {
|
---|
556 | my $value = $1;
|
---|
557 | $value =~ s/\"$//;
|
---|
558 | $value =~ s/\s+/ /gs;
|
---|
559 | $doc_obj->add_utf8_metadata($section, "Creator", $value);
|
---|
560 | print $outhandle " extracted Creator metadata \"$value\"\n"
|
---|
561 | if ($self->{'verbosity'} > 2);
|
---|
562 | next;
|
---|
563 | }
|
---|
564 | }
|
---|
565 | }
|
---|
566 | }
|
---|
567 | }
|
---|
568 |
|
---|
569 | foreach my $field (split /,/, $self->{'metadata_fields'}) {
|
---|
570 |
|
---|
571 | # don't need to extract field if it was passed in from a previous
|
---|
572 | # (recursive) plugin
|
---|
573 | next if defined $metadata->{$field};
|
---|
574 |
|
---|
575 | # see if there's a <meta> tag for this field
|
---|
576 | if ($$textref =~ /<meta(\s*?)(?:name|http-equiv)\s*=\s*\"?$field\"?([^>]*)/is) {
|
---|
577 | my $content = $1 . $2;
|
---|
578 | if ($content =~ /content\s*=\s*\"?(.*)\"?/is) {
|
---|
579 | if (defined $1) {
|
---|
580 | my $value = $1;
|
---|
581 | $value =~ s/\"$//;
|
---|
582 | $value =~ s/\s+/ /gs;
|
---|
583 | $value =~ s/\".*//gs;
|
---|
584 | $doc_obj->add_utf8_metadata($section, $field, $value);
|
---|
585 | print $outhandle " extracted \"$field\" metadata \"$value\"\n"
|
---|
586 | if ($self->{'verbosity'} > 2);
|
---|
587 | next;
|
---|
588 | }
|
---|
589 | }
|
---|
590 | }
|
---|
591 |
|
---|
592 | # TITLE: extract the document title
|
---|
593 |
|
---|
594 | if ($field =~ /^title$/i) {
|
---|
595 |
|
---|
596 | # see if there's a <title> tag
|
---|
597 | if ($$textref =~ /<title[^>]*>([^<]*)<\/title[^>]*>/is) {
|
---|
598 | if (defined $1) {
|
---|
599 | my $title = $1;
|
---|
600 | if ($title =~ /\w/) {
|
---|
601 | $title =~ s/<[^>]*>/ /g;
|
---|
602 | $title =~ s/ / /g;
|
---|
603 | $title =~ s/\s+/ /gs;
|
---|
604 | $title =~ s/^\s+//;
|
---|
605 | $title =~ s/\s+$//;
|
---|
606 | $doc_obj->add_utf8_metadata ($section, $field, $title);
|
---|
607 | print $outhandle " extracted \"$field\" metadata \"$title\"\n"
|
---|
608 | if ($self->{'verbosity'} > 2);
|
---|
609 | next;
|
---|
610 | }
|
---|
611 | }
|
---|
612 | }
|
---|
613 |
|
---|
614 | # if no title use first 100 characters
|
---|
615 | my $tmptext = $$textref;
|
---|
616 | $tmptext =~ s/<\/([^>]+)><\1>//g; # (eg) </b><b> - no space
|
---|
617 | $tmptext =~ s/<[^>]*>/ /g;
|
---|
618 | $tmptext =~ s/ / /g;
|
---|
619 | $tmptext =~ s/^\s+//s;
|
---|
620 | $tmptext =~ s/\s+$//;
|
---|
621 | $tmptext =~ s/\s+/ /gs;
|
---|
622 | $tmptext =~ s/$self->{'title_sub'}// if (defined $self->{'title_sub'});
|
---|
623 | $tmptext = substr ($tmptext, 0, 100);
|
---|
624 | $tmptext =~ s/\s\S*$/.../;
|
---|
625 | $doc_obj->add_utf8_metadata ($section, $field, $tmptext);
|
---|
626 | print $outhandle " extracted \"$field\" metadata \"$tmptext\"\n"
|
---|
627 | if ($self->{'verbosity'} > 2);
|
---|
628 | next;
|
---|
629 | }
|
---|
630 |
|
---|
631 |
|
---|
632 | # tag: extract the text between the first <H1> and </H1> tags
|
---|
633 | if ($field =~ /^tag[a-z0-9]+$/i) {
|
---|
634 |
|
---|
635 | my $tag = $field;
|
---|
636 | $tag =~ s/^tag//i;
|
---|
637 | my $tmptext = $$textref;
|
---|
638 | $tmptext =~ s/\s+/ /gs;
|
---|
639 | if ($tmptext =~ /<$tag[^>]*>/i) {
|
---|
640 | foreach my $word ($tmptext =~ m/<$tag[^>]*>(.*?)<\/$tag[^>]*>/g) {
|
---|
641 | $word =~ s/ / /g;
|
---|
642 | $word =~ s/<[^>]*>/ /g;
|
---|
643 | $word =~ s/^\s+//;
|
---|
644 | $word =~ s/\s+$//;
|
---|
645 | $word =~ s/\s+/ /gs;
|
---|
646 | if ($word ne "") {
|
---|
647 | $doc_obj->add_utf8_metadata ($section, $tag, $word);
|
---|
648 | print $outhandle " extracted \"$tag\" metadata \"$word\"\n"
|
---|
649 | if ($self->{'verbosity'} > 2);
|
---|
650 | }
|
---|
651 | }
|
---|
652 | }
|
---|
653 | next;
|
---|
654 | }
|
---|
655 | }
|
---|
656 | }
|
---|
657 |
|
---|
658 |
|
---|
659 | # evaluate any "../" to next directory up
|
---|
660 | # evaluate any "./" as here
|
---|
661 | sub eval_dir_dots {
|
---|
662 | my $self = shift (@_);
|
---|
663 | my ($filename) = @_;
|
---|
664 |
|
---|
665 | my $dirsep_os = &util::get_os_dirsep();
|
---|
666 | my @dirsep = split(/$dirsep_os/,$filename);
|
---|
667 |
|
---|
668 | my @eval_dirs = ();
|
---|
669 | foreach my $d (@dirsep) {
|
---|
670 | if ($d eq "..") {
|
---|
671 | pop(@eval_dirs);
|
---|
672 |
|
---|
673 | } elsif ($d eq ".") {
|
---|
674 | # do nothing!
|
---|
675 |
|
---|
676 | } else {
|
---|
677 | push(@eval_dirs,$d);
|
---|
678 | }
|
---|
679 | }
|
---|
680 |
|
---|
681 | return &util::filename_cat(@eval_dirs);
|
---|
682 | }
|
---|
683 |
|
---|
684 | sub replace_usemap_links {
|
---|
685 | my $self = shift (@_);
|
---|
686 | my ($front, $link, $back) = @_;
|
---|
687 |
|
---|
688 | $link =~ s/^\.\///;
|
---|
689 | return $front . $link . $back;
|
---|
690 | }
|
---|
691 |
|
---|
692 | sub inc_filecount {
|
---|
693 | my $self = shift (@_);
|
---|
694 |
|
---|
695 | if ($self->{'file_num'} == 1000) {
|
---|
696 | $self->{'dir_num'} ++;
|
---|
697 | $self->{'file_num'} = 0;
|
---|
698 | } else {
|
---|
699 | $self->{'file_num'} ++;
|
---|
700 | }
|
---|
701 | }
|
---|
702 |
|
---|
703 |
|
---|
704 | # Extend the BasPlug read_file so that strings like é are
|
---|
705 | # converted to UTF8 internally.
|
---|
706 | #
|
---|
707 | # We don't convert < or > or & or " in case
|
---|
708 | # they interfere with the GML files
|
---|
709 |
|
---|
710 | sub read_file {
|
---|
711 | my ($self, $filename, $encoding, $language, $textref) = @_;
|
---|
712 |
|
---|
713 | &BasPlug::read_file($self, $filename, $encoding, $language, $textref);
|
---|
714 |
|
---|
715 | # turn \ into \\ so that the rest of greenstone doesn't think there
|
---|
716 | # is an escape code following.
|
---|
717 | $$textref =~ s/\\/\\\\/go;
|
---|
718 |
|
---|
719 | # Convert things like é to their UTF8 equivalents
|
---|
720 | $$textref =~ s/&(lt|gt|amp|quot);/&z$1;/go;
|
---|
721 | $$textref =~ s/&([^;]+);/&unicode::ascii2utf8(\&ghtml::getcharequiv($1,1))/gseo;
|
---|
722 | $$textref =~ s/&z(lt|gt|amp|quot);/&$1;/go;
|
---|
723 | }
|
---|
724 |
|
---|
725 | 1;
|
---|