Created
March 29, 2011 19:47
-
-
Save hintjens/893087 to your computer and use it in GitHub Desktop.
pygmentize bb to wikidot
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #! /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; | |
| # Replace spaces with Unicode non-breaking space | |
| s/ / /mg; | |
| s/\t/ /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\]/\*\*/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; | |
| # Wikidot doesn't like @@@@ in a row | |
| s/\@\@\@\@//mg; | |
| $/ = "\n"; | |
| print "[[div class=\"code\"]]\n"; | |
| print $_; | |
| print "[[/div]]\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment