diff options
-rw-r--r-- | src/core/XML.cpp | 53 | ||||
-rw-r--r-- | src/core/XML.hpp | 16 | ||||
-rw-r--r-- | src/plugins/html/DemoOutput.cpp | 6 | ||||
-rw-r--r-- | src/plugins/html/DemoOutput.hpp | 11 | ||||
-rw-r--r-- | test/core/model/TestAdvanced.hpp | 2 | ||||
-rw-r--r-- | test/plugins/html/DemoOutputTest.cpp | 23 |
6 files changed, 79 insertions, 32 deletions
diff --git a/src/core/XML.cpp b/src/core/XML.cpp index 9cfb5dc..b7d1511 100644 --- a/src/core/XML.cpp +++ b/src/core/XML.cpp @@ -4,18 +4,23 @@ namespace ousia { namespace xml { -void Node::serialize(std::ostream &out, const std::string &doctype) +void Node::serialize(std::ostream &out, const std::string &doctype, bool pretty) { if (doctype != "") { - out << doctype << "\n"; + out << doctype; + if (pretty) { + out << '\n'; + } } - doSerialize(out, 0); + doSerialize(out, 0, pretty); } -void Element::doSerialize(std::ostream &out, unsigned int tabdepth) +void Element::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) { - for (unsigned int t = 0; t < tabdepth; t++) { - out << '\t'; + if (pretty) { + for (unsigned int t = 0; t < tabdepth; t++) { + out << '\t'; + } } out << '<' << name; for (auto &a : attributes) { @@ -23,25 +28,41 @@ void Element::doSerialize(std::ostream &out, unsigned int tabdepth) } // if we have no children, we close the tag immediately. if (children.size() == 0) { - out << "/>\n"; + out << "/>"; + if (pretty) { + out << '\n'; + } } else { - out << ">\n"; + out << ">"; + if (pretty) { + out << '\n'; + } for (auto &n : children) { - n->doSerialize(out, tabdepth + 1); + n->doSerialize(out, tabdepth + 1, pretty); } - for (unsigned int t = 0; t < tabdepth; t++) { - out << '\t'; + if (pretty) { + for (unsigned int t = 0; t < tabdepth; t++) { + out << '\t'; + } + } + out << "</" << name << ">"; + if (pretty) { + out << '\n'; } - out << "</" << name << ">\n"; } } -void Text::doSerialize(std::ostream &out, unsigned int tabdepth) +void Text::doSerialize(std::ostream &out, unsigned int tabdepth, bool pretty) { - for (unsigned int t = 0; t < tabdepth; t++) { - out << '\t'; + if (pretty) { + for (unsigned int t = 0; t < tabdepth; t++) { + out << '\t'; + } + } + out << text; + if (pretty) { + out << '\n'; } - out << text << '\n'; } } diff --git a/src/core/XML.hpp b/src/core/XML.hpp index e55ecba..b05d4c6 100644 --- a/src/core/XML.hpp +++ b/src/core/XML.hpp @@ -73,9 +73,12 @@ public: * @param out is the output stream the serialized data shall be * written to. * @param doctype enables you to add a prefix specifying the doctype. + * @param pretty is a flag that manipulates whether newlines and tabs are + * used. */ void serialize(std::ostream &out, - const std::string &doctype = "<?xml version=\"1.0\"?>"); + const std::string &doctype = "<?xml version=\"1.0\"?>", + bool pretty = true); /** * This method just writes the XML representation of this node to the * output stream. @@ -83,8 +86,11 @@ public: * @param out the output stream the serialized data shall be written * to. * @param tabdepth the current tabdepth for prettier output. + * @param pretty is a flag that manipulates whether newlines and tabs are + * used. */ - virtual void doSerialize(std::ostream &out, unsigned int tabdepth) = 0; + virtual void doSerialize(std::ostream &out, unsigned int tabdepth, + bool pretty) = 0; /** * @return the parent XML element of this node. @@ -130,7 +136,8 @@ public: * * The end tag of this element. * */ - void doSerialize(std::ostream &out, unsigned int tabdepth) override; + void doSerialize(std::ostream &out, unsigned int tabdepth, + bool pretty) override; const ManagedVector<Node> &getChildren() const { return children; } @@ -155,7 +162,8 @@ public: * This just writes the text to the output. * */ - void doSerialize(std::ostream &out, unsigned int tabdepth) override; + void doSerialize(std::ostream &out, unsigned int tabdepth, + bool pretty) override; }; } diff --git a/src/plugins/html/DemoOutput.cpp b/src/plugins/html/DemoOutput.cpp index 69f2756..4cadf12 100644 --- a/src/plugins/html/DemoOutput.cpp +++ b/src/plugins/html/DemoOutput.cpp @@ -29,12 +29,12 @@ namespace ousia { namespace html { void DemoHTMLTransformer::writeHTML(Handle<model::Document> doc, - std::ostream &out) + std::ostream &out, bool pretty) { Manager &mgr = doc->getManager(); // Create an XML object tree for the document first. Rooted<xml::Element> html{new xml::Element{ - mgr, {nullptr}, "html", {{"xlmns", "http://www.w3.org/1999/xhtml"}}}}; + mgr, {nullptr}, "html"}}; // add the head Element Rooted<xml::Element> head{new xml::Element{mgr, html, "head"}}; html->addChild(head); @@ -78,7 +78,7 @@ void DemoHTMLTransformer::writeHTML(Handle<model::Document> doc, body->addChild(book); // After the content has been transformed, we serialize it. - html->serialize(out, "<!DOCTYPE html>"); + html->serialize(out, "<!DOCTYPE html>", pretty); } /** diff --git a/src/plugins/html/DemoOutput.hpp b/src/plugins/html/DemoOutput.hpp index 07d758b..b755aac 100644 --- a/src/plugins/html/DemoOutput.hpp +++ b/src/plugins/html/DemoOutput.hpp @@ -83,11 +83,14 @@ public: * Therefore this is not an adequate model of our algorithms but only a * Demo. * - * @param doc is a Document using concepts of the book, headings, emphasis - * and lists domains but no other. - * @param out is the output stream the data shall be written to. + * @param doc is a Document using concepts of the book, headings, + * emphasis and lists domains but no other. + * @param out is the output stream the data shall be written to. + * @param pretty is a flag that manipulates whether newlines and tabs are + * used. */ - void writeHTML(Handle<model::Document> doc, std::ostream &out); + void writeHTML(Handle<model::Document> doc, std::ostream &out, + bool pretty = true); }; } } diff --git a/test/core/model/TestAdvanced.hpp b/test/core/model/TestAdvanced.hpp index a1510e7..769a0df 100644 --- a/test/core/model/TestAdvanced.hpp +++ b/test/core/model/TestAdvanced.hpp @@ -254,7 +254,7 @@ static Rooted<Document> constructAdvancedDocument(Manager &mgr, Logger &logger, { if (!addAnnotation(logger, doc, p, "Aufklärung ist der Ausgang des Menschen aus " - "seiner selbstverschuldeten Unmündigkeit", + "seiner selbstverschuldeten Unmündigkeit!", "strong")) { return {nullptr}; } diff --git a/test/plugins/html/DemoOutputTest.cpp b/test/plugins/html/DemoOutputTest.cpp index 5a34112..5b2758d 100644 --- a/test/plugins/html/DemoOutputTest.cpp +++ b/test/plugins/html/DemoOutputTest.cpp @@ -19,6 +19,7 @@ #include <gtest/gtest.h> #include <iostream> +#include <sstream> #include <plugins/html/DemoOutput.hpp> @@ -56,9 +57,17 @@ TEST(DemoHTMLTransformer, writeHTML) mgr.exportGraphviz("bookDocument.dot"); #endif - // TODO: change this. Don't use printouts + // we can only do a rough check here. DemoHTMLTransformer transformer; - transformer.writeHTML(doc, std::cout); + std::stringstream out; + transformer.writeHTML(doc, out); + const std::string res = out.str(); + ASSERT_FALSE(res == ""); + ASSERT_TRUE(res.find("Was ist Aufklärung?") != std::string::npos); + ASSERT_TRUE(res.find( + "Aufklärung ist der Ausgang des Menschen aus seiner " + "selbstverschuldeten Unmündigkeit!") != std::string::npos); + ASSERT_TRUE(res.find("Sapere aude!") != std::string::npos); } TEST(DemoHTMLTransformer, AnnotationProcessing) @@ -100,9 +109,15 @@ TEST(DemoHTMLTransformer, AnnotationProcessing) buildAnnotationEntity(doc, logger, {"emphasized"}, em_start, em_end); buildAnnotationEntity(doc, logger, {"strong"}, strong_start, strong_end); - // TODO: change this. Don't use printouts + // Check serialization. DemoHTMLTransformer transformer; - transformer.writeHTML(doc, std::cout); + std::stringstream out; + transformer.writeHTML(doc, out, false); + const std::string res = out.str(); + // In HTML the overlapping structure must be serialized as follows: + ASSERT_TRUE( + res.find("<em>bla<strong>blub</strong></em><strong>bla</strong>") != + std::string::npos); } } } |