in reply to Formatting a text file for browser
Here is the bare bones of what you need to do. You need to escape HTML special. If you want to use something other than <pre> tags you also need to change multiple spaces and tabs into and replace \n with <br>
In this example we wrap the result in <pre> tags to get a fixed width font accurate display of the original text. Tabs are rendered as 4 spaces.
The escapeHTML method this example did use is documented as an internal CGI.pm method so I have re-coded it here as it is naughty to use internal methods. I have also added the PerlMonks square bracket escape. It does not hurt anything.
#!/usr/bin/perl -w my $text = "c:/text.pl"; open TEXT, $text or die "Oops can't open $text $!"; open HTML, ">$text.htm" or die "Oops can't write $text.htm $!"; print HTML "<pre>\n"; while (<TEXT>) { $_ = escapeHTML($_); print HTML $_; } print HTML "</pre>\n"; close HTML; close TEXT; sub escapeHTML { local $_ = shift; # make the required escapes s/&/&/g; s/"/"/g; s/</</g; s/>/>/g; # make the whitespace escapes - not required within pre tags s/\t/ /g; s/( {2,})/" " x length $1/eg; # make the brower bugfix escapes; s/\x8b/‹/g; s/\x9b/›/g; # make the PERL MONKS escapes (if desired) s/\[/[/g; s/\]/]/g; # change newlines to <br> if desired # s/\n/<br>/g; return $_; }
cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|