]> git.sesse.net Git - xml-template/commitdiff
Implement prettyprinting in the PHP5 SWIG version.
authorsgunderson@bigfoot.com <>
Thu, 3 Nov 2011 21:11:14 +0000 (22:11 +0100)
committersgunderson@bigfoot.com <>
Thu, 3 Nov 2011 21:11:14 +0000 (22:11 +0100)
php5-swig/include.php
php5-swig/simple.php
php5-swig/xml-template.swig

index 69ee5132bcd86f0f5dea18bdd7bd4a95a4179e27..6845d50711a67b36d7bc795fc62677f0bfc65c70 100644 (file)
@@ -9,5 +9,5 @@ $master = XML_Template_process_file('../xml/master.xml', array(
        'h1' => 'Nice heading here',
        'contents' => $doc
 ), true);
-print XML_Template_convert_doc_to_string($master);
+print XML_Template_convert_doc_to_string($master, true);
 ?>
index 7e6ce848ca79222eae58d7404ee5b8f13285304c..919d9ee13532ba561e1801010a2f0c15a0c9439f 100644 (file)
@@ -5,7 +5,8 @@ $doc = XML_Template_process_file('../xml/simple.xml', array(
        'title' => 'A very basic example',
        '#hello' => 'Hello world!'
 ), true);
-print XML_Template_convert_doc_to_string($doc);
+XML_Template_clean_whitespace($doc, true);
+print XML_Template_convert_doc_to_string($doc, true);
 
 ?>
 
index 9cd270ec3126b115e1dc24a0fd5b5bbedd226828..5e4e13337c522f87744606f91c5e1682d0e624d4 100644 (file)
@@ -8,6 +8,7 @@ struct XmlDocPtrWrapper {
 %{
 
 #include <memory>
+#include <libxml/globals.h>
 
 #include "../c++11/xml-template.h"
 
@@ -117,14 +118,118 @@ int close_string(void *context)
 
 }  // namespace
 
-std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc)
+std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc, bool prettyprint)
 {
+       xmlIndentTreeOutput = prettyprint;
        std::string ret;
        xmlOutputBufferPtr buf = xmlOutputBufferCreateIO(write_to_string, close_string, &ret, NULL);
-       xmlSaveFileTo(buf, doc->ptr, NULL);
+       xmlSaveFormatFileTo(buf, doc->ptr, "UTF-8", prettyprint);
        return ret;
 }
-       
+
+namespace {
+
+// Remove document fragments (ie. move their content up in the parent node)
+// and combine neighboring text nodes into one.
+void normalize_node(xmlNodePtr node)
+{
+       xmlNode *next_child;
+       for (xmlNode *child = node->children; child != NULL; child = next_child) {
+               next_child = child->next;
+               if (child->type == XML_DOCUMENT_FRAG_NODE) {
+                       while (child->children != NULL) {
+                               xmlAddPrevSibling(child, child->children);
+                       }
+
+                       xmlUnlinkNode(child);
+                       xmlFreeNode(child);
+               }
+       }
+
+       // xmlAddPrevSibling merges adjacent text nodes, but many other things
+       // (including xmlUnlinkNode) do not, so make an extra pass.
+       for (xmlNode *child = node->children; child != NULL; child = child->next) {
+               while (child->type == XML_TEXT_NODE && (child->next != NULL && child->next->type == XML_TEXT_NODE)) {
+                       xmlNode *next_child = child->next;
+
+                       xmlChar *content = xmlNodeGetContent(next_child);
+                       xmlNodeAddContent(child, content);
+                       xmlFree(content);
+
+                       xmlUnlinkNode(next_child);
+                       xmlFreeNode(next_child);
+               }
+               normalize_node(child);
+       }
+}
+
+// Clean the page of non-necessary whitespace. Leaves whitespace alone if and
+// only if xml:space="preserve" on the element. (IOW, it doesn't parse the DTDs,
+// nor the CSS.)
+void clean_node(xmlNodePtr node, bool preserve_whitespace, bool aggressive)
+{
+       if (node->type == XML_TEXT_NODE) {
+               std::string content = reinterpret_cast<const char *>(xmlNodeGetContent(node));
+               if (!preserve_whitespace) {
+                       unsigned dstpos = 0;
+                       for (unsigned srcpos = 0; srcpos < content.size(); ++srcpos, ++dstpos) {
+                               if (content[srcpos] == '\n' ||
+                                   content[srcpos] == '\t' ||
+                                   content[srcpos] == ' ') {
+                                       content[dstpos] = ' ';
+
+                                       // compress double spaces
+                                       if (dstpos > 0 && content[dstpos - 1] == ' ') {
+                                               --dstpos;
+                                       }
+                               } else {
+                                       content[dstpos] = content[srcpos];
+                               }
+                       }
+                       content.resize(dstpos);
+               }
+               if (content.empty() || (aggressive && content == " ")) {
+                       xmlUnlinkNode(node);
+                       xmlFreeNode(node);
+               } else {
+                       xmlNodeSetContentLen(node, reinterpret_cast<const xmlChar *>(content.data()), content.size());
+               }
+       } else {
+               if (node->type == XML_ELEMENT_NODE) {
+                       xmlChar *space = xmlGetProp(node, reinterpret_cast<const xmlChar *>("xml:space"));
+                       preserve_whitespace = (space != NULL && strcmp(reinterpret_cast<const char *>(space), "preserve") == 0);
+               }
+
+               xmlNode *next_child;
+               for (xmlNode *child = node->children; child != NULL; child = next_child) {
+                       next_child = child->next;
+                       clean_node(child, preserve_whitespace, aggressive);
+               }
+
+               if (node->type == XML_ELEMENT_NODE && node->children == NULL) {
+                       std::string tag = reinterpret_cast<const char *>(node->name);
+
+                       // These are the only elements allowed in XHTML to be EMPTY,
+                       // so insert dummy nodes to prevent the output from using
+                       // the <foo/> syntax where not appropriate.
+                       if (tag != "base" && tag != "meta" && tag != "link" && tag != "hr" &&
+                           tag != "br" && tag != "param" && tag != "img" && tag != "area" &&
+                           tag != "input" && tag != "col") {
+                               xmlNode *text = xmlNewText(reinterpret_cast<const xmlChar *>(""));
+                               xmlAddChild(node, text);
+                       }
+               }
+       }
+}
+
+}  // namespace
+
+void XML_Template_clean_whitespace(XmlDocPtrWrapper doc, bool aggressive)
+{
+       normalize_node(xmlDocGetRootElement(doc->ptr));
+       clean_node(xmlDocGetRootElement(doc->ptr), false, aggressive);
+}
+
 %}
 
 %typemap(in) Directive* {
@@ -133,5 +238,6 @@ std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc)
 
 XmlDocPtrWrapper XML_Template_process_file(const std::string &input_filename, Directive *root_directive, bool clean);
 void XML_Template_process(XmlDocPtrWrapper doc, Directive *root_directive, bool clean);
-std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc);
+void XML_Template_clean_whitespace(XmlDocPtrWrapper doc, bool aggressive);
+std::string XML_Template_convert_doc_to_string(XmlDocPtrWrapper doc, bool prettyprint);