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

Last change on this file since 809 was 733, checked in by sjboddie, 25 years ago

just minor changes to book cover image stuff

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.5 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# a file with the same name as the gbb file but a .jpg extension is
37# taken as the cover image
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 0 if ($filename =~ /\.jpg$/i);
79 return undef unless ($filename =~ /\.gbb$/i && (-e $filename));
80
81 print STDERR "GBBPlug: processing $filename\n" if $processor->{'verbosity'};
82
83 # create a new document
84 my $doc_obj = new doc ($file, "indexed_doc");
85 open (FILE, $filename) || die "GBBPlug::read - ERROR: can't open $filename\n";
86
87 my $cursection = $doc_obj->get_top_section();
88
89 # add the cover image
90 my $coverimage = $filename;
91 $coverimage =~ s/\.gbb/\.jpg/i;
92 $doc_obj->associate_file($coverimage, "cover.jpg", "image/jpeg");
93
94 my $text = "";
95 my $line = "";
96 my $title = "";
97 while (defined ($line = <FILE>)) {
98 $text .= $line;
99 }
100
101 # convert to unicode
102 $text = &unicode::unicode2utf8(&gb::gb2unicode($text));
103 # segment the Chinese words
104 $text = &cnseg::segment($text);
105
106 # we'll use the worthless alarm thingy to temporarily replace
107 # '\n' so we'd better check it doesn't occur naturally
108 if ($text =~ /\a/) {
109 print STDERR "GBBPlug::read - 'WARNING '\a' character occurs in text!!\n";
110 }
111
112 # remove line breaks
113 $text =~ s/\n/\a/g;
114
115 # remove any leading rubbish
116 $text =~ s/^.*?(<<TOC)/$1/i;
117
118 my $curtoclevel = 1;
119 my $firstsection = 1;
120 my $toccount = 0;
121 while (length ($text) > 0) {
122 $text =~ s/^<<TOC(\d+)>>([^\a]*)\a(.*?)(<<TOC|\Z)/$4/i;
123 my $toclevel = $1;
124 my $metadata = $2;
125 my $sectiontext = $3;
126
127 if ($toclevel == 2) {
128 $toccount ++;
129 }
130
131
132 # close any sections below the current level and
133 # create a new section (special case for the firstsection)
134 while (($curtoclevel > $toclevel) ||
135 (!$firstsection && $curtoclevel == $toclevel)) {
136 $cursection = $doc_obj->get_parent_section ($cursection);
137 $curtoclevel--;
138 }
139 if ($curtoclevel+1 < $toclevel) {
140 print STDERR "WARNING - jump in toc levels in $htmlfile " .
141 "from $curtoclevel to $toclevel\n";
142 }
143 while ($curtoclevel < $toclevel) {
144 $curtoclevel++;
145 $cursection =
146 $doc_obj->insert_section($doc_obj->get_end_child($cursection));
147 }
148
149 # sort out metadata
150 while ($metadata =~ s/^.*?<<([^>]*)>>(.*?)<<[^>]*>>//) {
151 my $metakey = $1;
152 my $metavalue = $2;
153
154 if ($metavalue ne "" && $metakey ne "") {
155 # make sure key fits in with gsdl naming scheme
156 $metakey =~ tr/[A-Z]/[a-z]/;
157 $metakey = ucfirst ($metakey);
158 $doc_obj->add_utf8_metadata ($cursection, $metakey, $metavalue);
159 }
160 }
161
162
163 # remove header rubbish
164 $sectiontext =~ s/^.*?<body[^>]*>//i;
165
166 # and any other unwanted tags
167 $sectiontext =~ s/<(\/p|\/html|\/body)>//ig;
168
169 # fix up the image links
170 $sectiontext =~ s/(<img[^>]*?src\s*=\s*\"?)([^\">]+)(\"?[^>]*>)/
171 &replace_image_links($absdir, $doc_obj, $1, $2, $3)/ige;
172
173 # put line breaks back in
174 $sectiontext =~ s/\a/\n/g;
175
176 # add the text
177 $doc_obj->add_utf8_text($cursection, $sectiontext);
178
179 $firstsection = 0;
180 }
181
182 # add OID
183 $doc_obj->set_OID ();
184
185 # process the document
186 $processor->process($doc_obj);
187
188 return 1; # processed the file
189}
190
191sub replace_image_links {
192
193 my ($dir, $doc_obj, $front, $link, $back) = @_;
194
195 my ($filename, $error);
196 my $foundimage = 0;
197
198 $link =~ s/\/\///;
199 my ($imagetype) = $link =~ /([^\.]*)$/;
200 $imagetype =~ tr/[A-Z]/[a-z]/;
201 if ($imagetype eq "jpg") {$imagetype = "jpeg";}
202 if ($imagetype !~ /^(jpeg|gif|png)$/) {
203 print STDERR "GBHPlug: Warning - unknown image type ($imagetype)\n";
204 }
205 my ($imagefile) = $link =~ /([^\/]*)$/;
206 my ($imagepath) = $link =~ /^[^\/]*(.*)$/;
207
208 if (defined $imagepath && $imagepath =~ /\w/) {
209 # relative link
210 $filename = &util::filename_cat ($dir, $imagepath);
211 if (-e $filename) {
212 $doc_obj->associate_file ($filename, $imagefile, "image/$imagetype");
213 $foundimage = 1;
214 } else {
215 $error = "GBHPlug: Warning - couldn't find image file $imagefile in either $filename or";
216 }
217 }
218
219 if (!$foundimage) {
220 $filename = &util::filename_cat ($dir, $imagefile);
221 if (-e $filename) {
222 $doc_obj->associate_file ($filename, $imagefile, "image/$imagetype");
223 $foundimage = 1;
224 } elsif (defined $error) {
225 print STDERR "$error $filename\n";
226 } else {
227 print STDERR "GBHPlug: Warning - couldn't find image file $imagefile in $filename\n";
228 }
229 }
230
231 if ($foundimage) {
232 return "${front}_httpcollection_/archives/_thisOID_/${imagefile}${back}";
233 } else {
234 return "";
235 }
236}
237
2381;
239
240
241
242
243
244
245
246
247
248
249
Note: See TracBrowser for help on using the repository browser.