Commits

Luke Plant committed fdc1103

Implemented pretty printing of HTML when formatting

Comments (0)

Files changed (2)

semanticeditor/utils/etree.py

 from elementtree import ElementTree as ET
 from xml.parsers import expat
 
-# ElementTree utilities
+# ElementTree utilities.  Lots of these are pinched
+# from proposed 'ElementLib' module on effbot.
 
 def textjoin(a, b):
     a = a or ''
     parent[start_idx:stop_idx] = [newelem]
     return newelem
 
+def indent(elem, level=0):
+    i = "\n" + level*"  "
+    if len(elem):
+        if not elem.text or not elem.text.strip():
+            elem.text = i + "  "
+        if not elem.tail or not elem.tail.strip():
+            elem.tail = i
+        for child in elem:
+            indent(child, level+1)
+        if not child.tail or not child.tail.strip():
+            child.tail = i
+        if not elem.tail or not elem.tail.strip():
+            elem.tail = i
+    else:
+        if level and (not elem.tail or not elem.tail.strip()):
+            elem.tail = i

semanticeditor/utils/presentation.py

 """
 
 from elementtree import ElementTree as ET
-from semanticeditor.utils.etree import cleanup, flatten, get_parent, get_index, wrap_elements_in_tag
+from semanticeditor.utils.etree import cleanup, flatten, get_parent, get_index, wrap_elements_in_tag, indent
 from xml.parsers import expat
 import re
 
 # make the previous H1 into the start of a *2* column row!
 
 
-def format_html(html, styleinfo, return_tree=False):
+def format_html(html, styleinfo, return_tree=False, pretty_print=False):
     """
     Formats the XHTML given using a dictionary of style information.
     The dictionary has keys which are the names of headings,
     # Apply row/column commands
     _apply_commands(root, section_nodes, styleinfo, structure)
 
+    # Pretty print
+    if pretty_print:
+        indent(root)
+
     if return_tree:
         return (root, structure, section_nodes)
     else: