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

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

Modified tmp_area_convert_file routine to use pass new argument ensure_path_absolute to util::soft_link to make sure soft-linking works for GSDL remote case

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