diff --git a/README.md b/README.md index 370290f..6f3c45a 100644 --- a/README.md +++ b/README.md @@ -8,3 +8,23 @@ See this blog post giving example usage: + + +# convert google wiki to creole + +install, e.g.: +``` +~$ virtualenv --python=python2 wikiconvert +~$ cd wikiconvert/ +~/wikiconvert$ source bin/activate +(wikiconvert)~/wikiconvert$ git clone git@github.com:jedie/googlecode2github.git +(wikiconvert)~/wikiconvert$ pip install python-creole markdown +``` + +usage, e.g: +``` +~$ cd wikiconvert/ +~/wikiconvert$ source bin/activate +(wikiconvert)~/wikiconvert$ cd googlecode2github +(wikiconvert)~/wikiconvert$ python googlecode2github.py username/project /path/to/wiki /path/to/new_wiki +``` diff --git a/wiki2creole.py b/wiki2creole.py new file mode 100644 index 0000000..2dc330d --- /dev/null +++ b/wiki2creole.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python + +""" +Usage: + python2 /path/to/wiki2creole.py PROJID SRCDIR DSTDIR + +where "PROJID" is the github project id, e.g. "trentm/python-markdown2", +"SRCDIR" is a Google Code project wiki Subversion working copy dir and +"DSTDIR" is the git clone dir of the git project's wiki. +""" + +__version__ = "1.0.0" + +import sys +import os +import glob +import codecs + +import markdown +from creole import html2creole + +from wikiconvert import log +from wikiconvert import convert_file as wiki2markdown + + +def convert_dir(proj_id, src_dir, dst_dir): + if not os.path.isdir(dst_dir): + log("Create %r..." % dst_dir) + os.makedirs(dst_dir) + + if os.path.isfile(src_dir): + wiki2creole(proj_id, src_dir, dst_dir) + else: + for f in glob.glob(os.path.join(src_dir, "*.wiki")): + wiki2creole(proj_id, f, dst_dir) + + + +def wiki2creole(proj_id, src_path, dst_dir): + log("\nConvert %r..." % src_path) + + gh_page_name, md_file_path = wiki2markdown(proj_id, src_path, dst_dir) + + with codecs.open(md_file_path, 'r', 'utf-8') as f: + md_content = f.read() + + html = markdown.markdown(md_content) + html=html.replace("
", "\n
\n")
+    html=html.replace("
", "
\n") + + creole_content = html2creole(html) + + creole_path = os.path.join(dst_dir, gh_page_name+".creole") + log("write %r..." % creole_path) + with codecs.open(creole_path, 'w', 'utf-8') as f: + f.write(creole_content) + + + +if __name__ == '__main__': + if len(sys.argv) != 4: + print __doc__ + sys.exit(1) + convert_dir(sys.argv[1], sys.argv[2], sys.argv[3]) \ No newline at end of file diff --git a/wikiconvert.py b/wikiconvert.py index d09705d..3681b60 100644 --- a/wikiconvert.py +++ b/wikiconvert.py @@ -30,6 +30,13 @@ def convert_dir(proj_id, src_dir, dst_dir): for f in glob(join(src_dir, "*.wiki")): convert_file(proj_id, f, dst_dir) + +def convert_code(m): + code = m.group("code") + code = "\n".join([" %s" % line for line in code.splitlines()]) + return "\n%s\n" % code + + def convert_file(proj_id, src_path, dst_dir): src = codecs.open(src_path, 'r', 'utf-8').read() meta_lines = [] @@ -55,8 +62,18 @@ def convert_file(proj_id, src_path, dst_dir): text) # Code blocks - text = re.compile(r'^{{{+ *\n', re.M).sub(r"```\n", text) - text = re.compile(r'^}}}+ *(\n|$)', re.M).sub(r"```\n", text) + text = re.sub( + r''' + ^{{{ \s* $ + (?P + (.|\n)+? + ) + ^}}} \s* $ + ''', + convert_code, + text, + flags=re.VERBOSE | re.UNICODE | re.MULTILINE + ) # Headings. text = re.compile(r'^===(.*?)===\s*$', re.M).sub(lambda m: "### %s\n"%m.group(1).strip(), text) @@ -126,6 +143,8 @@ def sub_link(m): codecs.open(dst_path, 'w', 'utf-8').write(text) log("wrote '%s'" % dst_path) + return gh_page_name, dst_path + #---- internal support stuff @@ -134,7 +153,7 @@ def _indent(text): def _gh_page_name_from_gc_page_name(gc): """Github (gh) Wiki page name from Google Code (gc) Wiki page name.""" - gh = re.sub(r'([A-Z][a-z]+)', r'-\1', gc)[1:] + gh = re.sub(r'([A-Z][a-z]+)', r'-\1', gc).strip("-") return gh