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

Last change on this file since 620 was 616, checked in by sjboddie, 25 years ago

some new gb plugins

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 6.4 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
36package GBBPlug;
37
38use BasPlug;
39use sorttools;
40use util;
41use unicode;
42use cnseg;
43use gb;
44
45
46sub BEGIN {
47 @ISA = ('BasPlug');
48}
49
50sub new {
51 my ($class) = @_;
52 $self = new BasPlug ();
53
54 return bless $self, $class;
55}
56
57sub is_recursive {
58 my $self = shift (@_);
59
60 return 0; # this is not a recursive plugin
61}
62
63
64# return number of files processed, undef if can't process
65# Note that $base_dir might be "" and that $file might
66# include directories
67sub read {
68 my $self = shift (@_);
69 my ($pluginfo, $base_dir, $file, $metadata, $processor) = @_;
70
71 my $filename = &util::filename_cat($base_dir, $file);
72 my $absdir = $filename;
73 $absdir =~ s/[^\/\\]*$//;
74
75 return undef unless ($filename =~ /\.gbb$/i && (-e $filename));
76
77 print STDERR "GBBPlug: processing $filename\n" if $processor->{'verbosity'};
78
79 # create a new document
80 my $doc_obj = new doc ($file, "indexed_doc");
81 open (FILE, $filename) || die "GBBPlug::read - ERROR: can't open $filename\n";
82
83 my $cursection = $doc_obj->get_top_section();
84
85 my $text = "";
86 my $line = "";
87 my $title = "";
88 while (defined ($line = <FILE>)) {
89 $text .= $line;
90 }
91
92 # convert to unicode
93 $text = &unicode::unicode2utf8(&gb::gb2unicode($text));
94 # segment the Chinese words
95 $text = &cnseg::segment($text);
96
97 # we'll use the worthless alarm thingy to temporarily replace
98 # '\n' so we'd better check it doesn't occur naturally
99 if ($text =~ /\a/) {
100 print STDERR "GBBPlug::read - 'WARNING '\a' character occurs in text!!\n";
101 }
102
103 # need to add <classifytype> to each book as we'll be using 'Hierarchy'
104 # for HB collections rather than the default ('Book')
105 $doc_obj->add_metadata ($cursection, 'classifytype', 'Hierarchy');
106
107 # remove line breaks
108 $text =~ s/\n/\a/g;
109
110 # remove any leading rubbish
111 $text =~ s/^.*?(<<TOC)/$1/i;
112
113 my $curtoclevel = 1;
114 my $firstsection = 1;
115 while (length ($text) > 0) {
116 $text =~ s/^<<TOC(\d+)>>([^\a]*)\a(.*?)(<<TOC|\Z)/$4/i;
117 my $toclevel = $1;
118 my $metadata = $2;
119 my $sectiontext = $3;
120
121 # close any sections below the current level and
122 # create a new section (special case for the firstsection)
123 while (($curtoclevel > $toclevel) ||
124 (!$firstsection && $curtoclevel == $toclevel)) {
125 $cursection = $doc_obj->get_parent_section ($cursection);
126 $curtoclevel--;
127 }
128 if ($curtoclevel+1 < $toclevel) {
129 print STDERR "WARNING - jump in toc levels in $htmlfile " .
130 "from $curtoclevel to $toclevel\n";
131 }
132 while ($curtoclevel < $toclevel) {
133 $curtoclevel++;
134 $cursection =
135 $doc_obj->insert_section($doc_obj->get_end_child($cursection));
136 }
137
138 # sort out metadata
139 while ($metadata =~ s/^.*?<<([^>]*)>>(.*?)<<[^>]*>>//) {
140 my $metakey = $1;
141 my $metavalue = $2;
142
143 if ($metavalue ne "" && $metakey ne "") {
144 # make sure key fits in with gsdl naming scheme
145 $metakey =~ tr/[A-Z]/[a-z]/;
146 $metakey = ucfirst ($metakey);
147 $doc_obj->add_utf8_metadata ($cursection, $metakey, $metavalue);
148 }
149 }
150
151
152 # remove header rubbish
153 $sectiontext =~ s/^.*?<body[^>]*>//i;
154
155 # and any other unwanted tags
156 $sectiontext =~ s/<(\/p|\/html|\/body)>//g;
157
158 # fix up the image links
159 $sectiontext =~ s/(<img[^>]*?src\s*=\s*\"?)([^\">]+)(\"?[^>]*>)/
160 &replace_image_links($absdir, $doc_obj, $1, $2, $3)/ige;
161
162 # put line breaks back in
163 $sectiontext =~ s/\a/\n/g;
164
165 # add the text
166 $doc_obj->add_utf8_text($cursection, $sectiontext);
167
168 $firstsection = 0;
169 }
170
171 # add OID
172 $doc_obj->set_OID ();
173
174 # process the document
175 $processor->process($doc_obj);
176
177 return 1; # processed the file
178}
179
180sub replace_image_links {
181
182 my ($dir, $doc_obj, $front, $link, $back) = @_;
183
184 my ($filename, $error);
185 my $foundimage = 0;
186
187 $link =~ s/\/\///;
188 my ($imagetype) = $link =~ /([^\.]*)$/;
189 $imagetype =~ tr/[A-Z]/[a-z]/;
190 if ($imagetype eq "jpg") {$imagetype = "jpeg";}
191 if ($imagetype !~ /^(jpg|gif|png)$/) {
192 print STDERR "GBHPlug: Warning - unknown image type ($imagetype)\n";
193 }
194 my ($imagefile) = $link =~ /([^\/]*)$/;
195 my ($imagepath) = $link =~ /^[^\/]*(.*)$/;
196
197 if (defined $imagepath && $imagepath =~ /\w/) {
198 # relative link
199 $filename = &util::filename_cat ($dir, $imagepath);
200 if (-e $filename) {
201 $doc_obj->associate_file ($filename, $imagefile, "image/$imagetype");
202 $foundimage = 1;
203 } else {
204 $error = "GBHPlug: Warning - couldn't find image file $imagefile in either $filename or";
205 }
206 }
207
208 if (!$foundimage) {
209 $filename = &util::filename_cat ($dir, $imagefile);
210 if (-e $filename) {
211 $doc_obj->associate_file ($filename, $imagefile, "image/$imagetype");
212 $foundimage = 1;
213 } elsif (defined $error) {
214 print STDERR "$error $filename\n";
215 } else {
216 print STDERR "GBHPlug: Warning - couldn't find image file $imagefile in $filename\n";
217 }
218 }
219
220 if ($foundimage) {
221 return "${front}_httpcollection_/archives/_thisOID_/${imagefile}${back}";
222 } else {
223 return "";
224 }
225}
226
2271;
228
229
230
231
232
233
234
235
236
237
238
Note: See TracBrowser for help on using the repository browser.