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 |
|
---|
31 | package TEXTPlug;
|
---|
32 |
|
---|
33 | use BasPlug;
|
---|
34 |
|
---|
35 | use strict;
|
---|
36 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
37 |
|
---|
38 | sub BEGIN {
|
---|
39 | @TEXTPlug::ISA = ('BasPlug');
|
---|
40 | }
|
---|
41 |
|
---|
42 | my $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 |
|
---|
54 | my $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 |
|
---|
62 | sub 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 |
|
---|
75 | sub 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
|
---|
82 | sub 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/\[/[/g;
|
---|
105 | $title =~ s/\[/]/g;
|
---|
106 | $title =~ s/\</</g;
|
---|
107 | $title =~ s/\>/>/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 | # 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 |
|
---|
120 | sub text_to_html {
|
---|
121 | my $self = shift (@_);
|
---|
122 | my ($textref) = @_;
|
---|
123 |
|
---|
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'
|
---|
126 | $$textref =~ s/\\/\\\\/g; # macro language
|
---|
127 | $$textref =~ s/_/\\_/g; # macro language
|
---|
128 | $$textref =~ s/</</g;
|
---|
129 | $$textref =~ s/>/>/g;
|
---|
130 |
|
---|
131 | # insert preformat tags and add text to document object
|
---|
132 | $$textref = "<pre>\n$$textref\n</pre>";
|
---|
133 | }
|
---|
134 |
|
---|
135 |
|
---|
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
|
---|
140 | # a similar subroutine with the same name here.
|
---|
141 | sub 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 |
|
---|
148 | # derive output filename from input filename
|
---|
149 | my ($tailname, $dirname, $suffix)
|
---|
150 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
151 |
|
---|
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 |
|
---|
163 | # convert to utf-8 otherwise we have problems with the doc.xml file
|
---|
164 | # later on
|
---|
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
|
---|
167 |
|
---|
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 |
|
---|
172 | #my $output_filename = $tailname$output_ext;#output_ext has to be html!
|
---|
173 | my $output_filename = &util::filename_cat($tmp_dirname, $tailname.".html");
|
---|
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
|
---|
178 | unless(open(TEXT, "<$tmp_filename")) { # open it as a new file for writing
|
---|
179 | print STDERR "TEXTPlug.pm: Unable to open and read from $input_filename for converting to html...ERROR\n";
|
---|
180 | return ""; # no file name
|
---|
181 | }
|
---|
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);
|
---|
192 |
|
---|
193 | #print STDERR "****output_filename: $output_filename\n";
|
---|
194 | #print STDERR "****text: $text\n";
|
---|
195 |
|
---|
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";
|
---|
199 | return ""; # no filename
|
---|
200 | }
|
---|
201 | # write the html contents in text out to the file
|
---|
202 | print HTML $text;
|
---|
203 | close HTML;
|
---|
204 |
|
---|
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
|
---|
209 | }
|
---|
210 |
|
---|
211 |
|
---|
212 | 1;
|
---|