source: trunk/gsdl/perllib/plugins/StructuredHTMLPlug.pm@ 10504

Last change on this file since 10504 was 10496, checked in by kjdon, 19 years ago

added some sanity checks, renamed the checkout_toc option to delete_toc

  • Property svn:keywords set to Author Date Id Revision
File size: 13.5 KB
Line 
1###########################################################################
2#
3# StructuredHTMLPlug.pm -- html plugin with extra facilities for teasing out
4# hierarchical structure (such as h1, h2, h3, or user-defined tags) in an
5# HTML document
6#
7# A component of the Greenstone digital library software
8# from the New Zealand Digital Library Project at the
9# University of Waikato, New Zealand.
10#
11# Copyright (C) 1999 New Zealand Digital Library Project
12#
13# This program is free software; you can redistribute it and/or modify
14# it under the terms of the GNU General Public License as published by
15# the Free Software Foundation; either version 2 of the License, or
16# (at your option) any later version.
17#
18# This program is distributed in the hope that it will be useful,
19# but WITHOUT ANY WARRANTY; without even the implied warranty of
20# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21# GNU General Public License for more details.
22#
23# You should have received a copy of the GNU General Public License
24# along with this program; if not, write to the Free Software
25# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26#
27###########################################################################
28# This plugin is to process an HTML file where sections are divided by
29# user-defined headings tags. As it is difficult to predict what user's definition
30# this plugin allows to detect the user-defined titles up to three levels (level1, level2, level3...)
31# as well as allows to get rid of user-defined Table of Content (TOC)...
32# format:e.g. level1 (Abstract_title|ChapterTitle|Referencing Heading) level2(SectionHeading)...
33
34package StructuredHTMLPlug;
35
36use HTMLPlug;
37use ImagePlug;
38
39#use strict; # every perl program should have this!
40#no strict 'refs'; # make an exception so we can use variables as filehandles
41
42sub BEGIN {
43 @StructuredHTMLPlug::ISA = ('HTMLPlug');
44}
45
46my $arguments = [];
47
48my $options = { 'name' => "StructuredHTMLPlug",
49 'desc' => "{StructuredHTMLPlug.desc}",
50 'abstract' => "no",
51 'inherits' => "yes",
52 'args' => $arguments };
53
54sub new {
55 my ($class) = shift (@_);
56 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
57 push(@$pluginlist, $class);
58
59 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
60 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
61
62 my $self = (defined $hashArgOptLists)? new HTMLPlug($pluginlist,$inputargs,$hashArgOptLists): new HTMLPlug($pluginlist,$inputargs);
63
64 return bless $self, $class;
65}
66
67sub read {
68 my $self = shift (@_);
69 my ($pluginfo, $base_dir, $file, $metadata, $processor, $maxdocs, $gli) = @_;
70
71 my $filename = $file;
72 $filename = &util::filename_cat ($base_dir, $file) if $base_dir =~ /\w/;
73
74 if ($filename =~ m/\.html?$/) {
75 my $poss_doc_filename = $filename;
76 $poss_doc_filename =~ s/\.html?$/.doc/;
77
78 if (-e $poss_doc_filename) {
79 # this file has already been processed by Word plugin
80 return 0;
81 }
82 }
83 return $self->SUPER::read(@_);
84}
85
86sub process {
87 my $self = shift (@_);
88 #my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj) = @_;
89 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
90 my $outhandle = $self->{'outhandle'};
91
92 print $outhandle "StructuredHTMLPlug: processing $file\n"
93 if $self->{'verbosity'} > 1;
94 my @head_and_body = split(/<body/i,$$textref);
95 my $head = shift(@head_and_body);
96 my $body_text = join("<body", @head_and_body);
97
98 if (defined $self->{'extracted_word_metadata_fields'} && $self->{'extracted_word_metadata_fields'}=~ /\S/) {
99 my @doc_properties = split(/<xml>/i,$head);
100 my $doc_heading = shift(@doc_properties);
101 my $rest_doc_properties = join(" ", @doc_properties);
102 my @extracted_metadata = split(/<\/xml>/i, $rest_doc_properties);
103 my $extracted_metadata = shift (@extracted_metadata);
104 $self->extract_metadata($extracted_metadata, $metadata, $doc_obj);
105 }
106
107 # If delete_toc is enables, it means to get rid of toc and tof contents.
108 # get rid of TOC and TOF sections and their title
109 if ($self->{'delete_toc'} == 1){
110 #line-height:150%;mso-ansi-language:FR'>Contents<o:p></o:p></span></b></p>
111 # get rid of Table of Contents title and Table of Figures
112 # these two lines don't work - how can we do this properlly??
113 #$body_text =~ s/<p[^>]*><b><span[^>]*>(Table of Content.|Content.)<o:p><\/o:p><\/span><\/b><\/p>//isg;
114 #$body_text =~ s/<p[^>]*><b><span[^>]*>(Table of Figure.|Figure.)<o:p><\/o:p><\/span><\/b><\/p>//isg;
115 if (defined $self->{'toc_header'}&& $self->{'toc_header'} =~ /\S/){
116 $body_text =~ s/<p class=(($self->{'toc_header'})[^>]*)>(.+?)<\/p>//isg;
117 }
118 if (defined $self->{'tof_header'}&& $self->{'tof_header'}=~ /\S/) {
119 $body_text =~ s/<p class=(($self->{'tof_header'})[^>]*)>(.+?)<\/p>//isg;
120 }
121 }
122
123 if (defined $self->{'title_header'} && $self->{'title_header'}=~ /\S/){
124 $self->{'title_header'} =~ s/^(\()(.*)(\))/$2/is;
125 $body_text =~ s/<p class=(($self->{'title_header'})[^>]*)>(.+?)<\/p>/<p class=$1><title>$3<\/title><\/p>/isg;
126 }
127
128 if (defined $self->{'level1_header'} && $self->{'level1_header'}=~ /\S/ ){
129 $self->{'level1_header'} =~ s/^\((.*)\)/$1/i;
130 $body_text =~ s/<p class=(($self->{'level1_header'})[^>]*)>(.+?)<\/p>/<p class=$1><h1>$3<\/h1><\/p>/ig;
131 }
132
133 if (defined $self->{'level2_header'} && $self->{'level2_header'}=~ /\S/){
134 $self->{'level2_header'} =~ s/^\((.*)\)/$1/i;
135 $body_text =~ s/<p class=(($self->{'level2_header'})[^>]*)>(.+?)<\/p>/<p class=$1><h2>$3<\/h2><\/p>/ig;
136 }
137
138 if (defined $self->{'level3_header'} && $self->{'level3_header'}=~ /\S/ ){
139 $self->{'level3_header'} =~ s/^\((.*)\)/$1/is;
140 $body_text =~ s/<p class=(($self->{'level3_header'})[^>]*)>(.+?)<\/p>/<p class=$1><h3>$3<\/h3><\/p>/isg;
141 }
142 # Tidy up extra new lines
143 $body_text =~ s/(<p[^>]*><span[^>]*><o:p>&nbsp;<\/o:p><\/span><\/p>)//isg;
144 $body_text =~ s/(<p[^>]*><o:p>&nbsp;<\/o:p><\/p>)//isg;
145
146 my $body = "<body".$body_text;
147
148 my $section_text = $head;
149 $section_text .= "<!--\n<Section>\n-->\n";
150
151 # split HTML text on <h1>, <h2> etc tags
152 my @h_split = split(/<h/i,$body);
153
154 my $hnum = 0;
155
156 my $sectionh1 = 0;
157 $section_text .= shift(@h_split);
158
159 my $hc;
160 foreach $hc ( @h_split )
161 {
162 if ($hc =~ m/^([1-3])\s*.*?>(.*)$/s)
163 {
164 my $new_hnum = $1;
165 my $hc_after = $2;
166
167 if ($hc_after =~ m/^(.*?)<\/h$new_hnum>/is)
168 {
169 my $h_text = $1;
170 $hc =~ s/^(\&nbsp\;)+/\&nbsp\;/g;
171 # boil HTML down to some interesting text
172 $h_text =~ s/^[1-3]>//;
173 $h_text =~ s/<\/?.*?>//sg;
174 $h_text =~ s/\s+/ /sg;
175 $h_text =~ s/^\s$//s;
176 $h_text =~ s/(&nbsp;)+\W*/&nbsp;/sg;
177
178 if ($h_text =~ m/\w+/)
179 {
180 if ($new_hnum > $hnum)
181 {
182 # increase section nesting
183 $hnum++;
184 while ($hnum < $new_hnum)
185 {
186 my $spacing = " " x $hnum;
187 $section_text .= "<!--\n";
188 $section_text .= $spacing."<Section>\n";
189 $section_text .= "-->\n";
190 $hnum++;
191 }
192 }
193 else # ($new_hnum <= $hnum)
194 {
195 # descrease section nesting
196 while ($hnum >= $new_hnum)
197 {
198 my $spacing = " " x $hnum;
199 $section_text .= "<!--\n";
200 $section_text .= $spacing."</Section>\n";
201 $section_text .= "-->\n";
202 $hnum--;
203 }
204 $hnum++;
205 }
206
207 my $spacing = " " x $hnum;
208 $section_text .= "<!--\n";
209 $section_text .= $spacing."<Section>\n";
210 $section_text .= $spacing." <Description>\n";
211 $section_text .= $spacing." <Metadata name=\"Title\">$h_text</Metadata>";
212 $section_text .= $spacing." </Description>\n";
213 $section_text .= "-->\n";
214
215 print $outhandle $spacing."$h_text\n"
216 if $self->{'verbosity'} > 2;
217
218 $sectionh1++ if ($hnum==1);
219 }
220 }
221 else {
222### print STDERR "***** hc = <h$hc\n\n";
223
224 }
225 $section_text .= "<h$hc";
226 }
227 else
228 {
229 $section_text .= "<h$hc";
230 }
231 }
232
233 while ($hnum >= 1)
234 {
235 my $spacing = " " x $hnum;
236 $section_text .= "<!--\n";
237 $section_text .= $spacing."</Section>\n";
238 $section_text .= "-->\n";
239 $hnum--;
240 }
241
242 $section_text .= "<!--\n</Section>\n-->\n";
243
244 $$textref = $section_text;
245
246 # should be textref not testref???
247 #$$testref =~ s/<h(\d+)>(.*?)<\/h$1>/<Section><Metadata name=\"Title\">$1<\/Metadata></Section><h$1><\/h$1>/gi;
248
249 if ($sectionh1>0)
250 {
251 print $outhandle " Located section headings ..."
252 if $self->{'verbosity'} > 1;
253 }
254 print $outhandle " Passing on the HTMLPlug\n"
255 if $self->{'verbosity'} > 1;
256
257 $$textref =~ s/<!\[if !vml\]>/<![if vml]>/g;
258
259 $$textref =~ s/(&nbsp;)+/&nbsp;/sg;
260
261 ## $$textref =~ s/<o:p>&nbsp;<\/o:p>//g; # used with VML to space figures?
262
263 $self->SUPER::process(@_);
264
265 # associate original file with doc object
266 my $cursection = $doc_obj->get_top_section();
267 my $filename = &util::filename_cat($base_dir, $file);
268 if (-e $filename)
269 {
270 print $outhandle " Adding associated Word document\n"
271 if $self->{'verbosity'} > 1;
272
273 $doc_obj->associate_file($filename, "doc.doc", undef, $cursection);
274
275 my $doclink = "<a href=_httpcollection_/index/assoc/[archivedir]/doc.doc>";
276 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
277 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icondoc_");
278 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
279
280 my $file_size = -s $filename;
281 if ($file_size>1024)
282 {
283 my $fs_kbytes = sprintf("%d",$file_size/1024);
284 $doc_obj->add_utf8_metadata ($cursection, "filesize", "$fs_kbytes Kb");
285 }
286 else
287 {
288 $doc_obj->add_utf8_metadata ($cursection, "filesize", "$file_size bytes");
289 }
290
291 if ($file_size > 200000)
292 {
293 $doc_obj->add_utf8_metadata ($cursection, "fswarning", "1");
294 }
295 }
296}
297
298
299sub resize_if_necessary
300{
301 my ($self,$front,$back,$base_dir,$href) = @_;
302
303 # dig out width and height of image, if there
304 my $img_attributes = "$front back";
305 my ($img_width) = ($img_attributes =~ m/\s+width=\"?(\d+)\"?/i);
306 my ($img_height) = ($img_attributes =~ m/\s+height=\"?(\d+)\"?/i);
307
308 # derive local filename for image based on its URL
309 my $img_filename = $href;
310 $img_filename =~ s/^[^:]*:\/\///;
311 $img_filename = &util::filename_cat($base_dir, $img_filename);
312
313 # Replace %20's in URL with a space if required. Note that the filename
314 # may include the %20 in some situations
315 if ($img_filename =~ /\%20/) {
316 if (!-e $img_filename) {
317 $img_filename =~ s/\%20/ /g;
318 }
319 }
320 if ((-e $img_filename) && (defined $img_width) && (defined $img_height)) {
321 # get image info on width and height
322
323 my $outhandle = $self->{'outhandle'};
324 my $verbosity = $self->{'verbosity'};
325
326 my ($image_type, $actual_width, $actual_height, $image_size)
327 = &ImagePlug::identify($img_filename, $outhandle, $verbosity);
328
329 #print STDERR "**** $actual_width x $actual_height";
330 #print STDERR " (requested: $img_width x $img_height)\n";
331
332 if (($img_width < $actual_width) || ($img_height < $actual_height)) {
333 print $outhandle "Resizing $img_filename\n" if ($verbosity > 0);
334
335 # derive new image name based on current image
336 my ($tailname, $dirname, $suffix)
337 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
338
339 my $resized_filename
340 = &util::filename_cat($dirname, $tailname."_resized".$suffix);
341
342 #print STDERR "**** suffix = $suffix\n";
343
344 # Generate smaller image with convert
345 my $newsize = "$img_widthx$image_height";
346 my $command = "convert -interlace plane -verbose "
347 ."-geometry $newsize \"img_$filename\" \"$resized_filename\"";
348 print $outhandle "ImageResize: $command\n" if ($verbosity > 2);
349 my $result = '';
350 print $outhandle "ImageResize result: $result\n" if ($verbosity > 2);
351 }
352 }
353 return $href;
354}
355
356sub replace_images {
357 my $self = shift (@_);
358 my ($front, $link, $back, $base_dir,
359 $file, $doc_obj, $section) = @_;
360 # remove quotes from link at start and end if necessary
361 if ($link=~/^\"/) {
362 $link=~s/^\"//;$link=~s/\"$//;
363 $front.='"';
364 $back="\"$back";
365 }
366
367 $link =~ s/\n/ /g;
368
369 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
370
371## $href = $self->resize_if_necessary($front,$back,$base_dir,$href);
372
373 my $middle = $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section);
374
375 return $front . $middle . $back;
376}
377
378sub extract_metadata
379{
380 my $self = shift (@_);
381 my ($textref, $metadata, $doc_obj) = @_;
382 my $outhandle = $self->{'outhandle'};
383
384 # metadata fields to extract/save. 'key' is the (lowercase) name of the
385 # html meta, 'value' is the metadata name for greenstone to use
386 my %find_fields = ();
387 my ($tag,$value);
388
389 my $orig_field = "";
390 foreach my $field (split /,/, $self->{'extracted_word_metadata_fields'}) {
391 # support tag<tagname>
392 if ($field =~ /^(.*?)<(.*?)>$/) {
393 # "$2" is the user's preferred gs metadata name
394 $find_fields{lc($1)}=$2; # lc = lowercase
395 $orig_field = $1;
396 } else { # no <tagname> for mapping
397 # "$field" is the user's preferred gs metadata name
398 $find_fields{lc($field)}=$field; # lc = lowercase
399 $orig_field = $field;
400 }
401 if ($textref =~ m/<o:$orig_field>(.*)<\/o:$orig_field>/i){
402 $tag = $orig_field;
403 $value = $1;
404 if (!defined $value || !defined $tag){
405 print $outhandle "StructuredHTMLPlug: can't find VALUE in \"$tag\"\n";
406 next;
407 } else {
408 # clean up and add
409 chomp($value); # remove trailing \n, if any
410 $tag = $find_fields{lc($tag)};
411 print $outhandle " extracted \"$tag\" metadata \"$value\"\n"
412 if ($self->{'verbosity'} > 2);
413 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), $tag, $value);
414 }
415 }
416 }
417}
418
4191;
Note: See TracBrowser for help on using the repository browser.