Changeset 15119


Ignore:
Timestamp:
03/20/08 20:59:15 (13 years ago)
Author:
ak19
Message:

added srcreplaceable option and the subroutine tmp_area_convert_file to work with new script replace_srcdoc_with_html.pl. Subroutine had to be called this even though the html file is not generated in any tmp area, because ConvertToPlug has a method of this name

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/perllib/plugins/TEXTPlug.pm

    r12169 r15119  
    5656        'abstract' => "no",
    5757        'inherits' => "yes",
     58        'srcreplaceable' => "yes", # Source docs in regular txt format can be replaced with GS-generated html
    5859        'args'     => $arguments };
    5960
     
    124125}
    125126
     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). 
     141sub 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, "\\.[^\\.]+\$");
     152
     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?
     156
     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/</&lt;/g;
     175    $line =~ s/>/.*&gt;/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;
     192
     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
     201
     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    }
     215
     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}
     230
    1262311;
    127 
    128 
    129 
    130 
    131 
    132 
    133 
    134 
    135 
    136 
    137 
Note: See TracChangeset for help on using the changeset viewer.