#!/usr/bin/perl

use MDK::Common;

# convert twiki to mediawiki syntax

foreach my $file (@ARGV) {
    print "processing $file\n";
    substInFile {
        # the meat, the regexps were taken from http://twiki.org/cgi-bin/view/Plugins/MediawikiEditSyntaxRegex#TWiki
        s/\*([^*]*?)\*/'''$1'''/g; # bold
        s/~~([^~]*?)~~/''$1''/g; # italic
        s/^1.1.1.1.1.1 (.*)$/====== $1 ======/; # H6
        s/^1.1.1.1.1 (.*)$/===== $1 =====/; # H5
        s/^1.1.1.1 (.*)$/==== $1 ====/; # H4
        s/^1.1.1 (.*)$/=== $1 ===/; # H3
        s/^1.1 (.*)$/== $1 ==/; # H2
        s/^1 (.*)$/= $1 =/; # H1
        s/^#toc.*$//;
        s/{code}(.*){code}/{{cmd|$1}}/g;
        s/<br>\n/\n/g;
	#s/~~(.*)~~/{{cmd|$1}}/ms;
        s/\[(.*?)\s*?>\s*?(http\:.*?)\]/\[$2 $1\]/g; # FIXME: THIS WORKAROUNDS AN ISSUE IN NEXT REGEXP
        s/\[(.*?)\s*?>\s*?(https\:.*?)\]/\[$2 $1\]/g; # external link [[http:...][label]] 
        s/\[(.*?)\s*?>\s*?([^h][^t][^t][^p].*?)\]/\[\[$2 \|$1\]\]/g; # internal link
        # images:
        s/<img src="([^"]*)">/[[Image::$1]]/;
	# misc:
        s/^#toc.*$//;
    } $file;
}

