#!/usr/bin/python # -*- coding: utf-8 -*- # License: GPLv3 or later, http://www.gnu.org/licenses/gpl.html __version__ = "$Revision: 119 $" _rules = [#rules that are more specific are first { #links r"\[\[http\:\/\/(.*?)\|(.*?)\]\]":r"[http://\1 \2]", r"\[\[http\:\/\/(.*?)\]\]":r"[http://\1]", }, { #formatting special r"\n@@(.*?)@@\n":r"{{{\n\1\n}}}", }, { #titles r"(?m)^! (.*)$":r"= \1 =", r"(?m)^!! (.*)$":r"== \1 ==", r"(?m)^!!! (.*)$":r"=== \1 ===", r"(?m)^!!!! (.*)$":r"==== \1 ====", r"\[\[(.*?)\]\]":r"[wiki:\1]", #formatting r"\{\+(.*?)\+\}":r"__\1__", r"@@(.*?)@@":r"`\1`", r"(?s)\[@(.*?)@\]":r"{{{\n\1\n}}}", r"\{\-(.*?)\-\}":r"~~\1~~", r"'\^(.*?)\^'":r"^\1^", r"'_(.*?)_'":r",,\1,,", #code r"(?s)=([a-zA-Z_]+) \[=(.*?)=\]":r"{{{\n#!\1\2}}}", #list r"(?m)^\* (.*)$":r" * \1", r"(?m)^\*\* (.*)$":r" * \1", r"(?m)^\*\*\* (.*)$":r" * \1", r"(?m)^# (.*)$":r" 1. \1", r"(?m)^## (.*)$":r" 1. \1", r"(?m)^### (.*)$":r" 1. \1", #definition r"(?m)^: (.*) : $":r" \1::\n", "":"" }, { #color r"(?s)\%(black|blue|gray|green|maroon|navy|purple|red|silver|white|yellow)\% ?(.*?)\n?\%\%":r"[[span(\2,style=color: \1)]]" }, { #prevent CamelCase r"(\s)([A-Z][a-z]+[A-Z][a-z]+)(\s)":r"\1!\2\3", r"(\s)([A-Z][a-z]+[A-Z][a-z]+[A-Z][a-z]+)(\s)":r"\1!\2\3", r"(\s)([A-Z][a-z]+[A-Z][a-z]+[A-Z][a-z]+[A-Z][a-z]+)(\s)":r"\1!\2\3", } ] import sys class PmWiki2Trac: def __init__(self, options): self.options = options def loadfile(self, inputfile): if (inputfile == None): self.inputfile = sys.stdin else: self.inputfile = file(inputfile,"r") self.text = self.inputfile.read() def convert(self): import re self.convertedtext = self.text for ruleset in _rules: for regexp, replace in ruleset.iteritems(): self.convertedtext = re.sub(regexp, replace, self.convertedtext) def getconvertedtext(self): return self.convertedtext def output(self, outputfile): if (outputfile == None): self.outputfile = sys.stdout else: self.outputfile = file(outputfile,"w") self.outputfile.write(self.convertedtext) def _parseargs(args): usage = """usage: %prog [options] [inputfile] [outputfile]""" from optparse import OptionParser optionparser = OptionParser(usage=usage,version="%prog v"+__version__) optionparser.add_option("--verbose", dest="verbose", action="store_true", help="be more verbose", default=False) (options, args) = optionparser.parse_args(args = args) if (len(args) < 0): optionparser.print_help() exit(-1) return (options, args) def main(args): (options, args) = _parseargs(args) pmwiki2trac = PmWiki2Trac(options) if (len(args) >= 1): pmwiki2trac.loadfile(args[0]) else: pmwiki2trac.loadfile(None) pmwiki2trac.convert() if (len(args) >= 2): pmwiki2trac.output(args[1]) else: pmwiki2trac.output(None) if __name__ == "__main__": main(None)