diff options
Diffstat (limited to 'src/xmlfixups.cpp')
-rw-r--r-- | src/xmlfixups.cpp | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/xmlfixups.cpp b/src/xmlfixups.cpp index 458798a..2c05e3a 100644 --- a/src/xmlfixups.cpp +++ b/src/xmlfixups.cpp @@ -65,6 +65,9 @@ static const char* kConsolidateEnd[] = static const char* kConsolidateStart[] = { kElStylesheet, kElInfo, NULL }; +static const char* kNoPretty[] = + { kElPara, NULL }; + void loadStringSet(StringSet& set, const char** strings) { while(*strings) @@ -74,6 +77,7 @@ void loadStringSet(StringSet& set, const char** strings) XmlFixups::XmlFixups() { loadStringSet(m_duplicates, kNoDuplicates); + loadStringSet(m_nopretty, kNoPretty); loadStringSet(m_removes, kRemoveTags); loadStringSet(m_removeEmpty, kRemoveEmpty); loadStringSet(m_requireAttrs, kRequireAttrs); @@ -791,3 +795,56 @@ void XmlFixups::fixBlock(const DOM::Document& doc, DOM::Element& block) parent.replaceChild(el, block); } } + +void XmlFixups::prettyXml(DOM::Document& doc) +{ + DOM::Element el = doc.getDocumentElement(); + internalPrettyXml(doc, el, 0); +} + +void XmlFixups::internalPrettyXml(const DOM::Document& doc, DOM::Element& el, + int level) +{ + DOM::Node child; + + // Empty elements remain as before + if(!el.hasChildNodes()) + return; + + // Make sure we're allowed to pretty print this + if(m_nopretty.find(el.getNodeName()) != m_nopretty.end()) + return; + + // First see if we have any text nodes + for(child = el.getFirstChild(); child != NULL; child = child.getNextSibling()) + { + // Any non-empty text nodes means we don't pretty print this + if(child.getNodeType() == DOM::Node::TEXT_NODE && + !child.getNodeValue().empty()) + return; + } + + // The main indent string + string indent("\r\n"); + indent.append((level + 1) * 4, ' '); + + for(child = el.getFirstChild(); child != NULL; child = child.getNextSibling()) + { + if(child.getNodeType() == DOM::Node::TEXT_NODE) + continue; + + DOM::Text text = doc.createTextNode(indent); + el.insertBefore(text, child); + + if(child.getNodeType() == DOM::Node::ELEMENT_NODE) + internalPrettyXml(doc, (DOM::Element&)child, level + 1); + } + + // The last indent + indent.assign("\r\n"); + indent.append(level * 4, ' '); + + DOM::Text text = doc.createTextNode(indent); + el.appendChild(text); +} + |