Skip to content

Commit

Permalink
Merge pull request #172 from unsignedint/master
Browse files Browse the repository at this point in the history
process XML data before pretty-printing to trim whitespace
  • Loading branch information
jkbrzt committed Mar 18, 2014
2 parents 76ab6e4 + bee10e5 commit 733771f
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions httpie/output.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"""
import json
import xml.dom.minidom
from xml.etree import ElementTree
from functools import partial
from itertools import chain

Expand Down Expand Up @@ -406,13 +406,33 @@ class XMLProcessor(BaseProcessor):
"""XML body processor."""
# TODO: tests

# in-place prettyprint formatter
# c.f. http://effbot.org/zone/element-lib.htm#prettyprint
@staticmethod
def indent(elem, indent_text=' ' * DEFAULT_INDENT):
def _indent(elem, level=0):
i = "\n" + level * indent_text
if len(elem):
if not elem.text or not elem.text.strip():
elem.text = i + indent_text
if not elem.tail or not elem.tail.strip():
elem.tail = i
for elem in elem:
_indent(elem, level + 1)
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
return _indent(elem)

def process_body(self, content, content_type, subtype, encoding):
if subtype == 'xml':
try:
# Pretty print the XML
doc = xml.dom.minidom.parseString(content.encode(encoding))
content = doc.toprettyxml(indent=' ' * DEFAULT_INDENT)
except xml.parsers.expat.ExpatError:
root = ElementTree.fromstring(content.encode(encoding))
self.indent(root)
content = ElementTree.tostring(root)
except ElementTree.ParseError:
# Ignore invalid XML errors (skips attempting to pretty print)
pass
return content
Expand Down

0 comments on commit 733771f

Please sign in to comment.