#! /usr/bin/perl
#
#   Converts a pygment bb output file to Wikidot markup
#   Used to create syntax-colored listings without HTML
#
$/ = undef;
$_ = <>;

#   [/i][/color] often comes at start of next line for some reason
s/\n\[\/i\]/[\/i]\n/mg;
s/\n\[\/color\]/[\/color]\n/mg;

#   Escape stuff Wikidot treats as special
s/\/(\/+)/\@\@\/$1\@\@/mg;
s/\*(\*+)/\@\@*$1\@\@/mg;
s/\-(\-+)/\@\@\-$1\@\@/mg;
s/\_(\_+)/\@\@\_$1\@\@/mg;
s/>>/\@\@>>\@\@/mg;
s/<</\@\@<<\@\@/mg;

#   Replace 2 or more spaces with non-breaking space sequence
s/\t/    /mg;
s/( [ ]+)/{{\@\@$1\@\@}}/mg;

#   Replace [color] on multiple lines
s/\[color=#(......)\]/[[span style="color:#$1"]]/mg;
s/\[\/color\]/[[\/span]]/mg;

#   Replace [i] on one line
s/\[i\]([^\n\[]*)\s*\[\/i\]/\/\/$1\/\//mg;
#   Replace [i] on multiple lines
s/\[i\]/[[span style="font-style:italic"]]/mg;
s/\[\/i\]/[[\/span]]/mg;

s/\[b\]/**/mg;
s/(\s*)\[\/b\]/**$1/mg;

#   Escape [ and ]
s/\[(\[*)/\@\@[$1\@\@/mg;
s/\](\]*)/\@\@]$1\@\@/mg;

#   Fix up [[span...]]
s/\@\@\[\[\@\@span style="([^"]*)"\@\@\]\]\@\@/[[span style="$1"]]/mg;
s/\@\@\[\[\@\@\/span\@\@\]\]\@\@/[[\/span]]/mg;
s/\@\@(\[+)\[\[\@\@span style="([^"]*)"\@\@\]\]\@\@/\@\@$1\@\@[[span style="$2"]]/mg;
s/\@\@\[\[\@\@\/span\@\@\]\](\]+)\@\@/[[\/span]]\@\@$1\@\@/mg;
s/\@\@\[\[\[\@\@\/span\@\@\]\]\@\@/\@\@[\@\@[[\/span]]/mg;
s/\@\@\[\[\@\@span style="([^"]*)"\@\@\]\]\]\@\@/[[span style="$1"]]\@\@]\@\@/mg;

#   Wikidot doesn't like @@@@ in a row
s/\@\@\@\@//mg;

$/ = "\n";
print "[[div class=\"code\"]]\n";
print $_;
print "[[/div]]\n";
