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

Last change on this file since 10595 was 10595, checked in by chi, 18 years ago

Modification of level header regular expression.

  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 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 if (defined $self->{'toc_header'}&& $self->{'toc_header'} =~ /\S/){
114 $body_text =~ s/<p class=(($self->{'toc_header'})[^>]*)>(.+?)<\/p>//isg;
115 }
116 if (defined $self->{'tof_header'}&& $self->{'tof_header'}=~ /\S/) {
117 $body_text =~ s/<p class=(($self->{'tof_header'})[^>]*)>(.+?)<\/p>//isg;
118 }
119 }
120
121 if (defined $self->{'title_header'} && $self->{'title_header'}=~ /\S/){
122 $self->{'title_header'} =~ s/^(\()(.*)(\))/$2/is;
123 $body_text =~ s/<p class=(($self->{'title_header'})[^>]*)>(.+?)<\/p>/<p class=$1><title>$3<\/title><\/p>/isg;
124 }
125
126 if (defined $self->{'level1_header'} && $self->{'level1_header'}=~ /\S/ ){
127 $self->{'level1_header'} =~ s/^\((.*)\)/$1/i;
128 $body_text =~ s/<p class=(($self->{'level1_header'})[^>]*)>(.+?)<\/p>/<p class=$1><h1>$3<\/h1><\/p>/isg;
129 }
130
131 if (defined $self->{'level2_header'} && $self->{'level2_header'}=~ /\S/){
132 $self->{'level2_header'} =~ s/^\((.*)\)/$1/i;
133 $body_text =~ s/<p class=(($self->{'level2_header'})[^>]*)>(.+?)<\/p>/<p class=$1><h2>$3<\/h2><\/p>/isg;
134 }
135
136 if (defined $self->{'level3_header'} && $self->{'level3_header'}=~ /\S/ ){
137 $self->{'level3_header'} =~ s/^\((.*)\)/$1/is;
138 $body_text =~ s/<p class=(($self->{'level3_header'})[^>]*)>(.+?)<\/p>/<p class=$1><h3>$3<\/h3><\/p>/isg;
139 }
140 # Tidy up extra new lines
141 $body_text =~ s/(<p[^>]*><span[^>]*><o:p>&nbsp;<\/o:p><\/span><\/p>)//isg;
142 $body_text =~ s/(<p[^>]*><o:p>&nbsp;<\/o:p><\/p>)//isg;
143
144 my $body = "<body".$body_text;
145
146 my $section_text = $head;
147 $section_text .= "<!--\n<Section>\n-->\n";
148
149 # split HTML text on <h1>, <h2> etc tags
150 my @h_split = split(/<h/i,$body);
151
152 my $hnum = 0;
153
154 my $sectionh1 = 0;
155 $section_text .= shift(@h_split);
156
157 my $hc;
158 foreach $hc ( @h_split )
159 {
160 if ($hc =~ m/^([1-3])\s*.*?>(.*)$/s)
161 {
162 my $new_hnum = $1;
163 my $hc_after = $2;
164
165 if ($hc_after =~ m/^(.*?)<\/h$new_hnum>/is)
166 {
167 my $h_text = $1;
168 $hc =~ s/^(\&nbsp\;)+/\&nbsp\;/g;
169 # boil HTML down to some interesting text
170 $h_text =~ s/^[1-3]>//;
171 $h_text =~ s/<\/?.*?>//sg;
172 $h_text =~ s/\s+/ /sg;
173 $h_text =~ s/^\s$//s;
174 $h_text =~ s/(&nbsp;)+\W*/&nbsp;/sg;
175
176 if ($h_text =~ m/\w+/)
177 {
178 if ($new_hnum > $hnum)
179 {
180 # increase section nesting
181 $hnum++;
182 while ($hnum < $new_hnum)
183 {
184 my $spacing = " " x $hnum;
185 $section_text .= "<!--\n";
186 $section_text .= $spacing."<Section>\n";
187 $section_text .= "-->\n";
188 $hnum++;
189 }
190 }
191 else # ($new_hnum <= $hnum)
192 {
193 # descrease section nesting
194 while ($hnum >= $new_hnum)
195 {
196 my $spacing = " " x $hnum;
197 $section_text .= "<!--\n";
198 $section_text .= $spacing."</Section>\n";
199 $section_text .= "-->\n";
200 $hnum--;
201 }
202 $hnum++;
203 }
204
205 my $spacing = " " x $hnum;
206 $section_text .= "<!--\n";
207 $section_text .= $spacing."<Section>\n";
208 $section_text .= $spacing." <Description>\n";
209 $section_text .= $spacing." <Metadata name=\"Title\">$h_text</Metadata>";
210 $section_text .= $spacing." </Description>\n";
211 $section_text .= "-->\n";
212
213 print $outhandle $spacing."$h_text\n"
214 if $self->{'verbosity'} > 2;
215
216 $sectionh1++ if ($hnum==1);
217 }
218 }
219 else {
220### print STDERR "***** hc = <h$hc\n\n";
221
222 }
223 $section_text .= "<h$hc";
224 }
225 else
226 {
227 $section_text .= "<h$hc";
228 }
229 }
230
231 while ($hnum >= 1)
232 {
233 my $spacing = " " x $hnum;
234 $section_text .= "<!--\n";
235 $section_text .= $spacing."</Section>\n";
236 $section_text .= "-->\n";
237 $hnum--;
238 }
239
240 $section_text .= "<!--\n</Section>\n-->\n";
241
242 $$textref = $section_text;
243
244 # should be textref not testref???
245 #$$testref =~ s/<h(\d+)>(.*?)<\/h$1>/<Section><Metadata name=\"Title\">$1<\/Metadata></Section><h$1><\/h$1>/gi;
246
247 if ($sectionh1>0)
248 {
249 print $outhandle " Located section headings ..."
250 if $self->{'verbosity'} > 1;
251 }
252 print $outhandle " Passing on the HTMLPlug\n"
253 if $self->{'verbosity'} > 1;
254
255 $$textref =~ s/<!\[if !vml\]>/<![if vml]>/g;
256
257 $$textref =~ s/(&nbsp;)+/&nbsp;/sg;
258
259 ## $$textref =~ s/<o:p>&nbsp;<\/o:p>//g; # used with VML to space figures?
260
261 $self->SUPER::process(@_);
262
263 # associate original file with doc object
264 my $cursection = $doc_obj->get_top_section();
265 my $filename = &util::filename_cat($base_dir, $file);
266 if (-e $filename)
267 {
268 print $outhandle " Adding associated Word document\n"
269 if $self->{'verbosity'} > 1;
270
271 $doc_obj->associate_file($filename, "doc.doc", undef, $cursection);
272
273 my $doclink = "<a href=_httpcollection_/index/assoc/[archivedir]/doc.doc>";
274 $doc_obj->add_utf8_metadata ($cursection, "srclink", $doclink);
275 $doc_obj->add_utf8_metadata ($cursection, "srcicon", "_icondoc_");
276 $doc_obj->add_utf8_metadata ($cursection, "/srclink", "</a>");
277
278 my $file_size = -s $filename;
279 if ($file_size>1024)
280 {
281 my $fs_kbytes = sprintf("%d",$file_size/1024);
282 $doc_obj->add_utf8_metadata ($cursection, "filesize", "$fs_kbytes Kb");
283 }
284 else
285 {
286 $doc_obj->add_utf8_metadata ($cursection, "filesize", "$file_size bytes");
287 }
288
289 if ($file_size > 200000)
290 {
291 $doc_obj->add_utf8_metadata ($cursection, "fswarning", "1");
292 }
293 }
294}
295
296
297sub resize_if_necessary
298{
299 my ($self,$front,$back,$base_dir,$href) = @_;
300
301 # dig out width and height of image, if there
302 my $img_attributes = "$front back";
303 my ($img_width) = ($img_attributes =~ m/\s+width=\"?(\d+)\"?/i);
304 my ($img_height) = ($img_attributes =~ m/\s+height=\"?(\d+)\"?/i);
305
306 # derive local filename for image based on its URL
307 my $img_filename = $href;
308 $img_filename =~ s/^[^:]*:\/\///;
309 $img_filename = &util::filename_cat($base_dir, $img_filename);
310
311 # Replace %20's in URL with a space if required. Note that the filename
312 # may include the %20 in some situations
313 if ($img_filename =~ /\%20/) {
314 if (!-e $img_filename) {
315 $img_filename =~ s/\%20/ /g;
316 }
317 }
318 if ((-e $img_filename) && (defined $img_width) && (defined $img_height)) {
319 # get image info on width and height
320
321 my $outhandle = $self->{'outhandle'};
322 my $verbosity = $self->{'verbosity'};
323
324 my ($image_type, $actual_width, $actual_height, $image_size)
325 = &ImagePlug::identify($img_filename, $outhandle, $verbosity);
326
327 #print STDERR "**** $actual_width x $actual_height";
328 #print STDERR " (requested: $img_width x $img_height)\n";
329
330 if (($img_width < $actual_width) || ($img_height < $actual_height)) {
331 print $outhandle "Resizing $img_filename\n" if ($verbosity > 0);
332
333 # derive new image name based on current image
334 my ($tailname, $dirname, $suffix)
335 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
336
337 my $resized_filename
338 = &util::filename_cat($dirname, $tailname."_resized".$suffix);
339
340 #print STDERR "**** suffix = $suffix\n";
341
342 # Generate smaller image with convert
343 my $newsize = "$img_widthx$image_height";
344 my $command = "convert -interlace plane -verbose "
345 ."-geometry $newsize \"img_$filename\" \"$resized_filename\"";
346 print $outhandle "ImageResize: $command\n" if ($verbosity > 2);
347 my $result = '';
348 print $outhandle "ImageResize result: $result\n" if ($verbosity > 2);
349 }
350 }
351 return $href;
352}
353
354sub replace_images {
355 my $self = shift (@_);
356 my ($front, $link, $back, $base_dir,
357 $file, $doc_obj, $section) = @_;
358 # remove quotes from link at start and end if necessary
359 if ($link=~/^\"/) {
360 $link=~s/^\"//;$link=~s/\"$//;
361 $front.='"';
362 $back="\"$back";
363 }
364
365 $link =~ s/\n/ /g;
366
367 my ($href, $hash_part, $rl) = $self->format_link ($link, $base_dir, $file);
368
369## $href = $self->resize_if_necessary($front,$back,$base_dir,$href);
370
371 my $middle = $self->add_file ($href, $rl, $hash_part, $base_dir, $doc_obj, $section);
372
373 return $front . $middle . $back;
374}
375
376sub extract_metadata
377{
378 my $self = shift (@_);
379 my ($textref, $metadata, $doc_obj) = @_;
380 my $outhandle = $self->{'outhandle'};
381
382 # metadata fields to extract/save. 'key' is the (lowercase) name of the
383 # html meta, 'value' is the metadata name for greenstone to use
384 my %find_fields = ();
385 my ($tag,$value);
386
387 my $orig_field = "";
388 foreach my $field (split /,/, $self->{'extracted_word_metadata_fields'}) {
389 # support tag<tagname>
390 if ($field =~ /^(.*?)<(.*?)>$/) {
391 # "$2" is the user's preferred gs metadata name
392 $find_fields{lc($1)}=$2; # lc = lowercase
393 $orig_field = $1;
394 } else { # no <tagname> for mapping
395 # "$field" is the user's preferred gs metadata name
396 $find_fields{lc($field)}=$field; # lc = lowercase
397 $orig_field = $field;
398 }
399 if ($textref =~ m/<o:$orig_field>(.*)<\/o:$orig_field>/i){
400 $tag = $orig_field;
401 $value = $1;
402 if (!defined $value || !defined $tag){
403 print $outhandle "StructuredHTMLPlug: can't find VALUE in \"$tag\"\n";
404 next;
405 } else {
406 # clean up and add
407 chomp($value); # remove trailing \n, if any
408 $tag = $find_fields{lc($tag)};
409 print $outhandle " extracted \"$tag\" metadata \"$value\"\n"
410 if ($self->{'verbosity'} > 2);
411 $doc_obj->add_utf8_metadata($doc_obj->get_top_section(), $tag, $value);
412 }
413 }
414 }
415}
416
4171;
Note: See TracBrowser for help on using the repository browser.