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

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

Now replaces a source textfile with its html by converting it to the same format of html that is output after a build

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 7.3 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
[15150]152 # softlink to collection tmp dir
153 my $tmp_dirname = $dirname;
154 if(defined $ENV{'GSDLCOLLECTDIR'}) {
155 $tmp_dirname = $ENV{'GSDLCOLLECTDIR'};
156 } elsif(defined $ENV{'GSDLHOME'}) {
157 $tmp_dirname = $ENV{'GSDLHOME'};
158 }
159 $tmp_dirname = &util::filename_cat($tmp_dirname, "tmp");
160 &util::mk_dir($tmp_dirname) if (!-e $tmp_dirname);
161
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
[585]167
[15150]168 my $tmp_filename = &util::filename_cat($tmp_dirname, "$tailname$suffix");
169 &util::soft_link($input_filename, $tmp_filename);
170 # softlink symbolic link to or copy of original, so we don't accidentally damage it
171
[15119]172 #my $output_filename = $tailname$output_ext;#output_ext has to be html!
[15150]173 my $output_filename = &util::filename_cat($tmp_dirname, $tailname.".html");
[15119]174
175 # read contents of text file line by line into an array
176 # create an HTML file from the text file
177 # Recreate the original file for writing the updated contents
[15150]178 unless(open(TEXT, "<$tmp_filename")) { # open it as a new file for writing
[15119]179 print STDERR "TEXTPlug.pm: Unable to open and read from $input_filename for converting to html...ERROR\n";
[15150]180 return ""; # no file name
[15119]181 }
[15150]182
183 my $text;
184 {
185 local $/ = undef; # Read entire file at once. This is from http://perl.plover.com/local.html
186 $text = <TEXT>; # Now file is read in as one single 'line'
187 }
188 close(TEXT); # close the file
189
190 # convert the text
191 $self->text_to_html(\$text);
[15119]192
[15150]193 #print STDERR "****output_filename: $output_filename\n";
194 #print STDERR "****text: $text\n";
[585]195
[15119]196 # try creating this new file writing and try opening it for writing, else exit with error value
197 unless(open(HTML, ">$output_filename")) { # open the new html file for writing
198 print STDERR "TEXTPlug.pm: Unable to create $output_filename for writing $tailname$suffix txt converted to html...ERROR\n";
[15150]199 return ""; # no filename
[15119]200 }
[15150]201 # write the html contents in text out to the file
202 print HTML $text;
203 close HTML;
[585]204
[15150]205 # remove the copy of the original file/remove the symbolic link to original file
206 &util::rm($tmp_filename);
207
208 return $output_filename; # return the output file path
[15119]209}
[585]210
[15150]211
[15119]2121;
Note: See TracBrowser for help on using the repository browser.