[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] | 31 | package TEXTPlug;
|
---|
| 32 |
|
---|
[1435] | 33 | use BasPlug;
|
---|
[585] | 34 |
|
---|
[10254] | 35 | use strict;
|
---|
| 36 | no strict 'refs'; # allow filehandles to be variables and viceversa
|
---|
[2450] | 37 |
|
---|
[585] | 38 | sub BEGIN {
|
---|
[10254] | 39 | @TEXTPlug::ISA = ('BasPlug');
|
---|
[585] | 40 | }
|
---|
| 41 |
|
---|
[4744] | 42 | my $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 |
|
---|
| 54 | my $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] | 62 | sub 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] | 75 | sub 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
|
---|
| 82 | sub 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/\[/[/g;
|
---|
| 105 | $title =~ s/\[/]/g;
|
---|
| 106 | $title =~ s/\</</g;
|
---|
| 107 | $title =~ s/\>/>/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");
|
---|
| 112 |
|
---|
[2085] | 113 |
|
---|
| 114 | # we need to escape the escape character, or else mg will convert into
|
---|
| 115 | # eg literal newlines, instead of leaving the text as '\n'
|
---|
[3932] | 116 | $$textref =~ s/\\/\\\\/g; # macro language
|
---|
| 117 | $$textref =~ s/_/\\_/g; # macro language
|
---|
[2667] | 118 | $$textref =~ s/</</g;
|
---|
| 119 | $$textref =~ s/>/>/g;
|
---|
[2085] | 120 |
|
---|
[1244] | 121 | # insert preformat tags and add text to document object
|
---|
| 122 | $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
|
---|
[585] | 123 |
|
---|
[1244] | 124 | return 1;
|
---|
[585] | 125 | }
|
---|
| 126 |
|
---|
[15119] | 127 | # replace_srcdoc_with_html.pl requires all subroutines that support src_replaceable
|
---|
| 128 | # to contain a method called tmp_area_convert_file - this is indeed the case with all
|
---|
| 129 | # Perl modules that are subclasses of ConvertToPlug.pm, but as we want TEXTPlug to also
|
---|
| 130 | # be srcreplaceable and because TEXTPlug does not inherit from ConvertToPlug.pm, we have
|
---|
| 131 | # this ugly solution: same subroutine name.
|
---|
| 132 | # Despite the subroutine name, this method does not in fact create the output html file in
|
---|
| 133 | # some tmp folder. Instead, it creates the html file in the same folder as the input_filename
|
---|
| 134 | # and writes the contents as html paragraphs nested inside <html><head></head><body></body></html>
|
---|
| 135 | # It also sets the encoding of the html document created to UTF-8 in the head's meta tag.
|
---|
| 136 | # Note: doesn't seem to be able to cope with <br /> and <meta ... /> -> slashes are a problem.
|
---|
| 137 | # As a consequence, we resorted to making it not proper xhtml but just regular html.\
|
---|
| 138 | # The output file's name will be utf8 AND might not be the same as the input file's name
|
---|
| 139 | # (for instance, the output filename may have a number appended to it if there is already an html
|
---|
| 140 | # file in the input folder with the same name).
|
---|
| 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 | #my $convert_to_ext = $self->{'convert_to_ext'};
|
---|
| 148 |
|
---|
| 149 | # derive output filename from input filename
|
---|
| 150 | my ($tailname, $dirname, $suffix)
|
---|
| 151 | = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
|
---|
[585] | 152 |
|
---|
[15119] | 153 | # convert to utf-8 otherwise we have problems with the doc.xml file
|
---|
| 154 | # later on
|
---|
| 155 | &unicode::ensure_utf8(\$tailname); # TO DO: does this change the filename or not?
|
---|
[585] | 156 |
|
---|
[15119] | 157 | #my $output_filename = $tailname$output_ext;#output_ext has to be html!
|
---|
| 158 | my $output_filename = &util::filename_cat($dirname, $tailname.".html");
|
---|
| 159 |
|
---|
| 160 | # read contents of text file line by line into an array
|
---|
| 161 | # create an HTML file from the text file
|
---|
| 162 | # Recreate the original file for writing the updated contents
|
---|
| 163 | unless(open(TEXT, "<$input_filename")) { # open it as a new file for writing
|
---|
| 164 | print STDERR "TEXTPlug.pm: Unable to open and read from $input_filename for converting to html...ERROR\n";
|
---|
| 165 | return 0;
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | my @lines = ();
|
---|
| 169 | my $line;
|
---|
| 170 | my $newpara = 1; # true whenever we're going to start a new para
|
---|
| 171 |
|
---|
| 172 | while ($line=<TEXT>) {
|
---|
| 173 | # replace < and > with their character encodings
|
---|
| 174 | $line =~ s/</</g;
|
---|
| 175 | $line =~ s/>/.*>/g;
|
---|
| 176 |
|
---|
| 177 | if ($line =~ /^\s*$/) { # line is empty
|
---|
| 178 | if(!$newpara) {
|
---|
| 179 | push(@lines, "</p>".$line); # end of a paragraph, leave empty line in there
|
---|
| 180 | $newpara = 1;
|
---|
| 181 | } # If it is a new paragraph, we do nothing
|
---|
| 182 | } else { # a line with text
|
---|
| 183 | if($newpara) {
|
---|
| 184 | push(@lines, "<p>".$line); # start a new paragraph
|
---|
| 185 | $newpara = 0;
|
---|
| 186 | } else { # text-line is not a new paragraph, but just a new line
|
---|
| 187 | push(@lines, "<br>\n".$line); # put a break. It doesn't seem to accept <br />
|
---|
| 188 | }
|
---|
| 189 | }
|
---|
| 190 | }
|
---|
| 191 | close TEXT;
|
---|
[585] | 192 |
|
---|
[15119] | 193 | # we've come to the last line of input file, make sure that the text ends on </p>
|
---|
| 194 | # get the last line and check it has a </p> at the end:
|
---|
| 195 | $line = pop(@lines);
|
---|
| 196 | my $endpara = "</p>";
|
---|
| 197 | unless($line =~ m/$endpara$/ ) { # if no </p> at end of last para, then append it
|
---|
| 198 | $line = $line.$endpara;
|
---|
| 199 | }
|
---|
| 200 | push(@lines, $line); # either way, put the last line back
|
---|
[585] | 201 |
|
---|
[15119] | 202 | # write everything into the html file along with html start and end parts:
|
---|
| 203 | my $count = "1";
|
---|
| 204 | # create a sensible html file, don't overwrite pre-existing files of the same name
|
---|
| 205 | # just concatenate a number instead
|
---|
| 206 | while(-e "$output_filename") {
|
---|
| 207 | $output_filename = &util::filename_cat($dirname, $tailname.$count.".html");
|
---|
| 208 | $count++;
|
---|
| 209 | }
|
---|
| 210 | # try creating this new file writing and try opening it for writing, else exit with error value
|
---|
| 211 | unless(open(HTML, ">$output_filename")) { # open the new html file for writing
|
---|
| 212 | print STDERR "TEXTPlug.pm: Unable to create $output_filename for writing $tailname$suffix txt converted to html...ERROR\n";
|
---|
| 213 | return 0;
|
---|
| 214 | }
|
---|
[585] | 215 |
|
---|
[15119] | 216 | # append html start and end sections to the txt contents of the input file
|
---|
| 217 | print HTML "<html>\n<head>\n<title>$tailname</title>\n";
|
---|
| 218 | # we'll set the content to UTF-8 encoding
|
---|
| 219 | print HTML "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n"; # /> not accepted
|
---|
| 220 | print HTML "</head>\n<body>\n";
|
---|
| 221 | # go through each line and write it to the file:
|
---|
| 222 | foreach $line (@lines) {
|
---|
| 223 | print HTML $line;
|
---|
| 224 | }
|
---|
| 225 | print HTML "\n</body></html>\n";
|
---|
| 226 | close HTML;
|
---|
| 227 |
|
---|
| 228 | return $output_filename;
|
---|
| 229 | }
|
---|
[585] | 230 |
|
---|
[15119] | 231 | 1;
|
---|