1.1 --- a/convert.py Sun Apr 22 01:50:20 2012 +0200
1.2 +++ b/convert.py Sun Apr 22 02:19:08 2012 +0200
1.3 @@ -27,6 +27,7 @@
1.4 from cStringIO import StringIO
1.5 import codecs
1.6 import xmlread
1.7 +import parser
1.8
1.9 class ConfluenceHandler:
1.10
1.11 @@ -99,7 +100,7 @@
1.12 # Handle revisions.
1.13
1.14 elif objecttype == "BodyContent":
1.15 - write(join(versions_dir, content["content"]), content["body"])
1.16 + translate(join(versions_dir, content["content"]), content["body"])
1.17
1.18 self.content = {}
1.19
1.20 @@ -169,6 +170,19 @@
1.21 finally:
1.22 f.close()
1.23
1.24 +def translate(filename, body):
1.25 +
1.26 + """
1.27 + Write to the file with the given 'filename' a translation of the given
1.28 + 'body'.
1.29 + """
1.30 +
1.31 + out = codecs.open(filename, "w", encoding="utf-8")
1.32 + try:
1.33 + parser.parse(body, out)
1.34 + finally:
1.35 + out.close()
1.36 +
1.37 def sort_manifest(filename, pagetitle, output=None):
1.38
1.39 """
2.1 --- a/parser.py Sun Apr 22 01:50:20 2012 +0200
2.2 +++ b/parser.py Sun Apr 22 02:19:08 2012 +0200
2.3 @@ -156,22 +156,41 @@
2.4 listitem_regexp_str = r"^([*#-])+\s*(.*)$"
2.5 listitem_regexp = re.compile(listitem_regexp_str, re.MULTILINE)
2.6
2.7 +blocktypes = {
2.8 + "h1" : "= %s =",
2.9 + "h2" : "== %s ==",
2.10 + "h3" : "=== %s ===",
2.11 + "h4" : "==== %s ====",
2.12 + "h5" : "===== %s =====",
2.13 + "h6" : "====== %s ======",
2.14 + "bq" : "{{{%s}}}",
2.15 + }
2.16 +
2.17 +def parse(s, out):
2.18 +
2.19 + "Parse the content in the string 's', writing a translation to 'out'."
2.20 +
2.21 + for type, text in get_regions(s):
2.22 +
2.23 + # Handle list, heading, blockquote or anonymous blocks.
2.24 +
2.25 + if type is None:
2.26 + for blocktype, blocktext in get_blocks(text):
2.27 + if blocktypes.has_key(blocktype):
2.28 + print >>out, blocktypes[blocktype] % blocktext
2.29 + else:
2.30 + print >>out, blocktext
2.31 +
2.32 + # Handle sections.
2.33 +
2.34 + else:
2.35 + print >>out, "Region type:", type
2.36 + print >>out, text
2.37 +
2.38 if __name__ == "__main__":
2.39 import sys
2.40
2.41 s = sys.stdin.read()
2.42 -
2.43 - for type, text in get_regions(s):
2.44 - if type is None:
2.45 - for blocktype, blocktext in get_blocks(text):
2.46 - print "Block type:", blocktype
2.47 - print blocktext
2.48 - print
2.49 - else:
2.50 - print "Region type:", type
2.51 - print text
2.52 - print
2.53 -
2.54 - print "-" * 60
2.55 + parse(s, sys.stdout)
2.56
2.57 # vim: tabstop=4 expandtab shiftwidth=4