[Twisted-Python] Pretty printing in Woven
Alex Levy
mesozoic at polynode.com
Tue Jul 22 18:25:21 MDT 2003
Attached is a diff for pretty-printing HTML in Woven. I removed strip=1
from microdom.toprettyxml because it was senselessly munging whitespace; I
couldn't think of a good reason to keep this in, since dumping out huge
blocks of text in a single line never makes readable code.
The rest of it works, but others may have better ideas for how to do this.
Index: twisted/web/microdom.py
===================================================================
RCS file: /cvs/Twisted/twisted/web/microdom.py,v
retrieving revision 1.95
diff -u -r1.95 microdom.py
--- twisted/web/microdom.py 19 Jul 2003 02:09:50 -0000 1.95
+++ twisted/web/microdom.py 22 Jul 2003 04:17:56 -0000
@@ -137,10 +137,10 @@
rv = s.getvalue()
return rv
- def writeprettyxml(self, stream, indent='', addindent=' ', newl='\n', strip=1):
+ def writeprettyxml(self, stream, indent='', addindent=' ', newl='\n', strip=0):
return self.writexml(stream, indent, addindent, newl, strip)
- def toprettyxml(self, indent='', addindent=' ', newl='\n', strip=1):
+ def toprettyxml(self, indent='', addindent=' ', newl='\n', strip=0):
return self.toxml(indent, addindent, newl, strip)
def cloneNode(self, deep=0, parent=None):
@@ -477,6 +477,13 @@
ALLOWSINGLETON = ('img', 'br', 'hr', 'base', 'meta', 'link', 'param',
'area', 'input', 'col', 'basefont', 'isindex',
'frame')
+ BLOCKELEMENTS = ('html', 'head', 'body', 'noscript', 'ins', 'del',
+ 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'script',
+ 'ul', 'ol', 'dl', 'pre', 'hr', 'blockquote',
+ 'address', 'p', 'div', 'fieldset', 'table', 'tr',
+ 'form', 'object', 'fieldset', 'applet', 'map')
+ FORMATNICELY = ('tr', 'ul', 'ol', 'head')
+
# this should never be necessary unless people start
# changing .tagName on the fly(?)
if not self.preserveCase:
@@ -487,8 +494,11 @@
for ns in nsprefixes.keys():
del newprefixes[ns]
else:
- newprefixes = {}
- begin = [newl, indent, '<']
+ newprefixes = {}
+
+ begin = ['<']
+ if self.tagName in BLOCKELEMENTS:
+ begin = [newl, indent] + begin
bext = begin.extend
writeattr = lambda _atr, _val: bext((' ', _atr, '="', escape(_val), '"'))
if namespace != self.namespace:
@@ -525,8 +535,14 @@
w(">")
newindent = indent + addindent
for child in self.childNodes:
+ if self.tagName in BLOCKELEMENTS and \
+ self.tagName in FORMATNICELY:
+ w(j((newl, newindent)))
child.writexml(stream, newindent, addindent, newl, strip, downprefixes, self.namespace)
- w(j((newl, indent, "</", self.endTagName, '>')))
+ if self.tagName in BLOCKELEMENTS:
+ w(j((newl, indent)))
+ w(j(("</", self.endTagName, '>')))
+
elif self.tagName.lower() not in ALLOWSINGLETON:
w(j(('></', self.endTagName, '>')))
else:
Index: twisted/web/woven/utils.py
===================================================================
RCS file: /cvs/Twisted/twisted/web/woven/utils.py,v
retrieving revision 1.14
diff -u -r1.14 utils.py
--- twisted/web/woven/utils.py 13 May 2003 00:57:18 -0000 1.14
+++ twisted/web/woven/utils.py 22 Jul 2003 04:17:56 -0000
@@ -15,7 +15,7 @@
def doSendPage(self, d, request):
- page = str(d.toxml())
+ page = str(d.toprettyxml())
request.setHeader('content-length', str(len(page)))
request.write(page)
request.finish()
--
Alex Levy
WWW: http://mesozoic.geecs.org
"Never let your sense of morals prevent you from doing what is right."
-- Salvor Hardin, Isaac Asimov's _Foundation_
More information about the Twisted-Python
mailing list