-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathtext_file.cpp
More file actions
188 lines (149 loc) · 5.78 KB
/
Copy pathtext_file.cpp
File metadata and controls
188 lines (149 loc) · 5.78 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
#include <odr/internal/html/text_file.hpp>
#include <odr/exceptions.hpp>
#include <odr/file.hpp>
#include <odr/html.hpp>
#include <odr/internal/common/null_stream.hpp>
#include <odr/internal/common/path.hpp>
#include <odr/internal/html/common.hpp>
#include <odr/internal/html/html_service.hpp>
#include <odr/internal/html/html_writer.hpp>
#include <odr/internal/util/stream_util.hpp>
#include <sstream>
namespace odr::internal::html {
namespace {
class HtmlServiceImpl final : public HtmlService {
public:
HtmlServiceImpl(TextFile text_file, HtmlConfig config,
std::shared_ptr<Logger> logger)
: HtmlService(std::move(config), std::move(logger)),
m_text_file{std::move(text_file)} {
m_views.emplace_back(
std::make_shared<HtmlView>(*this, "text", 0, "text.html"));
}
void warmup() const override {}
[[nodiscard]] const HtmlViews &list_views() const override { return m_views; }
[[nodiscard]] bool exists(const std::string &path) const override {
if (path == "text.html") {
return true;
}
return false;
}
[[nodiscard]] std::string mimetype(const std::string &path) const override {
if (path == "text.html") {
return "text/html";
}
throw FileNotFound("Unknown path: " + path);
}
void write(const std::string &path, std::ostream &out) const override {
if (path == "text.html") {
HtmlWriter writer(out, config());
write_text(writer);
return;
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_html(const std::string &path,
HtmlWriter &out) const override {
if (path == "text.html") {
return write_text(out);
}
throw FileNotFound("Unknown path: " + path);
}
HtmlResources write_text(HtmlWriter &out) const {
HtmlResources resources;
out.write_begin();
out.write_header_begin();
// TODO charset
out.write_header_charset("UTF-8");
out.write_header_target("_blank");
out.write_header_title("odr");
out.write_header_viewport(
"width=device-width,initial-scale=1.0,user-scalable=yes");
auto css_file = File(
AbsPath(config().resource_path).join(RelPath("text.css")).string());
odr::HtmlResource document_css_resource =
HtmlResource::create(HtmlResourceType::css, "text/css", "text.css",
"text.css", css_file, true, false, true);
HtmlResourceLocation document_css_location =
config().resource_locator(document_css_resource, config());
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(*css_file.stream(), out.out());
out.write_header_style_end();
}
out.write_header_end();
out.write_body_begin();
out.write_element_begin("div", HtmlElementOptions().set_class("odr-text"));
out.write_element_begin("div",
HtmlElementOptions().set_class("odr-text-nr"));
std::unique_ptr<std::istream> in = m_text_file.stream();
for (std::uint32_t line = 1; !in->eof(); ++line) {
out.write_element_begin("div", HtmlElementOptions().set_inline(true));
out.out() << line;
out.write_element_end("div");
NullStream ss_out;
util::stream::pipe_line(*in, ss_out, false);
}
out.write_element_end("div");
out.write_element_begin("div",
HtmlElementOptions().set_attributes(
[&](const HtmlAttributeWriterCallback &clb) {
clb("class", "odr-text-body odr-text-wrap");
if (config().editable) {
clb("contenteditable", "true");
}
}));
in = m_text_file.stream();
while (!in->eof()) {
out.write_element_begin("div", HtmlElementOptions().set_inline(true));
std::ostringstream ss_out;
util::stream::pipe_line(*in, ss_out, false);
if (const std::string &line = ss_out.str(); line.empty()) {
out.write_element_begin(
"br", HtmlElementOptions().set_close_type(HtmlCloseType::trailing));
} else {
out.out() << escape_text(ss_out.str());
}
out.write_element_end("div");
}
out.write_element_end("div");
out.write_element_end("div");
auto js_file =
File(AbsPath(config().resource_path).join(RelPath("text.js")).string());
odr::HtmlResource document_js_resource =
HtmlResource::create(HtmlResourceType::js, "text/javascript", "text.js",
"text.js", js_file, true, false, true);
HtmlResourceLocation document_js_location =
config().resource_locator(document_js_resource, config());
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(*js_file.stream(), out.out());
out.write_script_end();
}
out.write_body_end();
out.write_end();
return resources;
}
protected:
TextFile m_text_file;
HtmlViews m_views;
};
} // namespace
} // namespace odr::internal::html
namespace odr::internal {
HtmlService
html::create_text_service(const TextFile &text_file,
[[maybe_unused]] const std::string &cache_path,
HtmlConfig config, std::shared_ptr<Logger> logger) {
return odr::HtmlService(std::make_unique<HtmlServiceImpl>(
text_file, std::move(config), std::move(logger)));
}
} // namespace odr::internal