source: trunk/gsdl/perllib/plugins/GBBPlug.pm@ 721

Last change on this file since 721 was 678, checked in by sjboddie, 25 years ago

added bookcover

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.7 KB
Line 
1###########################################################################
2#
3# GBBPlug.pm -- plugin for processing gb encoded html books
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26# creates multi-level document from document containing
27# <<TOC>> level tags. Metadata for each section is taken from any
28# other tags on the same line as the <<TOC>>. e.g. <<Title>>xxxx<</Title>>
29# sets Title metadata.
30
31# Everything else between TOC tags is expected to be simple html
32# and is treated accordingly.
33
34# Imput files are expected to be GB encoded and have .gbb file extension
35
36# An jpeg image with the same filename as the .gbb file will be associated
37# as cover.jpg if it exists
38
39package GBBPlug;
40
41use BasPlug;
42use sorttools;
43use util;
44use unicode;
45use cnseg;
46use gb;
47
48
49sub BEGIN {
50 @ISA = ('BasPlug');
51}
52
53sub new {
54 my ($class) = @_;
55 $self = new BasPlug ();
56
57 return bless $self, $class;
58}
59
60sub is_recursive {
61 my $self = shift (@_);
62
63 return 0; # this is not a recursive plugin
64}
65
66
67# return number of files processed, undef if can't process
68# Note that $base_dir might be "" and that $file might
69# include directories
70sub read {
71 my $self = shift (@_);
72 my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;
73
74 my $filename = &util::filename_cat($base_dir, $file);
75 my $absdir = $filename;
76 $absdir =~ s/[^\/\\]*$//;
77
78 return undef unless ($filename =~ /\.gbb$/i && (-e $filename));
79
80 print STDERR "GBBPlug: processing $filename\n" if $processor->{'verbosity'};
81
82 # create a new document
83 my $doc_obj = new doc ($file, "indexed_doc");
84 open (FILE, $filename) || die "GBBPlug::read - ERROR: can't open $filename\n";
85
86 my $cursection = $doc_obj->get_top_section();
87
88 # copy the book cover if it exists
89 my ($bookcover) = $filename =~ /^(.*?)\.gbb$/i;
90 $doc_obj->associate_file("$bookcover.jpg", "cover.jpg", "image/jpeg")
91 if -e "$bookcover.jpg";
92
93 my $text = "";
94 my $line = "";
95 my $title = "";
96 while (defined ($line = <FILE>)) {
97 $text .= $line;
98 }
99
100 # convert to unicode
101 $text = &unicode::unicode2utf8(&gb::gb2unicode($text));
102 # segment the Chinese words
103 $text = &cnseg::segment($text);
104
105 # we'll use the worthless alarm thingy to temporarily replace
106 # '\n' so we'd better check it doesn't occur naturally
107 if ($text =~ /\a/) {
108 print STDERR "GBBPlug::read - 'WARNING '\a' character occurs in text!!\n";
109 }
110
111 # need to add <classifytype> to each book as we'll be using 'Hierarchy'
112 # for HB collections rather than the default ('Book')
113 $doc_obj->add_metadata ($cursection, 'classifytype', 'Hierarchy');
114
115 # remove line breaks
116 $text =~ s/\n/\a/g;
117
118 # remove any leading rubbish
119 $text =~ s/^.*?(<<TOC)/$1/i;
120
121 my $curtoclevel = 1;
122 my $firstsection = 1;
123 while (length ($text) > 0) {
124 $text =~ s/^<<TOC(\d+)>>([^\a]*)\a(.*?)(<<TOC|\Z)/$4/i;
125 my $toclevel = $1;
126 my $metadata = $2;
127 my $sectiontext = $3;
128
129 # close any sections below the current level and
130 # create a new section (special case for the firstsection)
131 while (($curtoclevel > $toclevel) ||
132 (!$firstsection && $curtoclevel == $toclevel)) {
133 $cursection = $doc_obj->get_parent_section ($cursection);
134 $curtoclevel--;
135 }
136 if ($curtoclevel+1 < $toclevel) {
137 print STDERR "WARNING - jump in toc levels in $htmlfile " .
138 "from $curtoclevel to $toclevel\n";
139 }
140 while ($curtoclevel < $toclevel) {
141 $curtoclevel++;
142 $cursection =
143 $doc_obj->insert_section($doc_obj->get_end_child($cursection));
144 }
145
146 # sort out metadata
147 while ($metadata =~ s/^.*?<<([^>]*)>>(.*?)<<[^>]*>>//) {
148 my $metakey = $1;
149 my $metavalue = $2;
150
151 if ($metavalue ne "" && $metakey ne "") {
152 # make sure key fits in with gsdl naming scheme
153 $metakey =~ tr/[A-Z]/[a-z]/;
154 $metakey = ucfirst ($metakey);
155 $doc_obj->add_utf8_metadata ($cursection, $metakey, $metavalue);
156 }
157 }
158
159
160 # remove header rubbish
161 $sectiontext =~ s/^.*?<body[^>]*>//i;
162
163 # and any other unwanted tags
164 $sectiontext =~ s/<(\/p|\/html|\/body)>//g;
165
166 # fix up the image links
167 $sectiontext =~ s/(<img[^>]*?src\s*=\s*\"?)([^\">]+)(\"?[^>]*>)/
168 &replace_image_links($absdir, $doc_obj, $1, $2, $3)/ige;
169
170 # put line breaks back in
171 $sectiontext =~ s/\a/\n/g;
172
173 # add the text
174 $doc_obj->add_utf8_text($cursection, $sectiontext);
175
176 $firstsection = 0;
177 }
178
179 # add OID
180 $doc_obj->set_OID ();
181
182 # process the document
183 $processor->process($doc_obj);
184
185 return 1; # processed the file
186}
187
188sub replace_image_links {
189
190 my ($dir, $doc_obj, $front, $link, $back) = @_;
191
192 my ($filename, $error);
193 my $foundimage = 0;
194
195 $link =~ s/\/\///;
196 my ($imagetype) = $link =~ /([^\.]*)$/;
197 $imagetype =~ tr/[A-Z]/[a-z]/;
198 if ($imagetype eq "jpg") {$imagetype = "jpeg";}
199 if ($imagetype !~ /^(jpg|gif|png)$/) {
200 print STDERR "GBHPlug: Warning - unknown image type ($imagetype)\n";
201 }
202 my ($imagefile) = $link =~ /([^\/]*)$/;
203 my ($imagepath) = $link =~ /^[^\/]*(.*)$/;
204
205 if (defined $imagepath && $imagepath =~ /\w/) {
206 # relative link
207 $filename = &util::filename_cat ($dir, $imagepath);
208 if (-e $filename) {
209 $doc_obj->associate_file ($filename, $imagefile, "image/$imagetype");
210 $foundimage = 1;
211 } else {
212 $error = "GBHPlug: Warning - couldn't find image file $imagefile in either $filename or";
213 }
214 }
215
216 if (!$foundimage) {
217 $filename = &util::filename_cat ($dir, $imagefile);
218 if (-e $filename) {
219 $doc_obj->associate_file ($filename, $imagefile, "image/$imagetype");
220 $foundimage = 1;
221 } elsif (defined $error) {
222 print STDERR "$error $filename\n";
223 } else {
224 print STDERR "GBHPlug: Warning - couldn't find image file $imagefile in $filename\n";
225 }
226 }
227
228 if ($foundimage) {
229 return "${front}_httpcollection_/archives/_thisOID_/${imagefile}${back}";
230 } else {
231 return "";
232 }
233}
234
2351;
236
237
238
239
240
241
242
243
244
245
246
Note: See TracBrowser for help on using the repository browser.