diff options
author | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-08 18:11:21 +0100 |
---|---|---|
committer | Benjamin Paassen <bpaassen@techfak.uni-bielefeld.de> | 2015-01-08 18:11:21 +0100 |
commit | 2c3b327739b79d5ba7fe931e205bec1ad320b360 (patch) | |
tree | 6afca317bcd036a51e37e4cbec3cddb6bdeaf555 /src/plugins/html/DemoOutput.cpp | |
parent | 7269e0e232c7971248ffa47aa2ae44786f3d303a (diff) |
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.
Diffstat (limited to 'src/plugins/html/DemoOutput.cpp')
-rw-r--r-- | src/plugins/html/DemoOutput.cpp | 50 |
1 files changed, 34 insertions, 16 deletions
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<model::Document> doc, { Manager &mgr = doc->getManager(); // Create an XML object tree for the document first. - Rooted<xml::Element> html{new xml::Element{mgr, "html"}}; + Rooted<xml::Element> html{new xml::Element{ + mgr, "html", {{"xlmns", "http://www.w3.org/1999/xhtml"}}}}; // add the head Element Rooted<xml::Element> head{new xml::Element{mgr, "head"}}; html->children.push_back(head); + // add the meta element. + Rooted<xml::Element> 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<xml::Element> title{new xml::Element{mgr, "title"}}; head->children.push_back(title); @@ -56,20 +64,21 @@ void DemoHTMLTransformer::writeHTML(Handle<model::Document> doc, body->children.push_back(book); // After the content has been transformed, we serialize it. - html->serialize(out); + html->serialize( + out, + "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\"\n" + "\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">"); } /** * 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<xml::Element> DemoHTMLTransformer::transformSection(Handle<model::StructuredEntity> section) +Rooted<xml::Element> DemoHTMLTransformer::transformSection( + Handle<model::StructuredEntity> section) { Manager &mgr = section->getManager(); // check the section type. @@ -93,7 +103,8 @@ Rooted<xml::Element> DemoHTMLTransformer::transformSection(Handle<model::Structu Rooted<xml::Element> 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<model::StructuredEntity> heading = section->getField("heading")[0]; std::string headingclass; @@ -101,14 +112,11 @@ Rooted<xml::Element> DemoHTMLTransformer::transformSection(Handle<model::Structu case SectionType::BOOK: headingclass = "h1"; break; - case SectionType::CHAPTER: - headingclass = "h2"; - break; case SectionType::SECTION: - headingclass = "h3"; + headingclass = "h2"; break; case SectionType::SUBSECTION: - headingclass = "h4"; + headingclass = "h3"; break; case SectionType::NONE: // this can not happen; @@ -152,14 +160,15 @@ Rooted<xml::Element> DemoHTMLTransformer::transformSection(Handle<model::Structu return sec; } -Rooted<xml::Element> DemoHTMLTransformer::transformParagraph(Handle<model::StructuredEntity> par) +Rooted<xml::Element> DemoHTMLTransformer::transformParagraph( + Handle<model::StructuredEntity> par) { Manager &mgr = par->getManager(); - // create the p xml::Element + // create the p Element Rooted<xml::Element> 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<model::StructuredEntity> heading = par->getField("heading")[0]; // put the heading in a strong xml::Element. Rooted<xml::Element> strong{new xml::Element{mgr, "strong"}}; @@ -172,7 +181,7 @@ Rooted<xml::Element> DemoHTMLTransformer::transformParagraph(Handle<model::Struc strong->children.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<xml::Element> DemoHTMLTransformer::transformParagraph(Handle<model::Struc } return p; } + +// Rooted<xml::Element> +// DemoHTMLTransformer::transformList(Handle<model::StructuredEntity> 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<xml::Element> l{new xml::Element{mgr, listclass}}; +// // iterate through +//} } } |