#!/usr/bin/perl -w # # t2h v0.03 --crb3 09feb01/20feb01/04mar02 # started out with fanfiction.net stream. then expanded switch-options # to allow for full pages, set up the title nicely, etc.; still have # yet to get it to tear off mail-headers. # this tool doesn't do all the pretty tabling done by the various # tools on the Win32 side (ws2html etc), it just converts from # a r.a.a.c-style text posting to something that can be fed to # ff.net, or (full-page) posted on an internal server. # # --crb3 04mar04: if given filename doen't exist, try tacking ".txt" # onto it and look for that. # $debug = 0; # get chatty, show internal results # # these are command-line switches # $para=1; # use

paragraphing? $hardln=0; # convert \n to
? $fullpage=0; # generate full page? $tpt=0; # got a template for fullpage? $template=""; # here's where its name goes $title="title"; $author="author"; die "txt2html [options] infile [outfile] options: -f emit full HTML page, not just a story stream toggle -h keep hard linebreaks toggle -p use

paragraphs toggle (default) -a'AUTHOR' insert author's name\n" if($#ARGV < 0); for($nm=$b=0;$b<=$#ARGV;$b++){ if($ARGV[$b] =~ /^\-p/i){ # P: use

paragraphs $para ^= 1; }elsif($ARGV[$b] =~ /^\-h/i){ # H: keep hard linebreaks $hardln ^= 1; }elsif($ARGV[$b] =~ /^\-f/i){ # F: emit full HTML page $fullpage ^= 1; $template = $ARGV[$b]; chomp $template; $template =~ s/^\-f\=?//; if($template ne ""){ $tpt=1; # is a template filename print "using template $template... "; }else{ print "using internal template... "; } }elsif($ARGV[$b] =~ /^\-a/i){ # A: insert author's name $author = $ARGV[$b]; $author =~ s/^\-a\=?//i; print "author = $author\n" if $debug; }elsif($nm==0){ $infile = $ARGV[$b]; $nm++; print "infile = $infile\n" if $debug; }elsif($nm==1){ $outfile = $ARGV[$b]; $nm++; print "outfile = $outfile\n" if $debug; }else{ warn "Extra arg ignored: $ARGV[$b]\n"; } } print "nm=$nm\n" if $debug; if($nm<2){ $outfile = $infile; $outfile =~ s/\.txt//i; } $infile .= ".txt" unless(-e $infile); $outfile .= ".html" unless $outfile =~ /\.html?$/i; die "oops, infile and outfile are both $infile\n" if $infile eq $outfile; print "Converting $infile to $outfile"; print " full" if $fullpage; print "\n"; open(IFIL,"<$infile") or die "Can't open infile $infile\n"; open(OFIL,">$outfile") or die "Can't make outfile $outfile\n"; print OFIL "

" if $para; $firstln=1; while(defined($inline=)){ $blank=0; chomp $inline; if($firstln){ $title = $inline; # snag it in case it's used $title =~ s/^\;//; $firstln=0; $datacnt=0 if $debug; if($tpt){ open(TFIL,"<$template") or die "can't open template $template\n"; } # # #PAYLOAD# tag must be on a line by itself. # if($fullpage){ while(defined($pgline=&tfetch) and $pgline !~ /^\#PAYLOAD\#/i){ $pgline =~ s/\#TITLE\#/$title/i; $pgline =~ s/\#AUTHOR\#/$author/i; print OFIL $pgline; $datacnt++ if $debug; } next; # don't print first line twice } print "datacnt=$datacnt\n" if $debug; } if($inline =~ /^\;\ -> }else{ $inline =~ s/\ before adding $inline =~ s/\>/>/g; # in any tags } if($inline =~ /^\s*$/){ # blank line? if($para){ $inline = "

"; }else{ $inline = "
"; } $blank=1; } $inline =~ s#(^|[\s,.!?"'])\_\*(\w)#$1$2#g; # _*bolditalic*_ $inline =~ s#(\w[,.!?"']*)\*\_([\s.,!?"']|$)#$1$2#g; $inline =~ s#(^|[\s.,!?"'])\_(\w)#$1$2#g; # _italic_ $inline =~ s#(\w[,.!?"']*)\_([\s.,!?"']|$)#$1$2#g; # # --crb3 02dec01: special coding for title... # $inline =~ s#(^|[\s.,!?"'])\*\!\*(\w)#$1

$2#g; #*!*Title*!* $inline =~ s#(\w[\],.!?"']*)\*\!\*([\s,.!?"']|$)#$1

$2#g; $inline =~ s#(^|[\s.,!?"'])\*(\w)#$1$2#g; # *bold* $inline =~ s#(\w[,.!?"']*)\*([\s,.!?"']|$)#$1$2#g; $inline =~ s/\-{8,}/\
/g; $inline .= "
" if ($hardln and not $blank); print OFIL "$inline\n"; } print OFIL "

\n" if $para; if($fullpage){ while(defined($pgline=&tfetch)){ $pgline =~ s/\#TITLE\#/$title/i; $pgline =~ s/\#AUTHOR\#/$author/i; print OFIL $pgline; } } close IFIL; close OFIL; close TFIL if $tpt; # # what's past here is a plain-jane page framework, just enough # to put some margins on the page and get through picky browsers. # I use it to post fics for intranet browsing at home. # # sub tfetch { my($i); if($tpt){ return($i=); }else{ return($i=); } } __END__ #TITLE#

#TITLE#

#PAYLOAD#