#!perl #Applies wiki formatting to each input file and writes output to .html open OUT, ">@ARGV[0].html" or die "Could not open @ARGV[0].html"; while (readLine()) { escape(); #Apply HTML escaping if (pre()) {next} #Print preformatted blocks then continue with next line strong(); em(); code(); a(); #Apply in-line formatting if (table() or ul() or ol()) {redo} #Print multi-line blocks then continue with current line if (h2() or h3() or h4() or hr() or p()) {print OUT "$_\n"; next} #Print single-lines then continue with next line print OUT "$_\n" #Print remains } continue { if (eof) { close OUT; open OUT, ">@ARGV[0].html" or die "Could not open @ARGV[0].html"; } } sub readLine { $_ = <>; chomp; escape(); return defined $_; } sub escape { s~&~&~g; s~<~<~g; s~>~>~g; } sub pre { if (/^{{{/) { s~^{{{~
~;
        print OUT "$_\n";
        while (readLine()) {
            if (/^}}}/) {last}
            print OUT "$_\n";
        }
        s~^}}}~
~; print OUT "$_\n"; return 1; } } sub strong {s~__(.+?)__~$1~g} sub em {s~''(.+?)''~$1~g} sub code {s~{{(.+?)}}~$1~g} sub a { if (/\[(.+?)\|(.+?)\]/) {return s~\[(.+?)\|(.+?)\]~$1~} return s~\[(.+?)\]~$1~ } sub h2 {return s~^!!!+(.*)~

$1

~} sub h3 {return s~^!!(.*)~

$1

~} sub h4 {return s~^!(.*)~

$1

~} sub hr {return s~^---+$~
~} sub p {return s~^\s*$~

~} sub ul {return list("^\\*", "

\n", "\\*")} sub ol {return list("^#", "
    \n", "
\n", "#")} sub list { local ($tag, $start, $end, $continuation) = @_; if (/$tag/) { print OUT $start; li(); while (readLine()) { if (!/$tag/ && !/^ /) {last} li(); } print OUT $end; return 1; } } sub li { list($tag.$continuation, $start, $end, $continuation); s~$tag~
  • ~; print OUT "$_\n"; } sub table { if (/^\|/) { print OUT "\n"; row(); while(readLine()) { if (!/^\|/) {last;} row(); } print OUT "
    \n"; return 1; } } sub row { s~\|\|~~g; s~\|~~g; print OUT "$_\n"; }