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