From 2c3b327739b79d5ba7fe931e205bec1ad320b360 Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 8 Jan 2015 18:11:21 +0100 Subject: further extended the advanced document example, slightly improved XML serialization and fixed a bug in DemoOutput leading to errors if a section/paragraph had no heading. --- src/plugins/html/DemoOutput.cpp | 50 ++++++++++++++++++++++++++++------------- 1 file changed, 34 insertions(+), 16 deletions(-) (limited to 'src/plugins/html/DemoOutput.cpp') diff --git a/src/plugins/html/DemoOutput.cpp b/src/plugins/html/DemoOutput.cpp index 035ba25..eac240b 100644 --- a/src/plugins/html/DemoOutput.cpp +++ b/src/plugins/html/DemoOutput.cpp @@ -30,10 +30,18 @@ void DemoHTMLTransformer::writeHTML(Handle doc, { Manager &mgr = doc->getManager(); // Create an XML object tree for the document first. - Rooted html{new xml::Element{mgr, "html"}}; + Rooted html{new xml::Element{ + mgr, "html", {{"xlmns", "http://www.w3.org/1999/xhtml"}}}}; // add the head Element Rooted head{new xml::Element{mgr, "head"}}; html->children.push_back(head); + // add the meta element. + Rooted meta{ + new xml::Element{mgr, + "meta", + {{"http-equiv", "Content-Type"}, + {"content", "text/html; charset=utf-8"}}}}; + head->children.push_back(meta); // add the title Element with Text Rooted title{new xml::Element{mgr, "title"}}; head->children.push_back(title); @@ -56,20 +64,21 @@ void DemoHTMLTransformer::writeHTML(Handle doc, body->children.push_back(book); // After the content has been transformed, we serialize it. - html->serialize(out); + html->serialize( + out, + ""); } /** * This is just for easier internal handling. */ -enum class SectionType { BOOK, CHAPTER, SECTION, SUBSECTION, NONE }; +enum class SectionType { BOOK, SECTION, SUBSECTION, NONE }; SectionType getSectionType(const std::string &name) { if (name == "book") { return SectionType::BOOK; - } else if (name == "chapter") { - return SectionType::CHAPTER; } else if (name == "section") { return SectionType::SECTION; } else if (name == "subsection") { @@ -79,7 +88,8 @@ SectionType getSectionType(const std::string &name) } } -Rooted DemoHTMLTransformer::transformSection(Handle section) +Rooted DemoHTMLTransformer::transformSection( + Handle section) { Manager &mgr = section->getManager(); // check the section type. @@ -93,7 +103,8 @@ Rooted DemoHTMLTransformer::transformSection(Handle sec{ new xml::Element{mgr, "div", {{"class", secclass}}}}; // check if we have a heading. - if (section->hasField("heading")) { + if (section->hasField("heading") && + section->getField("heading").size() > 0) { Rooted heading = section->getField("heading")[0]; std::string headingclass; @@ -101,14 +112,11 @@ Rooted DemoHTMLTransformer::transformSection(Handle DemoHTMLTransformer::transformSection(Handle DemoHTMLTransformer::transformParagraph(Handle par) +Rooted DemoHTMLTransformer::transformParagraph( + Handle par) { Manager &mgr = par->getManager(); - // create the p xml::Element + // create the p Element Rooted p{new xml::Element{mgr, "p"}}; // check if we have a heading. - if (par->hasField("heading")) { + if (par->hasField("heading") && par->getField("heading").size() > 0) { Rooted heading = par->getField("heading")[0]; // put the heading in a strong xml::Element. Rooted strong{new xml::Element{mgr, "strong"}}; @@ -172,7 +181,7 @@ Rooted DemoHTMLTransformer::transformParagraph(Handlechildren.push_back(n); } } - + // transform paragraph children to XML as well for (auto &n : par->getField()) { std::string childDescriptorName = n->getDescriptor()->getName(); @@ -189,5 +198,14 @@ Rooted DemoHTMLTransformer::transformParagraph(Handle +// DemoHTMLTransformer::transformList(Handle list){ +// Manager &mgr = list->getManager(); +// // create the list Element, which is either ul or ol (depends on descriptor) +// std::string listclass = list->getDescriptor()->getName(); +// Rooted l{new xml::Element{mgr, listclass}}; +// // iterate through +//} } } -- cgit v1.2.3