#!/bin/bash
#
# set of sed rules that transform a MoinMoin source file
# from the format that the wiki accepts to plain html
#
# version hg-0 <user:harjoc server:gmail.com>

function s {
	perl -p -e "$1"
}

function ct {
	PAT="$1" TAG="$2" perl -e '
my $last = "";
my $pat=@ENV{PAT};
my $tag=@ENV{TAG};
while (<>) {
	print "<$tag>\n"  if (    /$pat/ and not $last =~ /$pat/);
	print "</$tag>\n" if (not /$pat/ and     $last =~ /$pat/);
	print;
	$last = $_;
}'
}

s 's/\[\[TableOfContents\]\]//g' |\
s 's/===(.*?)===/<h4>\1<\/h4>/g' |\
s 's/==(.*?)==/<h3>\1<\/h3>/g' |\
s 's/=(.*?)=/<h2>\1<\/h2>/g' |\
s 's/^{{{$/<pre><code>/g' |\
s 's/^}}}$/<\/code><\/pre>/g' |\
s 's/{{{(.*?)}}}/<tt>\1<\/tt>/g' |\
s 's/\["(.*?)"\]/<em>\1<\/em>/g' |\
s "s/''(.*?)''/<em>\1<\/em>/g" |\
ct  '^\|\|' table |\
s 's/^\|\|/<tr>||/g' |\
s 's/\|\|$//g' |\
s 's/\|\|/<td>/g' |\
ct  '^ *\*' ul |\
s 's/ *\*/ <li>/' |\
cat

