Skip to content

Instantly share code, notes, and snippets.

@orenhe
Created May 3, 2012 14:09
Show Gist options
  • Save orenhe/2585891 to your computer and use it in GitHub Desktop.
Save orenhe/2585891 to your computer and use it in GitHub Desktop.
mediawiki2rst
#!/usr/bin/perl
#
# mediawiki2rst - converts mediawiki markup to Restructured Text format
#
# python-docutils provide rst2<whatever> tools, thus it can be one hop towards converting to html, odt, latex etc
#
# License: BSD-style
#
# Author: Oren Held
#
# Note: this is a very incomplete implementation
#
while (<>) {
chomp;
# Sections
if (/====\s*([^=]+)\s*====/) {
$len=length($1);
print "\n".$1."\n"; print "-"x$len."\n\n";
next;
}
elsif (/===\s*([^=]+)\s*===/) {
$len=length($1);
print "\n".$1."\n"; print "="x$len."\n\n";
next;
}
elsif (/==\s*([^=]+)\s*==/) {
$len=length($1);
print "\n"."-"x $len."\n"; print $1."\n"; print "-"x$len."\n\n";
next;
};
# Bullets and enumerations
if (/^*/ || /^#/) {
s/^#/#./;
s/^\*\*\*/ \*/;
s/^\*\*/ \*/;
print $_;
print "\n\n";
next;
};
# Emphasis - TODO - this part needs to be run after all output, because some lines won't reach this stage due to
# 'next' statements earlier
s/\'\'\'([^\']+)\'\'\'/**$1**/g;
s/\'\'([^\']+)\'\'/*$1*/g;
print $_;
print "\n";
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment