-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathdocument.cpp
More file actions
489 lines (411 loc) · 15.4 KB
/
Copy pathdocument.cpp
File metadata and controls
489 lines (411 loc) · 15.4 KB
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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
#include <odr/internal/html/document.hpp>
#include <odr/document.hpp>
#include <odr/document_element.hpp>
#include <odr/exceptions.hpp>
#include <odr/file.hpp>
#include <odr/html.hpp>
#include <odr/style.hpp>
#include <odr/internal/abstract/html_service.hpp>
#include <odr/internal/common/null_stream.hpp>
#include <odr/internal/common/path.hpp>
#include <odr/internal/html/common.hpp>
#include <odr/internal/html/document_element.hpp>
#include <odr/internal/html/document_style.hpp>
#include <odr/internal/html/html_service.hpp>
#include <odr/internal/html/html_writer.hpp>
#include <odr/internal/util/stream_util.hpp>
#include <odr/internal/util/string_util.hpp>
#include <algorithm>
#include <mutex>
namespace odr::internal::html {
namespace {
void front(const Document &document, const WritingState &state) {
HtmlWriter &out = state.out();
const bool paged_content =
(document.document_type() == DocumentType::text &&
state.config().text_document_margin) ||
document.document_type() == DocumentType::presentation ||
document.document_type() == DocumentType::drawing;
out.write_begin();
out.write_header_begin();
out.write_header_charset("UTF-8");
out.write_header_target("_blank");
out.write_header_title("odr");
if (paged_content) {
out.write_header_viewport("width=device-width,user-scalable=yes");
} else {
out.write_header_viewport(
"width=device-width,initial-scale=1.0,user-scalable=yes");
}
auto document_css_file = File(AbsPath(state.config().resource_path)
.join(RelPath("document.css"))
.string());
odr::HtmlResource document_css_resource = HtmlResource::create(
HtmlResourceType::css, "text/css", "document.css", "document.css",
document_css_file, true, false, true);
HtmlResourceLocation document_css_location =
state.config().resource_locator(document_css_resource, state.config());
state.resources().emplace_back(std::move(document_css_resource),
document_css_location);
if (document_css_location.has_value()) {
out.write_header_style(document_css_location.value());
} else {
out.write_header_style_begin();
util::stream::pipe(*document_css_file.stream(), out.out());
out.write_header_style_end();
}
if (document.document_type() == DocumentType::spreadsheet) {
auto spreadsheet_css_file = File(AbsPath(state.config().resource_path)
.join(RelPath("spreadsheet.css"))
.string());
odr::HtmlResource spreadsheet_css_resource = HtmlResource::create(
HtmlResourceType::css, "text/css", "spreadsheet.css", "spreadsheet.css",
spreadsheet_css_file, true, false, true);
HtmlResourceLocation spreadsheet_css_location =
state.config().resource_locator(spreadsheet_css_resource,
state.config());
state.resources().emplace_back(std::move(spreadsheet_css_resource),
spreadsheet_css_location);
if (spreadsheet_css_location.has_value()) {
out.write_header_style(spreadsheet_css_location.value());
} else {
out.write_header_style_begin();
util::stream::pipe(*spreadsheet_css_file.stream(), out.out());
out.write_header_style_end();
}
}
out.write_header_end();
std::string body_clazz = "odr-body";
if (paged_content) {
body_clazz += " odr-background";
}
if (document.document_type() == DocumentType::spreadsheet) {
switch (state.config().spreadsheet_gridlines) {
case HtmlTableGridlines::soft:
body_clazz += " odr-gridlines-soft";
break;
case HtmlTableGridlines::hard:
body_clazz += " odr-gridlines-hard";
break;
case HtmlTableGridlines::none:
default:
body_clazz += " odr-gridlines-none";
break;
}
}
out.write_body_begin(HtmlElementOptions().set_class(body_clazz));
if (paged_content) {
out.write_element_begin("div", HtmlElementOptions().set_class("odr-pages"));
}
}
void back(const Document &document, const WritingState &state) {
HtmlWriter &out = state.out();
const bool paged_content =
(document.document_type() == DocumentType::text &&
state.config().text_document_margin) ||
document.document_type() == DocumentType::presentation ||
document.document_type() == DocumentType::drawing;
if (paged_content) {
out.write_element_end("div");
}
auto document_js_file = File(AbsPath(state.config().resource_path)
.join(RelPath("document.js"))
.string());
odr::HtmlResource document_js_resource = HtmlResource::create(
HtmlResourceType::js, "text/javascript", "document.js", "document.js",
document_js_file, true, false, true);
HtmlResourceLocation document_js_location =
state.config().resource_locator(document_js_resource, state.config());
state.resources().emplace_back(std::move(document_js_resource),
document_js_location);
if (document_js_location.has_value()) {
out.write_script(document_js_location.value());
} else {
out.write_script_begin();
util::stream::pipe(*document_js_file.stream(), out.out());
out.write_script_end();
}
out.write_body_end();
out.write_end();
}
std::string fill_path_variables(const std::string &path,
const std::optional<std::uint32_t> index = {}) {
std::string result = path;
util::string::replace_all(result, "{index}",
index ? std::to_string(*index) : "");
return result;
}
class HtmlFragmentBase {
public:
HtmlFragmentBase(std::string name, const std::size_t index, std::string path,
Document document)
: m_name{std::move(name)}, m_index{index}, m_path{std::move(path)},
m_document{std::move(document)} {}
virtual ~HtmlFragmentBase() = default;
[[nodiscard]] const std::string &name() const { return m_name; }
[[nodiscard]] std::size_t index() const { return m_index; }
[[nodiscard]] const std::string &path() const { return m_path; }
virtual void write_fragment(HtmlWriter &out, WritingState &state) const = 0;
void write_document(HtmlWriter &out, WritingState &state) const {
front(m_document, state);
write_fragment(out, state);
back(m_document, state);
}
protected:
std::string m_name;
std::size_t m_index = 0;
std::string m_path;
Document m_document;
};
class HtmlFragmentView final : public abstract::HtmlView {
public:
HtmlFragmentView(const abstract::HtmlService &service,
std::shared_ptr<HtmlFragmentBase> fragment)
: m_service{&service}, m_fragment{std::move(fragment)} {}
[[nodiscard]] const std::string &name() const override {
return m_fragment->name();
}
[[nodiscard]] std::size_t index() const override {
return m_fragment->index();
}
[[nodiscard]] const std::string &path() const override {
return m_fragment->path();
}
[[nodiscard]] const HtmlConfig &config() const override {
return m_service->config();
}
[[nodiscard]] const abstract::HtmlService &service() const {
return *m_service;
}
HtmlResources write_html(HtmlWriter &out) const override {
HtmlResources resources;
WritingState state(out, service().config(), resources);
m_fragment->write_document(out, state);
return resources;
}
private:
const abstract::HtmlService *m_service = nullptr;
std::shared_ptr<HtmlFragmentBase> m_fragment;
};
class HtmlServiceImpl final : public HtmlService {
public:
HtmlServiceImpl(Document document,
std::vector<std::shared_ptr<HtmlFragmentBase>> fragments,
HtmlConfig config, std::shared_ptr<Logger> logger)
: HtmlService(std::move(config), std::move(logger)),
m_document{std::move(document)}, m_fragments{std::move(fragments)} {
m_views.emplace_back(
std::make_shared<HtmlView>(*this, "document", 0, "document.html"));
for (const auto &fragment : m_fragments) {
if (fragment->name() == "document") {
continue;
}
m_views.emplace_back(std::make_shared<HtmlFragmentView>(*this, fragment));
}
}
const HtmlViews &list_views() const override { return m_views; }
void warmup() const override {
std::lock_guard lock(m_mutex);
if (m_warm) {
return;
}
NullStream null;
HtmlWriter out(null, config());
m_resources = write_document(out);
m_warm = true;
}
bool exists(const std::string &path) const override {
if (std::ranges::any_of(m_views, [&path](const auto &view) {
return view.path() == path;
})) {
return true;
}
warmup();
if (std::ranges::any_of(m_resources, [&path](const auto &pair) {
const auto &[resource, location] = pair;
return location.has_value() && location.value() == path;
})) {
return true;
}
return false;
}
std::string mimetype(const std::string &path) const override {
if (std::ranges::any_of(m_views, [&path](const auto &view) {
return view.path() == path;
})) {
return "text/html";
}
warmup();
for (const auto &[resource, location] : m_resources) {
if (location.has_value() && location.value() == path) {
return resource.mime_type();
}
}
throw FileNotFound("Unknown path: " + path);
}
void write(const std::string &path, std::ostream &out) const override {
for (const auto &view : m_views) {
if (view.path() == path) {
HtmlWriter writer(out, config());
write_html(path, writer);
return;
}
}
warmup();
for (const auto &[resource, location] : m_resources) {
if (location.has_value() && location.value() == path) {
resource.write_resource(out);
return;
}
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_html(const std::string &path,
HtmlWriter &out) const override {
if (path == "document.html") {
return write_document(out);
}
for (const auto &view : m_views) {
if (view.path() == path) {
return view.impl()->write_html(out);
}
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_document(HtmlWriter &out) const {
HtmlResources resources;
WritingState state(out, config(), resources);
front(m_document, state);
for (const auto &fragment : m_fragments) {
fragment->write_fragment(out, state);
}
back(m_document, state);
return resources;
}
protected:
Document m_document;
std::vector<std::shared_ptr<HtmlFragmentBase>> m_fragments;
HtmlViews m_views;
mutable std::mutex m_mutex;
mutable bool m_warm = false;
mutable HtmlResources m_resources;
};
class TextHtmlFragment final : public HtmlFragmentBase {
public:
explicit TextHtmlFragment(std::string name, const std::size_t index,
std::string path, Document document)
: HtmlFragmentBase(std::move(name), index, std::move(path),
std::move(document)) {}
void write_fragment(HtmlWriter &out, WritingState &state) const override {
const Element root = m_document.root_element();
const TextRoot element = root.as_text_root();
if (state.config().text_document_margin) {
auto page_layout = element.page_layout();
page_layout.height = {};
out.write_element_begin(
"div", HtmlElementOptions()
.set_class("odr-page-outer")
.set_style(translate_outer_page_style(page_layout)));
out.write_element_begin(
"div", HtmlElementOptions()
.set_class("odr-page-inner")
.set_style(translate_inner_page_style(page_layout)));
translate_children(element.children(), state);
out.write_element_end("div");
out.write_element_end("div");
} else {
out.write_element_begin("div");
translate_children(element.children(), state);
out.write_element_end("div");
}
}
};
class SlideHtmlFragment final : public HtmlFragmentBase {
public:
explicit SlideHtmlFragment(std::string name, const std::size_t index,
std::string path, Document document,
const Slide &slide)
: HtmlFragmentBase(std::move(name), index, std::move(path),
std::move(document)),
m_slide{slide} {}
void write_fragment(HtmlWriter &, WritingState &state) const override {
translate_slide(m_slide, state);
}
private:
Slide m_slide;
};
class SheetHtmlFragment final : public HtmlFragmentBase {
public:
explicit SheetHtmlFragment(std::string name, const std::size_t index,
std::string path, Document document,
const Sheet &sheet)
: HtmlFragmentBase(std::move(name), index, std::move(path),
std::move(document)),
m_sheet{sheet} {}
void write_fragment(HtmlWriter &, WritingState &state) const override {
translate_sheet(m_sheet, state);
}
private:
Sheet m_sheet;
};
class PageHtmlFragment final : public HtmlFragmentBase {
public:
explicit PageHtmlFragment(std::string name, const std::size_t index,
std::string path, Document document,
const Page &page)
: HtmlFragmentBase(std::move(name), index, std::move(path),
std::move(document)),
m_page{page} {}
void write_fragment(HtmlWriter &, WritingState &state) const override {
translate_page(m_page, state);
}
private:
Page m_page;
};
} // namespace
} // namespace odr::internal::html
namespace odr::internal {
odr::HtmlService html::create_document_service(
const Document &document, const std::string & /*cache_path*/,
HtmlConfig config, std::shared_ptr<Logger> logger) {
std::vector<std::shared_ptr<HtmlFragmentBase>> fragments;
if (document.document_type() == DocumentType::text) {
fragments.push_back(std::make_unique<TextHtmlFragment>(
"document", 0, config.document_output_file_name, document));
} else if (document.document_type() == DocumentType::presentation) {
std::size_t i = 0;
for (Element child : document.root_element().children()) {
Slide slide = child.as_slide();
fragments.push_back(std::make_unique<SlideHtmlFragment>(
slide.name(), i + 1,
fill_path_variables(config.slide_output_file_name, i), document,
slide));
++i;
}
} else if (document.document_type() == DocumentType::spreadsheet) {
std::size_t i = 0;
for (Element child : document.root_element().children()) {
Sheet sheet = child.as_sheet();
fragments.push_back(std::make_unique<SheetHtmlFragment>(
sheet.name(), i + 1,
fill_path_variables(config.sheet_output_file_name, i), document,
sheet));
++i;
}
} else if (document.document_type() == DocumentType::drawing) {
std::size_t i = 0;
for (Element child : document.root_element().children()) {
Page page = child.as_page();
fragments.push_back(std::make_unique<PageHtmlFragment>(
page.name(), i + 1,
fill_path_variables(config.page_output_file_name, i), document,
page));
++i;
}
} else {
throw UnknownDocumentType();
}
return odr::HtmlService(std::make_unique<HtmlServiceImpl>(
document, fragments, std::move(config), std::move(logger)));
}
} // namespace odr::internal