Ignore:
Timestamp:
2008-03-29T17:41:28+13:00 (16 years ago)
Author:
ak19
Message:

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

File:
1 edited

Legend:

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

    r15119 r15150  
    110110    # Add FileFormat metadata
    111111    $doc_obj->add_metadata($cursection, "FileFormat", "TEXT");
    112    
    113 
     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) = @_;
     123   
    114124    # we need to escape the escape character, or else mg will convert into
    115125    # eg literal newlines, instead of leaving the text as '\n'
     
    118128    $$textref =~ s/</&lt;/g;
    119129    $$textref =~ s/>/&gt;/g;
    120 
     130   
    121131    # insert preformat tags and add text to document object
    122     $doc_obj->add_utf8_text($cursection, "<pre>\n$$textref\n</pre>");
    123 
    124     return 1;
    125 }
     132    $$textref = "<pre>\n$$textref\n</pre>";
     133}
     134
    126135
    127136# replace_srcdoc_with_html.pl requires all subroutines that support src_replaceable
     
    129138# Perl modules that are subclasses of ConvertToPlug.pm, but as we want TEXTPlug to also
    130139# 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). 
     140# a similar subroutine with the same name here.
    141141sub tmp_area_convert_file {
    142142    my $self = shift (@_);
    143143    my ($output_ext, $input_filename) = @_;
    144144   
    145     #my $outhandle = $self->{'outhandle'};
     145    my $outhandle = $self->{'outhandle'};
    146146    #my $failhandle = $self->{'failhandle'};
    147     #my $convert_to_ext = $self->{'convert_to_ext'};
    148147   
    149148    # derive output filename from input filename
     
    151150    = &File::Basename::fileparse($input_filename, "\\.[^\\.]+\$");
    152151
     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
    153163    # convert to utf-8 otherwise we have problems with the doc.xml file
    154164    # later on
    155     &unicode::ensure_utf8(\$tailname); # TO DO: does this change the filename or not?
     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
    156171
    157172    #my $output_filename = $tailname$output_ext;#output_ext has to be html!
    158     my $output_filename = &util::filename_cat($dirname, $tailname.".html");
     173    my $output_filename = &util::filename_cat($tmp_dirname, $tailname.".html");
    159174   
    160175    # read contents of text file line by line into an array
    161176    # create an HTML file from the text file
    162177    # Recreate the original file for writing the updated contents
    163     unless(open(TEXT, "<$input_filename")) { # open it as a new file for writing
     178    unless(open(TEXT, "<$tmp_filename")) { # open it as a new file for writing
    164179    print STDERR "TEXTPlug.pm: Unable to open and read from $input_filename for converting to html...ERROR\n";
    165     return 0;
    166     }
     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);
    167192       
    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     }
     193    #print STDERR "****output_filename: $output_filename\n";
     194    #print STDERR "****text: $text\n";
     195
    210196    # try creating this new file writing and try opening it for writing, else exit with error value
    211197    unless(open(HTML, ">$output_filename")) {  # open the new html file for writing
    212198    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";
     199    return ""; # no filename
     200    }
     201    # write the html contents in text out to the file
     202    print HTML $text;
    226203    close HTML;
    227  
    228     return $output_filename;
    229 }
     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
    230211
    2312121;
Note: See TracChangeset for help on using the changeset viewer.