#!/usr/bin/python
import os
import re
import shutil
def main(old_dir, new_dir, data_dir=None):
names = os.listdir(old_dir)
names.sort()
for name in names:
if name[-4:] == ".txt":
topic = name[:-4]
print topic
txt = file(os.path.join(old_dir, name)).read()
makePage(new_dir, topic, twiki2moin(txt))
if data_dir:
copyAttachments(new_dir, data_dir, topic, txt)
def twiki2moin(txt):
# remove lines starting and ending with %
txt = re.compile("^%.*%$", re.M).sub("", txt)
# change attachment links
txt = re.compile(r"\[\[%ATTACHURL%/(.*?)\]\[(.*?)\]\]").sub('[attachment:\\1 \\2]', txt)
# change links
txt = re.compile(r"\[\[(https?://[^\[\]]+)\]\[(.*?)\]\]").sub('[\\1 \\2]', txt)
txt = re.compile(r"\[\[(.*?)\]\[(.*?)\]\]").sub('[:\\1:\\2]', txt)
txt = re.compile(r"\[\[(.*?)\]\]").sub('["\\1"]', txt)
# convert bold italic
txt = re.compile(r"\b__([^*\n]*?)__").sub("'''''\\1'''''", txt)
# convert italic
txt = re.compile(r"\b_([^*\n]*?)_").sub("''\\1''", txt)
# convert bold
txt = re.compile(r"\*\b([^*\n]*?)\*").sub("'''\\1'''", txt)
# convert verbatim
txt = re.compile(r"\B=\b([^*\n]*?)=\B").sub("{{{\\1}}}", txt)
txt = re.compile(r"
").sub("{{{", txt)
txt = re.compile(r"").sub("}}}", txt)
# convert bold fix .. no equivalent
txt = re.compile(r"\B==\b([^*\n]*?)==\B").sub("{{{\\1}}}", txt)
# convert definition list
# Three spaces, a dollar sign, the term, a colon, a space, followed by the definition ( " $ term: definition" -> "term:: definition" )
txt = re.compile(" \$ (.*): (.*)").sub(" \\1:: \\2", txt)
# convert lists
txt = re.compile("((?: {3})+)([0-9]+)\s(.*)").sub("\\1\\2. \\3", txt)
txt = re.compile("(\t+)([0-9]+)\s(.*)").sub("\\1\\2. \\3", txt)
# convert headings
txt = re.compile("^-?" + re.escape("---+++++") + "\s*(.*)$", re.M).sub("====== \\1 ======", txt)
txt = re.compile("^-?" + re.escape("---++++") + "\s*(.*)$", re.M).sub("===== \\1 =====", txt)
txt = re.compile("^-?" + re.escape("---+++") + "\s*(.*)$", re.M).sub("==== \\1 ====", txt)
txt = re.compile("^-?" + re.escape("---++") + "\s*(.*)$", re.M).sub("=== \\1 ===", txt)
txt = re.compile("^-?" + re.escape("---+") + "\s*(.*)$", re.M).sub("== \\1 ==", txt)
txt = re.compile("^-?" + re.escape("---#") + "\s*(.*)$", re.M).sub("= \\1 =", txt)
# remove signatures
## uncommento to enable ## txt = re.compile("^-- Main.([a-z]+) - [0-9]+ [a-zA-Z]+ 200[0-9]\s*\n?", re.M).sub("", txt)
# tables
txt = re.compile(r"\|", re.M).sub("||", txt)
# rules
txt = re.compile(r"^\s*