|
#!/usr/bin/perl -w # # t2efic --crb3 19Oct06/04apr07 # # takes in text. spits out HTML text suitable for # feeding to eFic, drupal, and maybe other sites too. # # /home/crb3/crb3/perl/f/fic/efic # # $pars=0; # wrap paragraphs in <p>..</p> tags $dotitle=1; # '*!*' is titling toggle $titag='h2'; # what html tags to wrap title with $dobars=1; # convert ----, -=- to <hr> $chatty=$debug=0; while(defined($ARGV[0]) and index($ARGV[0],'-')==0){ $arg=shift(@ARGV); # get any the switches $key=substr($arg,1,1); # get no-arg switches first substr($arg,0,2)=""; if($key eq 'v'){ # verbose? $chatty ^= 1; next; }elsif($key eq 'D'){ # debug mode: emits various tracers $debug^=1; # if any happen to be left in next; }elsif($key eq 'p'){ # add <p></p> formatting? $pars^=1; next; }elsif($key eq 't'){ # ignore titling stuff $dotitle^=1; next; }elsif($key eq 'b'){ # ignore ------- -> hr? $dobars^=1; next; }elsif($key eq '?' or $key eq 'h'){ print <<EOT; t2efic text-to-HTML converter for web-posts --crb3 19oct06/04apr07 t2efic [options] <infile.txt> <outfile.txt> Options: -b toggle: convert ----, -=- bars to <hr> (default: on) -p toggle: wrap paragraphs in <p></p> tags (def: off) -t toggle: '*!*' as title-toggles (def: on) -i <infile.txt> explicit input file arg (or tail-arg implicit) -o <outfile.txt> explicit output file arg (def: infile.efic.html) -v toggle: script gets chatty -D toggle: debug mode. turn on ad-hoc debug tracers -h, -? show helps, then leave EOT exit(0); } $arg =~ s/^\=//; # handles switch=arg $arg=shift(@ARGV) if($arg eq "" and ($ARGV[0] !~ /^\-\w/) ); # # handles space-separated switch/arg if($key eq 'i'){ # infile, explicit method $ifil=$arg; }elsif($key eq 'o'){ # outfile, explicit $ofil=$arg; }else{ warn "$0: unrecognized option -$key $arg\n"; } } unless(defined($ifil)){ die "no infile specified\n" unless defined $ARGV[0]; $ifil=shift(@ARGV); } unless(defined($ofil)){ $ofil=$ARGV[0] if defined $ARGV[0]; } unless(defined($ofil)){ ($ofil=$ifil) =~ s/\.txt$//; $ofil .= ".efic.html"; } $|=1 if $debug; open(IFIL,"<$ifil") or die "can't open infile $ifil\n"; open(OFIL,">$ofil") or die "can't make outfile $ofil\n"; $i=$b=0; # italic/bold state-toggles $pg=""; while(defined($ln=<IFIL>)){ chomp $ln; if($ln =~ /^\s*$/){ # blank line, sometimes with extraneous spaces if(length($pg)){ emit_efic_pg($pg); }elsif($pars){ emit_efic_pg(' '); } $pg=""; }else{ $pg .= "$ln "; } } close(IFIL); emit_efic_pg($pg) if length($pg); close(OFIL); # # emit_efic_pg. # # factored out. uses $b and $i global bold/italic toggles. # sub emit_efic_pg { my $pg=shift(@_); my($p,$pt); if($dotitle){ if( ($p=index($pg,'*!*'))>-1){ if(index($pg,'*!*',$p+3)>-1){ substr($pg,$p,3)="<center><$titag>"; $pt=index($pg,'*!*',$p+3); # things have moved. substr($pg,$pt,3)="</$titag></center>"; } } } if($dobars){ $pg =~ s/\-{4,}/\<hr \/\>/gs if index($pg,'----')>-1; $pg =~ s/\-\=\-/\<center\>\<hr width\=\"30\"\>\<\/center\>/gs if index($pg,'-=-')>-1; # " synbal } $p=0; while( ($pt=index($pg,'_',$p))>-1){ substr($pg,$pt,1)= ($i ? '</i>' : '<i>'); $i ^=1; $p=$pt+1; } $p=0; while( ($pt=index($pg,'*',$p))>-1){ substr($pg,$pt,1)= ($b ? '</b>' : '<b>'); $b ^=1; $p=$pt+1; } if($pars){ substr($pg,0,0)="<p>"; $pg =~ s/\s*$/\<\/p\>/s; } $pg =~ s/ \-{3,} / <hr \/> /gs; print OFIL "$pg\n\n"; # all done. next? print "." if($debug or $chatty); } |
Grab a gzipped copy here |
| Syntax highlighting using Syntax::Highlight::Engine::Kate |