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.hpp | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) (limited to 'src/plugins/html/DemoOutput.hpp') diff --git a/src/plugins/html/DemoOutput.hpp b/src/plugins/html/DemoOutput.hpp index 70a5daa..0819d7f 100644 --- a/src/plugins/html/DemoOutput.hpp +++ b/src/plugins/html/DemoOutput.hpp @@ -46,13 +46,7 @@ private: */ Rooted transformSection(Handle sec); Rooted transformParagraph(Handle par); - /** - * This method is to be called recursively to write a list to HTML. - * TODO: Implement - */ -// void writeList(Handle sec, std::ostream& out, -// int tabdepth); - +// Rooted transformList(Handle list); //TODO: Implement emphasis. public: -- cgit v1.2.3 From b05499223916879e051d102e1b7e2fd080f46e7d Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 8 Jan 2015 20:31:30 +0100 Subject: further extended advanced document (now list domain is supported as well) and extended DemoOutput accordingly. --- src/core/model/Domain.cpp | 20 ++++---- src/core/model/Domain.hpp | 6 --- src/plugins/html/DemoOutput.cpp | 38 ++++++++++----- src/plugins/html/DemoOutput.hpp | 4 +- test/core/model/TestAdvanced.hpp | 92 ++++++++++++++++++++++++++++++++++-- test/plugins/html/DemoOutputTest.cpp | 5 +- 6 files changed, 132 insertions(+), 33 deletions(-) (limited to 'src/plugins/html/DemoOutput.hpp') diff --git a/src/core/model/Domain.cpp b/src/core/model/Domain.cpp index f9e2a55..49a3200 100644 --- a/src/core/model/Domain.cpp +++ b/src/core/model/Domain.cpp @@ -53,16 +53,16 @@ void Descriptor::doResolve(std::vector> &res, // &DESCRIPTOR_ATTRIBUTES_ALIAS); } -void StructuredClass::doResolve(std::vector> &res, - const std::vector &path, - Filter filter, void *filterData, unsigned idx, - VisitorSet &visited) -{ - Descriptor::doResolve(res, path, filter, filterData, idx, visited); - if (!isa.isNull()) { - isa->doResolve(res, path, filter, filterData, idx, visited); - } -} +//void StructuredClass::doResolve(std::vector> &res, +// const std::vector &path, +// Filter filter, void *filterData, unsigned idx, +// VisitorSet &visited) +//{ +// Descriptor::doResolve(res, path, filter, filterData, idx, visited); +// if (!isa.isNull()) { +// isa->doResolve(res, path, filter, filterData, idx, visited); +// } +//} void Domain::doResolve(std::vector> &res, const std::vector &path, Filter filter, diff --git a/src/core/model/Domain.hpp b/src/core/model/Domain.hpp index 8d5de0c..6a07b32 100644 --- a/src/core/model/Domain.hpp +++ b/src/core/model/Domain.hpp @@ -468,12 +468,6 @@ private: Owned isa; NodeVector parents; -protected: - void doResolve(std::vector> &res, - const std::vector &path, Filter filter, - void *filterData, unsigned idx, - VisitorSet &visited) override; - public: const bool transparent; // TODO: Is it possible to have root=true and cardinality other than 1? diff --git a/src/plugins/html/DemoOutput.cpp b/src/plugins/html/DemoOutput.cpp index eac240b..307d37a 100644 --- a/src/plugins/html/DemoOutput.cpp +++ b/src/plugins/html/DemoOutput.cpp @@ -147,9 +147,8 @@ Rooted DemoHTMLTransformer::transformSection( Rooted child; if (childDescriptorName == "paragraph") { child = transformParagraph(n); - // TODO: Implement - // } else if(childDescriptorName == "ul"){ - // writeList(n, out); + } else if (childDescriptorName == "ul" || childDescriptorName == "ol") { + child = transformList(n); } else { child = transformSection(n); } @@ -199,13 +198,30 @@ Rooted DemoHTMLTransformer::transformParagraph( return p; } -// Rooted -// 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 -//} +Rooted 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 list items. + for (auto &item : list->getField()) { + std::string itDescrName = item->getDescriptor()->getName(); + if (itDescrName == "item") { + // create the list item. + Rooted li{new xml::Element{mgr, "li"}}; + l->children.push_back(li); + // extract the item text, enveloped in a paragraph Element. + Rooted li_content = transformParagraph(item); + // We omit the paragraph Element and add the children directly to + // the list item + for (auto &n : li_content->children) { + li->children.push_back(n); + } + } + } + return l; +} } } diff --git a/src/plugins/html/DemoOutput.hpp b/src/plugins/html/DemoOutput.hpp index 0819d7f..6c046e7 100644 --- a/src/plugins/html/DemoOutput.hpp +++ b/src/plugins/html/DemoOutput.hpp @@ -46,8 +46,8 @@ private: */ Rooted transformSection(Handle sec); Rooted transformParagraph(Handle par); -// Rooted transformList(Handle list); - //TODO: Implement emphasis. + Rooted transformList(Handle list); +// TODO: Implement emphasis. public: /** diff --git a/test/core/model/TestAdvanced.hpp b/test/core/model/TestAdvanced.hpp index bb7671a..bec00f9 100644 --- a/test/core/model/TestAdvanced.hpp +++ b/test/core/model/TestAdvanced.hpp @@ -56,7 +56,7 @@ static Rooted constructHeadingDomain(Manager &mgr, card.merge({0, 1}); // set up heading StructuredClass. Rooted heading{new StructuredClass( - mgr, "heading", {nullptr}, card, {nullptr}, {nullptr}, true)}; + mgr, "heading", domain, card, {nullptr}, {nullptr}, true)}; // as field we actually want to refer to the field of paragraph. Rooted p = resolveDescriptor(bookDomain, "paragraph"); heading->getFieldDescriptors().push_back(p->getFieldDescriptors()[0]); @@ -75,6 +75,40 @@ static Rooted constructHeadingDomain(Manager &mgr, return domain; } +/** + * This constructs the "list" domain given the book domain. + */ +static Rooted constructListDomain(Manager &mgr, + Handle sys, + Handle bookDomain, + Logger &logger) +{ + // set up domain node. + Rooted domain{new Domain(mgr, sys, "list")}; + // set up cardinality + Cardinality any; + any.merge(Range::typeRangeFrom(0)); + // get book.paragraph + Rooted p = resolveDescriptor(bookDomain, "paragraph"); + // set up item StructuredClass; + Rooted item{new StructuredClass( + mgr, "item", domain, any, {nullptr}, {nullptr}, false)}; + domain->getStructureClasses().push_back(item); + // as field we actually want to refer to the field of paragraph. + item->getFieldDescriptors().push_back(p->getFieldDescriptors()[0]); + // set up list StructuredClasses. + std::vector listTypes{"ol", "ul"}; + for (auto &listType : listTypes) { + Rooted list{new StructuredClass( + mgr, listType, domain, any, {nullptr}, p, false)}; + Rooted list_field{new FieldDescriptor(mgr, list)}; + list_field->getChildren().push_back(item); + list->getFieldDescriptors().push_back(list_field); + domain->getStructureClasses().push_back(list); + } + return domain; +} + static bool addText(Handle parent, std::vector> &doms, const std::string &content) @@ -120,9 +154,10 @@ static bool addHeading(Handle parent, */ static Rooted constructAdvancedDocument(Manager &mgr, Rooted bookDom, - Rooted headingDom) + Rooted headingDom, + Rooted listDom) { - std::vector> doms{bookDom, headingDom}; + std::vector> doms{bookDom, headingDom, listDom}; // Start with the (empty) document. Rooted doc{new Document(mgr, "kant_was_ist_aufklaerung.oxd")}; @@ -144,6 +179,9 @@ static Rooted constructAdvancedDocument(Manager &mgr, // Add the main section. Rooted sec = StructuredEntity::buildEntity(book, doms, "section"); + if (sec.isNull()) { + return {nullptr}; + } // Add the heading. if (!addHeading(sec, doms, "Was ist Aufklärung?")) { @@ -154,6 +192,9 @@ static Rooted constructAdvancedDocument(Manager &mgr, { Rooted p = StructuredEntity::buildEntity(sec, doms, "paragraph"); + if (p.isNull()) { + return {nullptr}; + } // Add its text. // TODO: Use em and strong here if (!addText(p, doms, @@ -172,6 +213,51 @@ static Rooted constructAdvancedDocument(Manager &mgr, } } + // Add the "Lesarten" section + Rooted lesarten = + StructuredEntity::buildEntity(book, doms, "section"); + if (lesarten.isNull()) { + return {nullptr}; + } + // Add the heading. + if (!addHeading(lesarten, doms, "Lesarten")) { + return {nullptr}; + } + // Add list with citations + { + //TODO: We need to restrict this to the list domain. Otherwise + // this leads to resolve errors for some reason. + Rooted ul = + StructuredEntity::buildEntity(lesarten, {listDom}, "ul"); + if (ul.isNull()) { + return {nullptr}; + } + std::vector citations{ + "Berlinische Monatsschrift. Dezember-Heft 1784. S. 481–494.", + "Kant. Kleine Schriften. Neuwied 1793. Haupt. 8o. S. 34–50.", + "I. Kant. Zerstreute Aufsätze. Frankfurt und Leipzig 1793. 8o. S. " + "25–37.", + "I. Kant. Sämmtliche kleine Schriften. 4 Bände. 1797–98. 8o. " + "Königsberg u. Leipzig (Voigt, Jena). Nachdruck. Bd. III, S. " + "159–172.", + " I. Kant's vermischte Schriften. 3 Bände. Halle 1799. " + "(Tieftrunk). Bd. II. S. 687–700.", + "Kant. Vorzügliche kleine Schriften und Aufsätze, hrsg. mit Noten " + "von F. Ch. Starke. 2 Bände. Leipzig 1833 und Quedlinburg 1838. " + "Bd. I, S. 75–84."}; + for (auto &cit : citations) { + // TODO: This needs to be restricted as well. + Rooted item = + StructuredEntity::buildEntity(ul, {listDom}, "item"); + if (item.isNull()) { + return {nullptr}; + } + if (!addText(item, doms, cit)) { + return {nullptr}; + } + } + } + return doc; } } diff --git a/test/plugins/html/DemoOutputTest.cpp b/test/plugins/html/DemoOutputTest.cpp index 7857314..471ccc3 100644 --- a/test/plugins/html/DemoOutputTest.cpp +++ b/test/plugins/html/DemoOutputTest.cpp @@ -43,8 +43,11 @@ TEST(DemoHTMLTransformer, writeHTML) model::constructBookDomain(mgr, sys, logger); Rooted headingDom = model::constructHeadingDomain(mgr, sys, bookDom, logger); + Rooted listDom = + model::constructListDomain(mgr, sys, bookDom, logger); // Construct the document. - Rooted doc = model::constructAdvancedDocument(mgr, bookDom, headingDom); + Rooted doc = + model::constructAdvancedDocument(mgr, bookDom, headingDom, listDom); #ifdef MANAGER_GRAPHVIZ_EXPORT // dump the manager state -- cgit v1.2.3 From 4ec16559eba87553241e2e20a9e31a62b7aed08a Mon Sep 17 00:00:00 2001 From: Benjamin Paassen Date: Thu, 8 Jan 2015 23:33:24 +0100 Subject: first attempts on implementing annotation support for DemoHTML output. --- src/plugins/html/DemoOutput.cpp | 116 +++++++++++++++++++++++++++------------- src/plugins/html/DemoOutput.hpp | 30 ++++++++--- 2 files changed, 103 insertions(+), 43 deletions(-) (limited to 'src/plugins/html/DemoOutput.hpp') diff --git a/src/plugins/html/DemoOutput.cpp b/src/plugins/html/DemoOutput.cpp index c858695..92ff88c 100644 --- a/src/plugins/html/DemoOutput.cpp +++ b/src/plugins/html/DemoOutput.cpp @@ -16,6 +16,9 @@ along with this program. If not, see . */ +#include + + #include #include #include @@ -53,13 +56,23 @@ void DemoHTMLTransformer::writeHTML(Handle doc, // So far was the "preamble". No we have to get to the document content. + // build the start and end map for annotation processing. + AnnoMap startMap; + AnnoMap endMap; + for (auto &a : doc->getAnnotations()) { + // we assume uniquely IDed annotations, which should be checked in the + // validation process. + startMap.emplace(a->getStart()->getName(), a); + endMap.emplace(a->getEnd()->getName(), a); + } + // extract the book root node. Rooted root = doc->getRoot(); if (root->getDescriptor()->getName() != "book") { throw OusiaException("The given documents root is no book node!"); } // transform the book node. - Rooted book = transformSection(root); + Rooted book = transformSection(root, startMap, endMap); // add it as child to the body node. body->children.push_back(book); @@ -89,7 +102,7 @@ SectionType getSectionType(const std::string &name) } Rooted DemoHTMLTransformer::transformSection( - Handle section) + Handle section, AnnoMap &startMap, AnnoMap &endMap) { Manager &mgr = section->getManager(); // check the section type. @@ -125,7 +138,8 @@ Rooted DemoHTMLTransformer::transformSection( Rooted h{new xml::Element{mgr, headingclass}}; sec->children.push_back(h); // extract the heading text, enveloped in a paragraph Element. - Rooted h_content = transformParagraph(heading); + Rooted h_content = + transformParagraph(heading, startMap, endMap); // We omit the paragraph Element and add the children directly to the // heading Element for (auto &n : h_content->children) { @@ -146,11 +160,11 @@ Rooted DemoHTMLTransformer::transformSection( const std::string childDescriptorName = n->getDescriptor()->getName(); Rooted child; if (childDescriptorName == "paragraph") { - child = transformParagraph(n); + child = transformParagraph(n, startMap, endMap); } else if (childDescriptorName == "ul" || childDescriptorName == "ol") { - child = transformList(n); + child = transformList(n, startMap, endMap); } else { - child = transformSection(n); + child = transformSection(n, startMap, endMap); } if (!child.isNull()) { sec->children.push_back(child); @@ -159,8 +173,38 @@ Rooted DemoHTMLTransformer::transformSection( return sec; } +Rooted DemoHTMLTransformer::transformList( + Handle list, AnnoMap &startMap, AnnoMap &endMap) +{ + 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 list items. + for (auto &item : list->getField()) { + std::string itDescrName = item->getDescriptor()->getName(); + if (itDescrName == "item") { + // create the list item. + Rooted li{new xml::Element{mgr, "li"}}; + l->children.push_back(li); + // extract the item text, enveloped in a paragraph Element. + Rooted li_content = + transformParagraph(item, startMap, endMap); + // We omit the paragraph Element and add the children directly to + // the list item + for (auto &n : li_content->children) { + li->children.push_back(n); + } + } + } + return l; +} + +typedef model::AnnotationEntity::Anchor Anchor; +typedef std::stack> AnnoStack; + Rooted DemoHTMLTransformer::transformParagraph( - Handle par) + Handle par, AnnoMap &startMap, AnnoMap &endMap) { Manager &mgr = par->getManager(); // create the p Element @@ -173,7 +217,8 @@ Rooted DemoHTMLTransformer::transformParagraph( Rooted strong{new xml::Element{mgr, "strong"}}; p->children.push_back(strong); // extract the heading text, enveloped in a paragraph Element. - Rooted h_content = transformParagraph(heading); + Rooted h_content = + transformParagraph(heading, startMap, endMap); // We omit the paragraph Element and add the children directly to the // heading Element for (auto &n : h_content->children) { @@ -183,8 +228,33 @@ Rooted DemoHTMLTransformer::transformParagraph( // transform paragraph children to XML as well for (auto &n : par->getField()) { - if (n->isa(typeOf())) { - // TODO: Handle Anchors! + if (n->isa(typeOf())) { + //TODO: This needs some more brain work. +// // check if this is a start Anchor. +// auto it = startMap.find(n->getName()); +// if(it != startMap.end()){ +// // if we have a start Anchor, we put another AnnotationEntity +// // on top the stack. +// opened.push(it->second); +// // and we create an open tag. +// +// continue; +// } +// // check if this is an end Anchor. +// auto it = endMap.find(n->getName()); +// if(it != endMap.end()){ +// /* +// * Now it gets somewhat interesting: We have to close all +// * tags that started after the one that is closed now and +// * re-open them afterwards. So we create a lokal stack to +// * temporarily store all AnnotationEntities that need to +// * be re-opened. +// */ +// AnnoStack tmp; +// Rooted< +// while(!opened.empty() && ) +// } +// continue; } std::string childDescriptorName = n->getDescriptor()->getName(); @@ -200,31 +270,5 @@ Rooted DemoHTMLTransformer::transformParagraph( } return p; } - -Rooted 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 list items. - for (auto &item : list->getField()) { - std::string itDescrName = item->getDescriptor()->getName(); - if (itDescrName == "item") { - // create the list item. - Rooted li{new xml::Element{mgr, "li"}}; - l->children.push_back(li); - // extract the item text, enveloped in a paragraph Element. - Rooted li_content = transformParagraph(item); - // We omit the paragraph Element and add the children directly to - // the list item - for (auto &n : li_content->children) { - li->children.push_back(n); - } - } - } - return l; -} } } diff --git a/src/plugins/html/DemoOutput.hpp b/src/plugins/html/DemoOutput.hpp index 6c046e7..e08ec2b 100644 --- a/src/plugins/html/DemoOutput.hpp +++ b/src/plugins/html/DemoOutput.hpp @@ -30,6 +30,7 @@ #ifndef _OUSIA_HTML_DEMO_OUTPUT_HPP_ #define _OUSIA_HTML_DEMO_OUTPUT_HPP_ +#include #include #include @@ -38,16 +39,31 @@ namespace ousia { namespace html { +typedef std::map> AnnoMap; + class DemoHTMLTransformer { private: /** - * These methods are called recursively to transform a document to an XML - * tree. + * This transforms a section-like entity, namely book, section + * and subsection, to an XHTML element, including its header. For the + * children of the default field the respective transform function is + * called recursively. + */ + Rooted transformSection(Handle sec, + AnnoMap& startMap, AnnoMap& endMap); + /** + * This transforms a list entity, namely ul and ol to an XHTML element. + * For each item, the transformParagraph function is called. + */ + Rooted transformList(Handle list, + AnnoMap& startMap, AnnoMap& endMap); + /** + * This transforms a paragraph-like entity, namely heading, item and + * paragraph, to an XHTML element including the text and the anchors + * contained. For anchor handling we require the AnnoMaps. */ - Rooted transformSection(Handle sec); - Rooted transformParagraph(Handle par); - Rooted transformList(Handle list); -// TODO: Implement emphasis. + Rooted transformParagraph(Handle par, + AnnoMap& startMap, AnnoMap& endMap); public: /** @@ -68,7 +84,7 @@ public: * and lists domains but no other. * @param out is the output stream the data shall be written to. */ - void writeHTML(Handle doc, std::ostream& out); + void writeHTML(Handle doc, std::ostream &out); }; } } -- cgit v1.2.3