source: gsdl/trunk/perllib/plugins/TextPlugin.pm@ 16580

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

Shared subroutine tmp_area_convert_file now ensures that the tailname of the file is utf8 by calling superclass BasePlugin's filepath_to_utf8 on it.

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1###########################################################################
2#
3# TextPlugin.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 TextPlugin;
32
33use ReadTextFile;
34
35use strict;
36no strict 'refs'; # allow filehandles to be variables and viceversa
37no strict 'subs';
38
39sub BEGIN {
40 @TextPlugin::ISA = ('ReadTextFile');
41}
42
43my $arguments =
44 [ { 'name' => "process_exp",
45 'desc' => "{BasePlugin.process_exp}",
46 'type' => "regexp",
47 'deft' => &get_default_process_exp(),
48 'reqd' => "no" } ,
49 { 'name' => "title_sub",
50 'desc' => "{TextPlugin.title_sub}",
51 'type' => "regexp",
52 'deft' => "",
53 'reqd' => "no" } ];
54
55my $options = { 'name' => "TextPlugin",
56 'desc' => "{TextPlugin.desc}",
57 'abstract' => "no",
58 'inherits' => "yes",
59 'srcreplaceable' => "yes", # Source docs in regular txt format can be replaced with GS-generated html
60 'args' => $arguments };
61
62
63sub new {
64 my ($class) = shift (@_);
65 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
66 push(@$pluginlist, $class);
67
68 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
69 push(@{$hashArgOptLists->{"OptList"}},$options);
70
71 my $self = new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists);
72
73 return bless $self, $class;
74}
75
76sub get_default_process_exp {
77 my $self = shift (@_);
78
79 return q^(?i)\.te?xt$^;
80}
81
82# do plugin specific processing of doc_obj
83sub process {
84 my $self = shift (@_);
85 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
86 my $outhandle = $self->{'outhandle'};
87
88 my $cursection = $doc_obj->get_top_section();
89 # get title metadata
90 # (don't need to get title if it has been passed
91 # in from another plugin)
92 if (!defined $metadata->{'Title'}) {
93 my $title = $self->get_title_metadata($textref);
94 $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
95 }
96 # Add FileFormat metadata
97 $doc_obj->add_metadata($cursection, "FileFormat", "Text");
98
99 # insert preformat tags and add text to document object
100 $self->text_to_html($textref); # modifies the text
101 $doc_obj->add_utf8_text($cursection, $$textref);
102
103 return 1;
104}
105
106sub get_title_metadata {
107 my $self = shift (@_);
108 my ($textref) = @_;
109
110 my ($title) = $$textref;
111 $title =~ /^\s+/s;
112 if (defined $self->{'title_sub'} && $self->{'title_sub'}) {
113 $title =~ s/$self->{'title_sub'}//;
114 }
115 $title =~ /^\s*([^\n]*)/s; $title=$1;
116 if (length($title) > 100) {
117 $title = substr ($title, 0, 100) . "...";
118 }
119 $title =~ s/\[/&\#91;/g;
120 $title =~ s/\[/&\#93;/g;
121 $title =~ s/\</&\#60;/g;
122 $title =~ s/\>/&\#62;/g;
123
124 return $title;
125}
126
127sub text_to_html {
128 my $self = shift (@_);
129 my ($textref) = @_;
130
131 # we need to escape the escape character, or else mg will convert into
132 # eg literal newlines, instead of leaving the text as '\n'
133 $$textref =~ s/\\/\\\\/g; # macro language
134 $$textref =~ s/_/\\_/g; # macro language
135 $$textref =~ s/</&lt;/g;
136 $$textref =~ s/>/&gt;/g;
137
138 # insert preformat tags and add text to document object
139 $$textref = "<pre>\n$$textref\n</pre>";
140}
141
142
143# replace_srcdoc_with_html.pl requires all subroutines that support src_replaceable
144# to contain a method called tmp_area_convert_file - this is indeed the case with all
145# Perl modules that are subclasses of ConvertToPlug.pm, but as we want TextPlugin to also
146# be srcreplaceable and because TextPlugin does not inherit from ConvertToPlug.pm, we have
147# a similar subroutine with the same name here.
148sub tmp_area_convert_file {
149 my $self = shift (@_);
150 my ($output_ext, $input_filename) = @_;
151
152 my $outhandle = $self->{'outhandle'};
153 #my $failhandle = $self->{'failhandle'};
154
155 # derive output filename from input filename
156 my ($tailname, $dirname, $suffix)
157 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
158
159 # Softlink to collection tmp dir
160 # First find a temporary directory to create the output file in
161 my $tmp_dirname = $dirname;
162 if(defined $ENV{'GSDLCOLLECTDIR'}) {
163 $tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
164 } elsif(defined $ENV{'GSDLHOME'}) {
165 $tmp_dirname = $ENV{'GSDLHOME'};
166 }
167 $tmp_dirname = &util::filename_cat($tmp_dirname, "tmp");
168 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
169
170 # convert to utf-8 otherwise we have problems with the doc.xml file
171 # later on
172 $tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
173
174 $suffix = lc($suffix);
175 my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
176
177 # Make sure we have the absolute path to the input file
178 # (If gsdl is remote, we're given relative path to input file, of the form import/tailname.suffix
179 # But we can't softlink to relative paths. Therefore, we need to ensure that
180 # the input_filename is the absolute path.
181 my $ensure_path_absolute = 1; # true
182
183 # Now make the softlink, so we don't accidentally damage the input file
184 # softlinking creates a symbolic link to (or, if that's not possible, it makes a copy of) the original
185 &util::soft_link($input_filename, $tmp_filename, $ensure_path_absolute);
186
187 my $verbosity = $self->{'verbosity'};
188 if ($verbosity > 0) {
189 # need this output statement, as GShell.java's runRemote() sets status to CANCELLED
190 # if there is no output! (Therefore, it only had this adverse affect when running GSDL remotely)
191 print $outhandle "Converting $tailname$suffix to html\n";
192 }
193
194 #my $output_filename = $tailname$output_ext; #output_ext has to be html!
195 my $output_filename = &util::filename_cat($tmp_dirname, $tailname.".html");
196
197 # Read contents of text file line by line into an array
198 # create an HTML file from the text file
199 # Recreate the original file for writing the updated contents
200 unless(open(TEXT, "<$tmp_filename")) { # open it as a new file for writing
201 print STDERR "TextPlugin.pm: Unable to open and read from $tmp_filename for converting to html...ERROR: $!\n";
202 return ""; # no file name
203 }
204
205 # Read the entire file at once
206 my $text;
207 {
208 local $/ = undef; # Now can read the entire file at once. From http://perl.plover.com/local.html
209 $text = <TEXT>; # File is read in as one single 'line'
210 }
211 close(TEXT); # close the file
212
213 # Get the title before embedding the text in pre tags
214 my $title = $self->get_title_metadata(\$text);
215
216 # Now convert the text
217 $self->text_to_html(\$text);
218
219 # try creating this new file writing and try opening it for writing, else exit with error value
220 unless(open(HTML, ">$output_filename")) { # open the new html file for writing
221 print STDERR "TextPlugin.pm: Unable to create $output_filename for writing $tailname$suffix txt converted to html...ERROR: $!\n";
222 return ""; # no filename
223 }
224 # write the html contents of the text (which is embedded in <pre> tags) out to the file with proper enclosing tags
225 print HTML "<html>\n<head>\n<title>$title</title>\n</head>\n<body>\n";
226 print HTML $text;
227 print HTML "\n</body>\n</html>";
228 close HTML;
229
230 # remove the copy of the original file/remove the symbolic link to original file
231 &util::rm($tmp_filename);
232
233 return $output_filename; # return the output file path
234}
235
236
2371;
Note: See TracBrowser for help on using the repository browser.