1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
|
/*
Ousía
Copyright (C) 2014 Benjamin Paaßen, Andreas Stöckel
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stack>
#include <core/common/Exceptions.hpp>
#include <core/common/Rtti.hpp>
#include <core/common/Variant.hpp>
#include "DemoOutput.hpp"
namespace ousia {
namespace html {
typedef std::stack<Rooted<AnnotationEntity>> AnnoStack;
static bool canHandleAnchor(Handle<Anchor> a)
{
std::string annoClassName = a->getAnnotation()->getDescriptor()->getName();
return annoClassName == "emphasized" || annoClassName == "strong";
}
static Rooted<xml::Element> openAnnotation(Manager &mgr, AnnoStack &opened,
Handle<AnnotationEntity> entity,
Handle<xml::Element> current,
bool stackOnly)
{
// we push the newly opened entity on top of the stack.
opened.push(entity);
if (stackOnly) {
return nullptr;
}
// get the elment name
std::string elemName = entity->getDescriptor()->getName();
// emphasized has to be shortened
if (elemName == "emphasized") {
elemName = "em";
}
// create the new XML element representing the annotation
Rooted<xml::Element> tmp{new xml::Element{mgr, current, elemName}};
current->addChild(tmp);
// and return it.
return tmp;
}
static Rooted<xml::Element> transformAnchor(Manager &mgr, Handle<Anchor> a,
Handle<xml::Element> current,
Logger &logger, AnnoStack &opened,
bool stackOnly)
{
// check if this is a start Anchor.
if (a->isStart()) {
// if we have a start anchor, we open an annotation element.
current =
openAnnotation(mgr, opened, a->getAnnotation(), current, stackOnly);
// check if this is an end Anchor.
} else if (a->isEnd()) {
/*
* 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;
if (opened.empty()) {
// if we have no opened entities left, that is a
// malformed document.
logger.error("An unopened entity was closed!", *a);
return current;
}
Rooted<AnnotationEntity> closed = opened.top();
current = current->getParent();
opened.pop();
while (closed != a->getAnnotation()) {
/*
* We implicitly close tags by climbing up the XML tree
* until we are at the right element.
*/
current = current->getParent();
tmp.push(closed);
if (opened.empty()) {
// if we have no opened entities left, that is a
// malformed document.
logger.error("An unopened entity was closed!", *a);
return current;
}
closed = opened.top();
opened.pop();
}
// At this point we have closed all necessary entities. Now we
// need to re-open some of them.
while (!tmp.empty()) {
closed = tmp.top();
tmp.pop();
current = openAnnotation(mgr, opened, closed, current, stackOnly);
}
}
// otherwise it is a disconnected Anchor and we can ignore it.
return current;
}
/**
* Reopens all Annotations in the given AnnoStack but does not manipulate the
* original stack. The input argument is a copy.
* @return the innermost opened element.
*/
static Rooted<xml::Element> reOpenAnnotations(Manager &mgr, AnnoStack opened,
Handle<xml::Element> parent)
{
AnnoStack tmp;
while (!opened.empty()) {
tmp.push(opened.top());
opened.pop();
}
Rooted<xml::Element> current = parent;
while (!tmp.empty()) {
Rooted<AnnotationEntity> closed = tmp.top();
tmp.pop();
current = openAnnotation(mgr, opened, closed, current, false);
}
return current;
}
static Rooted<xml::Element> transformParagraph(Manager &mgr,
Handle<xml::Element> parent,
Handle<StructuredEntity> par,
Logger &logger,
AnnoStack &opened)
{
// create the p Element
Rooted<xml::Element> p{new xml::Element{mgr, parent, "p"}};
// check if we have a heading.
if (par->getDescriptor()->hasField("heading") &&
par->getField("heading").size() > 0) {
Handle<StructuredEntity> heading =
par->getField("heading")[0].cast<StructuredEntity>();
// put the heading in a strong xml::Element.
Rooted<xml::Element> strong{new xml::Element{mgr, p, "strong"}};
p->addChild(strong);
// extract the heading text, enveloped in a paragraph Element.
// in this case we use an empy annotation stack because annotations do
// not extend on subtree fields.
AnnoStack emptyStack;
Rooted<xml::Element> h_content =
transformParagraph(mgr, strong, heading, logger, emptyStack);
// We omit the paragraph Element and add the children directly to the
// heading Element
for (auto &n : h_content->getChildren()) {
strong->addChild(n);
}
}
// reopen all annotations.
Rooted<xml::Element> current = reOpenAnnotations(mgr, opened, p);
// transform paragraph children to XML as well
for (auto &n : par->getField()) {
if (n->isa(&RttiTypes::Anchor)) {
Rooted<Anchor> a = n.cast<Anchor>();
if (canHandleAnchor(a)) {
current =
transformAnchor(mgr, a, current, logger, opened, false);
}
continue;
}
// if this is not an anchor, we can only handle text.
if (!n->isa(&RttiTypes::StructuredEntity)) {
continue;
}
Handle<StructuredEntity> t = n.cast<StructuredEntity>();
std::string childDescriptorName = t->getDescriptor()->getName();
if (childDescriptorName == "text") {
Handle<DocumentPrimitive> primitive =
t->getField()[0].cast<DocumentPrimitive>();
std::string text_content = primitive->getContent().asString();
current->addChild(new xml::Text(mgr, current, text_content));
}
}
// at this point we implicitly close all annotations that are left opened.
// they will be reopened in the next paragraph.
return p;
}
static Rooted<xml::Element> transformList(Manager &mgr,
Handle<xml::Element> parent,
Handle<StructuredEntity> list,
Logger &logger, AnnoStack &opened)
{
// 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, parent, listclass}};
// iterate through list items.
for (auto &it : list->getField()) {
if (it->isa(&RttiTypes::Anchor)) {
Rooted<Anchor> a = it.cast<Anchor>();
if (canHandleAnchor(a)) {
// just put the entity on the AnnoStack, but do not open it
// explicitly. That will be done inside the next paragraph.
transformAnchor(mgr, a, l, logger, opened, true);
}
continue;
}
Handle<StructuredEntity> item = it.cast<StructuredEntity>();
std::string itDescrName = item->getDescriptor()->getName();
if (itDescrName == "item") {
// create the list item.
Rooted<xml::Element> li{new xml::Element{mgr, l, "li"}};
l->addChild(li);
// extract the item text, enveloped in a paragraph Element.
Rooted<xml::Element> li_content =
transformParagraph(mgr, li, item, logger, opened);
// We omit the paragraph Element and add the children directly to
// the list item
for (auto &n : li_content->getChildren()) {
li->addChild(n);
}
}
}
return l;
}
/**
* This is just for easier internal handling.
*/
enum class SectionType { BOOK, CHAPTER, SECTION, SUBSECTION, NONE };
static 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") {
return SectionType::SUBSECTION;
} else {
return SectionType::NONE;
}
}
static Rooted<xml::Element> transformSection(Manager &mgr,
Handle<xml::Element> parent,
Handle<StructuredEntity> section,
Logger &logger, AnnoStack &opened)
{
// check the section type.
const std::string secclass = section->getDescriptor()->getName();
SectionType type = getSectionType(secclass);
if (type == SectionType::NONE) {
// if the input node is no section, we ignore it.
return {nullptr};
}
// create a section tag containing the sections content.
Rooted<xml::Element> sec{
new xml::Element{mgr, parent, "section", {{"class", secclass}}}};
// check if we have a heading.
if (section->getDescriptor()->hasField("heading") &&
section->getField("heading").size() > 0) {
Handle<StructuredEntity> heading =
section->getField("heading")[0].cast<StructuredEntity>();
std::string headingclass;
switch (type) {
case SectionType::BOOK:
headingclass = "h1";
break;
case SectionType::CHAPTER:
headingclass = "h2";
break;
case SectionType::SECTION:
headingclass = "h3";
break;
case SectionType::SUBSECTION:
headingclass = "h4";
break;
case SectionType::NONE:
// this can not happen;
break;
}
Rooted<xml::Element> h{new xml::Element{mgr, sec, headingclass}};
sec->addChild(h);
// extract the heading text, wrapped in a paragraph Element.
// in this case we use an empy annotation stack because annotations do
// not extend on subtree fields.
AnnoStack emptyStack;
Rooted<xml::Element> h_content =
transformParagraph(mgr, h, heading, logger, emptyStack);
// We omit the paragraph element and add the children directly to the
// heading Element
for (auto &n : h_content->getChildren()) {
h->addChild(n);
}
}
// Then we get all the children.
for (auto &n : section->getField()) {
if (n->isa(&RttiTypes::Anchor)) {
Rooted<Anchor> a = n.cast<Anchor>();
if (canHandleAnchor(a)) {
// just put the entity on the AnnoStack, but do not open it
// explicitly. That will be done inside the next paragraph.
transformAnchor(mgr, a, sec, logger, opened, true);
}
continue;
}
if (!n->isa(&RttiTypes::StructuredEntity)) {
continue;
}
Handle<StructuredEntity> s = n.cast<StructuredEntity>();
/*
* Strictly speaking this is the wrong mechanism, because we would have
* to make an "isa" call here because we can not rely on our knowledge
* that paragraphs can only be paragraphs or lists. There would have
* to be a listener structure of transformations that check if they can
* transform this specific node.
*/
const std::string childDescriptorName = s->getDescriptor()->getName();
Rooted<xml::Element> child;
if (childDescriptorName == "paragraph") {
child = transformParagraph(mgr, sec, s, logger, opened);
} else if (childDescriptorName == "ul" || childDescriptorName == "ol") {
child = transformList(mgr, sec, s, logger, opened);
} else {
child = transformSection(mgr, sec, s, logger, opened);
}
if (!child.isNull()) {
sec->addChild(child);
}
}
return sec;
}
void DemoHTMLTransformer::writeHTML(Handle<Document> doc, std::ostream &out,
Logger &logger, bool pretty)
{
// validate the document.
if (!doc->validate(logger)) {
return;
}
Manager &mgr = doc->getManager();
// initialize an empty annotation Stack.
AnnoStack opened;
// Create an XML object tree for the document first.
Rooted<xml::Element> html{new xml::Element{mgr, {nullptr}, "html"}};
// add the head Element
Rooted<xml::Element> head{new xml::Element{mgr, html, "head"}};
html->addChild(head);
// add the meta element.
Rooted<xml::Element> meta{
new xml::Element{mgr,
head,
"meta",
{{"http-equiv", "Content-Type"},
{"content", "text/html; charset=utf-8"}}}};
head->addChild(meta);
// add the title Element with Text
Rooted<xml::Element> title{new xml::Element{mgr, head, "title"}};
head->addChild(title);
title->addChild(
new xml::Text(mgr, title, "Test HTML Output for " + doc->getName()));
// add the body Element
Rooted<xml::Element> body{new xml::Element{mgr, html, "body"}};
html->addChild(body);
// So far was the "preamble". No we have to get to the document content.
// extract the book root node.
Rooted<StructuredEntity> root = doc->getRoot();
if (root->getDescriptor()->getName() != "book") {
throw OusiaException("The given documents root is no book node!");
}
// transform the book node.
Rooted<xml::Element> book =
transformSection(mgr, body, root, logger, opened);
// add it as child to the body node.
body->addChild(book);
// After the content has been transformed, we serialize it.
html->serialize(out, "<!DOCTYPE html>", pretty);
}
}
}
|