source: gsdl/trunk/perllib/plugins/TEXTPlug.pm@ 15119

Last change on this file since 15119 was 15119, checked in by ak19, 16 years ago

added srcreplaceable option and the subroutine tmp_area_convert_file to work with new script replace_srcdoc_with_html.pl. Subroutine had to be called this even though the html file is not generated in any tmp area, because ConvertToPlug has a method of this name

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.4 KB
Line 
1###########################################################################
2#
3# TEXTPlug.pm -- simple text plugin
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 simple single-level document. Adds Title metadata
27# of first line of text (up to 100 characters long).
28
29# 12/05/02 Added usage datastructure - John Thompson
30
31package TEXTPlug;
32
33use BasPlug;
34
35use strict;
36no strict 'refs'; # allow filehandles to be variables and viceversa
37
38sub BEGIN {
39 @TEXTPlug::ISA = ('BasPlug');
40}
41
42my $arguments =
43 [ { 'name' => "process_exp",
44 'desc' => "{BasPlug.process_exp}",
45 'type' => "regexp",
46 'deft' => &get_default_process_exp(),
47 'reqd' => "no" } ,
48 { 'name' => "title_sub",
49 'desc' => "{TEXTPlug.title_sub}",
50 'type' => "regexp",
51 'deft' => "",
52 'reqd' => "no" } ];
53
54my $options = { 'name' => "TEXTPlug",
55 'desc' => "{TEXTPlug.desc}",
56 'abstract' => "no",
57 'inherits' => "yes",
58 'srcreplaceable' => "yes", # Source docs in regular txt format can be replaced with GS-generated html
59 'args' => $arguments };
60
61
62sub new {
63 my ($class) = shift (@_);
64 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
65 push(@$pluginlist, $class);
66
67 if(defined $arguments){ push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});}
68 if(defined $options) { push(@{$hashArgOptLists->{"OptList"}},$options)};
69
70 my $self = new BasPlug($pluginlist, $inputargs, $hashArgOptLists);
71
72 return bless $self, $class;
73}
74
75sub get_default_process_exp {
76 my $self = shift (@_);
77
78 return q^(?i)\.te?xt$^;
79}
80
81# do plugin specific processing of doc_obj
82sub process {
83 my $self = shift (@_);
84 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
85 my $outhandle = $self->{'outhandle'};
86
87 print STDERR "<Processing n='$file' p='TEXTPlug'>\n" if ($gli);
88 print $outhandle "TEXTPlug: processing $file\n"
89 if $self->{'verbosity'} > 1;
90
91 my $cursection = $doc_obj->get_top_section();
92 # get title metadata
93 # (don't need to get title if it has been passed
94 # in from another plugin)
95 if (!defined $metadata->{'Title'}) {
96 my ($title) = $$textref;
97 $title =~ /^\s+/s;
98 if (defined $self->{'title_sub'} &&
99 $self->{'title_sub'}) {$title =~ s/$self->{'title_sub'}//;}
100 $title =~ /^\s*([^\n]*)/s; $title=$1;
101 if (length($title) > 100) {
102 $title = substr ($title, 0, 100) . "...";
103 }
104 $title =~ s/\[/&#91;/g;
105 $title =~ s/\[/&#93;/g;
106 $title =~ s/\</&#60;/g;
107 $title =~ s/\>/&#62;/g;
108 $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
109 }
110 # Add FileFormat metadata
111 $doc_obj->add_metadata($cursection, "FileFormat", "TEXT");
112
113
114 # we need to escape the escape character, or else mg will convert into
115 # eg literal newlines, instead of leaving the text as '\n'
116 $$textref =~ s/\\/\\\\/g; # macro language
117 $$textref =~ s/_/\\_/g; # macro language
118 $$textref =~ s/</&lt;/g;
119 $$textref =~ s/>/&gt;/g;
120
121 # insert preformat tags and add text to document object
122 $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
123
124 return 1;
125}
126
127# replace_srcdoc_with_html.pl requires all subroutines that support src_replaceable
128# to contain a method called tmp_area_convert_file - this is indeed the case with all
129# Perl modules that are subclasses of ConvertToPlug.pm, but as we want TEXTPlug to also
130# be srcreplaceable and because TEXTPlug does not inherit from ConvertToPlug.pm, we have
131# this ugly solution: same subroutine name.
132# Despite the subroutine name, this method does not in fact create the output html file in
133# some tmp folder. Instead, it creates the html file in the same folder as the input_filename
134# and writes the contents as html paragraphs nested inside <html><head></head><body></body></html>
135# It also sets the encoding of the html document created to UTF-8 in the head's meta tag.
136# Note: doesn't seem to be able to cope with <br /> and <meta ... /> -> slashes are a problem.
137# As a consequence, we resorted to making it not proper xhtml but just regular html.\
138# The output file's name will be utf8 AND might not be the same as the input file's name
139# (for instance, the output filename may have a number appended to it if there is already an html
140# file in the input folder with the same name).
141sub tmp_area_convert_file {
142 my $self = shift (@_);
143 my ($output_ext, $input_filename) = @_;
144
145 #my $outhandle = $self->{'outhandle'};
146 #my $failhandle = $self->{'failhandle'};
147 #my $convert_to_ext = $self->{'convert_to_ext'};
148
149 # derive output filename from input filename
150 my ($tailname, $dirname, $suffix)
151 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
152
153 # convert to utf-8 otherwise we have problems with the doc.xml file
154 # later on
155 &unicode::ensure_utf8(\$tailname); # TO DO: does this change the filename or not?
156
157 #my $output_filename = $tailname$output_ext;#output_ext has to be html!
158 my $output_filename = &util::filename_cat($dirname, $tailname.".html");
159
160 # read contents of text file line by line into an array
161 # create an HTML file from the text file
162 # Recreate the original file for writing the updated contents
163 unless(open(TEXT, "<$input_filename")) { # open it as a new file for writing
164 print STDERR "TEXTPlug.pm: Unable to open and read from $input_filename for converting to html...ERROR\n";
165 return 0;
166 }
167
168 my @lines = ();
169 my $line;
170 my $newpara = 1; # true whenever we're going to start a new para
171
172 while ($line=<TEXT>) {
173 # replace < and > with their character encodings
174 $line =~ s/</&lt;/g;
175 $line =~ s/>/.*&gt;/g;
176
177 if ($line =~ /^\s*$/) { # line is empty
178 if(!$newpara) {
179 push(@lines, "</p>".$line); # end of a paragraph, leave empty line in there
180 $newpara = 1;
181 } # If it is a new paragraph, we do nothing
182 } else { # a line with text
183 if($newpara) {
184 push(@lines, "<p>".$line); # start a new paragraph
185 $newpara = 0;
186 } else { # text-line is not a new paragraph, but just a new line
187 push(@lines, "<br>\n".$line); # put a break. It doesn't seem to accept <br />
188 }
189 }
190 }
191 close TEXT;
192
193 # we've come to the last line of input file, make sure that the text ends on </p>
194 # get the last line and check it has a </p> at the end:
195 $line = pop(@lines);
196 my $endpara = "</p>";
197 unless($line =~ m/$endpara$/ ) { # if no </p> at end of last para, then append it
198 $line = $line.$endpara;
199 }
200 push(@lines, $line); # either way, put the last line back
201
202 # write everything into the html file along with html start and end parts:
203 my $count = "1";
204 # create a sensible html file, don't overwrite pre-existing files of the same name
205 # just concatenate a number instead
206 while(-e "$output_filename") {
207 $output_filename = &util::filename_cat($dirname, $tailname.$count.".html");
208 $count++;
209 }
210 # try creating this new file writing and try opening it for writing, else exit with error value
211 unless(open(HTML, ">$output_filename")) { # open the new html file for writing
212 print STDERR "TEXTPlug.pm: Unable to create $output_filename for writing $tailname$suffix txt converted to html...ERROR\n";
213 return 0;
214 }
215
216 # append html start and end sections to the txt contents of the input file
217 print HTML "<html>\n<head>\n<title>$tailname</title>\n";
218 # we'll set the content to UTF-8 encoding
219 print HTML "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"; # /> not accepted
220 print HTML "</head>\n<body>\n";
221 # go through each line and write it to the file:
222 foreach $line (@lines) {
223 print HTML $line;
224 }
225 print HTML "\n</body></html>\n";
226 close HTML;
227
228 return $output_filename;
229}
230
2311;
Note: See TracBrowser for help on using the repository browser.