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

Last change on this file since 16854 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
RevLine 
[585]1###########################################################################
2#
[15872]3# TextPlugin.pm -- simple text plugin
[585]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
[1244]26# creates simple single-level document. Adds Title metadata
27# of first line of text (up to 100 characters long).
[585]28
[3540]29# 12/05/02 Added usage datastructure - John Thompson
30
[15872]31package TextPlugin;
[585]32
[15872]33use ReadTextFile;
[585]34
[10254]35use strict;
36no strict 'refs'; # allow filehandles to be variables and viceversa
[15872]37no strict 'subs';
[2450]38
[585]39sub BEGIN {
[15872]40 @TextPlugin::ISA = ('ReadTextFile');
[585]41}
42
[4744]43my $arguments =
44 [ { 'name' => "process_exp",
[15872]45 'desc' => "{BasePlugin.process_exp}",
[6408]46 'type' => "regexp",
[4744]47 'deft' => &get_default_process_exp(),
48 'reqd' => "no" } ,
49 { 'name' => "title_sub",
[15872]50 'desc' => "{TextPlugin.title_sub}",
[6408]51 'type' => "regexp",
[4744]52 'deft' => "",
53 'reqd' => "no" } ];
[3540]54
[15872]55my $options = { 'name' => "TextPlugin",
56 'desc' => "{TextPlugin.desc}",
[6408]57 'abstract' => "no",
[4744]58 'inherits' => "yes",
[15119]59 'srcreplaceable' => "yes", # Source docs in regular txt format can be replaced with GS-generated html
[4744]60 'args' => $arguments };
[3540]61
[2450]62
[585]63sub new {
[10218]64 my ($class) = shift (@_);
65 my ($pluginlist,$inputargs,$hashArgOptLists) = @_;
66 push(@$pluginlist, $class);
[3540]67
[15872]68 push(@{$hashArgOptLists->{"ArgList"}},@{$arguments});
69 push(@{$hashArgOptLists->{"OptList"}},$options);
[2450]70
[15872]71 my $self = new ReadTextFile($pluginlist, $inputargs, $hashArgOptLists);
[2450]72
[585]73 return bless $self, $class;
74}
75
[1244]76sub get_default_process_exp {
[585]77 my $self = shift (@_);
78
[1244]79 return q^(?i)\.te?xt$^;
[585]80}
81
[1244]82# do plugin specific processing of doc_obj
83sub process {
[585]84 my $self = shift (@_);
[6332]85 my ($textref, $pluginfo, $base_dir, $file, $metadata, $doc_obj, $gli) = @_;
[1424]86 my $outhandle = $self->{'outhandle'};
[1244]87
[585]88 my $cursection = $doc_obj->get_top_section();
[1244]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'}) {
[16301]93 my $title = $self->get_title_metadata($textref);
[1244]94 $doc_obj->add_utf8_metadata ($cursection, "Title", $title);
[585]95 }
[8121]96 # Add FileFormat metadata
[15872]97 $doc_obj->add_metadata($cursection, "FileFormat", "Text");
[15150]98
99 # insert preformat tags and add text to document object
100 $self->text_to_html($textref); # modifies the text
[16301]101 $doc_obj->add_utf8_text($cursection, $$textref);
[15150]102
103 return 1;
104}
105
[16301]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
[15150]127sub text_to_html {
128 my $self = shift (@_);
129 my ($textref) = @_;
[8121]130
[2085]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'
[3932]133 $$textref =~ s/\\/\\\\/g; # macro language
134 $$textref =~ s/_/\\_/g; # macro language
[2667]135 $$textref =~ s/</&lt;/g;
136 $$textref =~ s/>/&gt;/g;
[15150]137
[1244]138 # insert preformat tags and add text to document object
[15150]139 $$textref = "<pre>\n$$textref\n</pre>";
[585]140}
141
[15150]142
[15119]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
[15872]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
[15150]147# a similar subroutine with the same name here.
[15119]148sub tmp_area_convert_file {
149 my $self = shift (@_);
150 my ($output_ext, $input_filename) = @_;
151
[15150]152 my $outhandle = $self->{'outhandle'};
[15119]153 #my $failhandle = $self->{'failhandle'};
154
155 # derive output filename from input filename
156 my ($tailname, $dirname, $suffix)
157 = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
[585]158
[15167]159 # Softlink to collection tmp dir
160 # First find a temporary directory to create the output file in
[15150]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
[15119]170 # convert to utf-8 otherwise we have problems with the doc.xml file
171 # later on
[16580]172 $tailname = $self->SUPER::filepath_to_utf8($tailname) unless &unicode::check_is_utf8($tailname);
173
[15167]174 $suffix = lc($suffix);
[15150]175 my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
[15167]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
[15150]182
[15167]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!
[15150]195 my $output_filename = &util::filename_cat($tmp_dirname, $tailname.".html");
[15119]196
[15167]197 # Read contents of text file line by line into an array
[15119]198 # create an HTML file from the text file
199 # Recreate the original file for writing the updated contents
[15150]200 unless(open(TEXT, "<$tmp_filename")) { # open it as a new file for writing
[15872]201 print STDERR "TextPlugin.pm: Unable to open and read from $tmp_filename for converting to html...ERROR: $!\n";
[15150]202 return ""; # no file name
[15119]203 }
[15150]204
[15167]205 # Read the entire file at once
[15150]206 my $text;
207 {
[15167]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'
[15150]210 }
211 close(TEXT); # close the file
[16301]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
[15150]217 $self->text_to_html(\$text);
[16301]218
[15119]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
[15872]221 print STDERR "TextPlugin.pm: Unable to create $output_filename for writing $tailname$suffix txt converted to html...ERROR: $!\n";
[15150]222 return ""; # no filename
[15119]223 }
[16301]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>";
[15150]228 close HTML;
[585]229
[15150]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
[15119]234}
[585]235
[15150]236
[15119]2371;
Note: See TracBrowser for help on using the repository browser.