mail[Wesnoth-commits] r43544 - /trunk/data/tools/wesnoth/wmlparser2.py


Others Months | Index by Date | Thread Index
>>   [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Header


Content

Posted by AI0867 on June 18, 2010 - 03:11:
Author: ai0867
Date: Fri Jun 18 03:11:18 2010
New Revision: 43544

URL: http://svn.gna.org/viewcvs/wesnoth?rev=43544&view=rev
Log:
Port --to-xml option to wmlparser2

Modified:
    trunk/data/tools/wesnoth/wmlparser2.py

Modified: trunk/data/tools/wesnoth/wmlparser2.py
URL: 
http://svn.gna.org/viewcvs/wesnoth/trunk/data/tools/wesnoth/wmlparser2.py?rev=43544&r1=43543&r2=43544&view=diff
==============================================================================
--- trunk/data/tools/wesnoth/wmlparser2.py (original)
+++ trunk/data/tools/wesnoth/wmlparser2.py Fri Jun 18 03:11:18 2010
@@ -429,7 +429,27 @@
         sys.stdout.write(sdepth1)
     sys.stdout.write("}")
 
+from xml.sax.saxutils import escape
+def xmlify(tree, verbose=False, depth=0):
+    sdepth = ""
+    if verbose:
+        sdepth = "  " * depth
+    for child in tree.data:
+        if isinstance(child, TagNode):
+            print '%s<%s>' % (sdepth, child.name)
+            xmlify(child, verbose, depth + 1)
+            print '%s</%s>' % (sdepth, child.name)
+        else:
+            if "\n" in child.get_text() or "\r" in child.get_text():
+                print sdepth + '<' + child.name + '>' + \
+            '<![CDATA[' + child.get_text() + ']]>' + '</' + child.name + '>'
+            else:
+                print sdepth + '<' + child.name + '>' + \
+            escape(child.get_text())  + '</' + child.name + '>'
+
 if __name__ == "__main__":
+    # Hack to make us not crash when we encounter characters that aren't 
ASCII
+    sys.stdout = __import__("codecs").getwriter('utf-8')(sys.stdout)
     opt = optparse.OptionParser()
     opt.add_option("-i", "--input")
     opt.add_option("-t", "--text")
@@ -437,6 +457,7 @@
     opt.add_option("-d", "--defines")
     opt.add_option("-T", "--test", action = "store_true")
     opt.add_option("-j", "--to-json", action = "store_true")
+    opt.add_option("-x", "--to-xml", action = "store_true")
     options, args = opt.parse_args()
 
     if not options.input and not options.text and not options.test:
@@ -571,6 +592,8 @@
     if options.to_json:
         jsonify(p.root, True)
         print
+    elif options.to_xml:
+        xmlify(p.root, True)
     else:
         print(p.root.debug())
 




Related Messages


Powered by MHonArc, Updated Fri Jun 18 04:00:47 2010