Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Use for inspiration only. Very much a 1-off tool limited to a specific migration's needs.
#!/usr/bin/python
# This is a simple script to convert a very limited set of MoinMoin wiki syntax
#to markdown. I am using it in my migration from MoinMoin to a gitit+markdown
#wiki.
DO_GIT = False
#DISABLED#DO_GIT = True
import os
import re
REPLACEMENTS = (
("(./)", "[x]"),
)
WIKI_WORDS = """SomePage
AnotherPage""".split()
class HeaderConverter(object):
expression = re.compile("^(=+)(.*?)=+")
def process(self, line):
match = self.expression.match(line)
if match:
level = len(match.group(1))
return ("#" * level) + match.group(2).rstrip()
return line
class UnorderedListConverter(object):
expression = re.compile("^( +)\*(.*)")
def process(self, line):
match = self.expression.match(line)
if match:
level = len(match.group(1))
return (" " * (level - 1)) + "*" + match.group(2).rstrip()
return line
class OrderedListConverter(object):
expression = re.compile("^( +)\\d+\.(.*)")
def process(self, line):
match = self.expression.match(line)
if match:
level = len(match.group(1))
return (" " * (level - 1)) + "1." + match.group(2).rstrip()
return line
class WikiWordConverter(object):
def process(self, line):
for word in WIKI_WORDS:
if word in line:
return line.replace(word, "[%s]()" % word)
return line
def convert(moin):
lineConverters = [HeaderConverter(), UnorderedListConverter(),
OrderedListConverter(), WikiWordConverter()]
markdownLines = []
for line in moin.split("\n"):
for converter in lineConverters:
line = converter.process(line)
markdownLines.append(line)
markdown = "\n".join(markdownLines)
for pair in REPLACEMENTS:
markdown = markdown.replace(pair[0], pair[1])
return markdown
def visit(wikiWords, dirName, fileNames):
fileNames.sort()
if not dirName.endswith("/revisions"):
return
parentDir = os.path.basename(os.path.dirname(dirName))
if parentDir not in wikiWords and not parentDir.startswith("WhiteBoard"):
return
print "Processing %s revisions of page %s" %(len(fileNames), parentDir)
for revision in fileNames:
inFile = open(os.path.join(dirName, revision))
outFile = open(parentDir + ".page", "w")
outFile.write(convert(inFile.read()))
inFile.close()
outFile.close()
if DO_GIT:
os.system("git add '%s.page'" % parentDir)
os.system("git commit -m 'Automatic conversion from MoinMoin to" \
" gitit/Markdown via the moin_to_markdown.py script." \
" Page: %s Revision: %s'" % (parentDir, revision))
if __name__ == "__main__":
os.path.walk("/var/local/plwiki/data/pages", visit, WIKI_WORDS)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.