-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathhtml_writer.cpp
More file actions
312 lines (249 loc) · 7.2 KB
/
Copy pathhtml_writer.cpp
File metadata and controls
312 lines (249 loc) · 7.2 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
#include <odr/internal/html/html_writer.hpp>
#include <odr/internal/html/common.hpp>
#include <algorithm>
#include <cstring>
#include <stdexcept>
namespace odr::internal::html {
namespace {
template <class... Ts> struct overloaded : Ts... {
using Ts::operator()...;
};
template <class... Ts> overloaded(Ts...) -> overloaded<Ts...>;
bool is_empty(const HtmlWritable &writable) {
return std::visit(
overloaded{
[](const char *str) { return std::strcmp(str, "") == 0; },
[](const std::string &str) { return str.empty(); },
[](const HtmlWriteCallback &) { return false; },
},
writable);
}
void write_writable(std::ostream &out, const HtmlWritable &writable) {
std::visit(overloaded{
[&out](const char *str) { out << str; },
[&out](const std::string &str) { out << str; },
[&out](const HtmlWriteCallback &clb) { clb(out); },
},
writable);
}
void write_key_value(std::ostream &out, const HtmlWritable &key,
const HtmlWritable &value) {
out << " ";
write_writable(out, key);
out << "=\"";
write_writable(out, value);
out << "\"";
}
void write_attributes(std::ostream &out, const HtmlAttributes &attributes) {
std::visit(overloaded{
[&out](const HtmlAttributesVector &vector) {
for (const auto &[key, value] : vector) {
write_key_value(out, key, value);
}
},
[&out](const HtmlAttributeCallback &callback) {
callback([&out](const HtmlWritable &key,
const HtmlWritable &value) {
write_key_value(out, key, value);
});
},
},
attributes);
}
void write_element_options(std::ostream &out,
const HtmlElementOptions &options) {
if (options.clazz && !is_empty(*options.clazz)) {
out << " class=\"";
write_writable(out, *options.clazz);
out << "\"";
}
if (options.style && !is_empty(*options.style)) {
out << " style=\"";
write_writable(out, *options.style);
out << "\"";
}
if (options.attributes) {
write_attributes(out, *options.attributes);
}
if (options.extra) {
out << " ";
write_writable(out, *options.extra);
}
}
} // namespace
HtmlElementOptions &HtmlElementOptions::set_inline(const bool is_inline) {
inline_element = is_inline;
return *this;
}
HtmlElementOptions &
HtmlElementOptions::set_close_type(const HtmlCloseType _close_type) {
close_type = _close_type;
return *this;
}
HtmlElementOptions &
HtmlElementOptions::set_attributes(std::optional<HtmlAttributes> _attributes) {
attributes = std::move(_attributes);
return *this;
}
HtmlElementOptions &
HtmlElementOptions::set_style(std::optional<HtmlWritable> _style) {
style = std::move(_style);
return *this;
}
HtmlElementOptions &
HtmlElementOptions::set_class(std::optional<HtmlWritable> _class) {
clazz = std::move(_class);
return *this;
}
HtmlElementOptions &
HtmlElementOptions::set_extra(std::optional<HtmlWritable> _extra) {
extra = std::move(_extra);
return *this;
}
HtmlWriter::HtmlWriter(std::ostream &out, const bool format,
const std::uint8_t indent,
const std::uint32_t current_indent)
: m_out{&out}, m_format{format}, m_indent(indent, ' '),
m_current_indent{current_indent} {}
HtmlWriter::HtmlWriter(std::ostream &out, const HtmlConfig &config)
: HtmlWriter{out, config.format_html, config.html_indent} {}
void HtmlWriter::write_begin() {
out() << "<!DOCTYPE html>\n";
out() << "<html>";
}
void HtmlWriter::write_end() {
write_new_line();
out() << "</html>";
}
void HtmlWriter::write_header_begin() {
write_new_line();
++m_current_indent;
out() << "<head>";
}
void HtmlWriter::write_header_end() {
--m_current_indent;
write_new_line();
out() << "</head>";
}
void HtmlWriter::write_header_title(const std::string &title) {
write_new_line();
out() << "<title>" << title << "</title>";
}
void HtmlWriter::write_header_meta(const std::string &name,
const std::string &content) {
write_new_line();
out() << R"(<meta name=")";
out() << name;
out() << R"(" content=")";
out() << content;
out() << R"("/>)";
}
void HtmlWriter::write_header_viewport(const std::string &viewport) {
write_header_meta("viewport", viewport);
}
void HtmlWriter::write_header_target(const std::string &target) {
write_new_line();
out() << "<base target=\"";
out() << target;
out() << "\"/>";
}
void HtmlWriter::write_header_charset(const std::string &charset) {
write_new_line();
out() << "<meta charset=\"";
out() << charset;
out() << "\"/>";
}
void HtmlWriter::write_header_style(const std::string &href) {
write_new_line();
out() << R"(<link rel="stylesheet" href=")";
out() << href;
out() << "\"/>";
}
void HtmlWriter::write_header_style_begin() {
write_new_line();
++m_current_indent;
out() << "<style>";
}
void HtmlWriter::write_header_style_end() {
--m_current_indent;
write_new_line();
out() << "</style>";
}
void HtmlWriter::write_script(const std::string &src) {
write_new_line();
out() << R"(<script type="text/javascript" src=")" << src << "\"></script>";
}
void HtmlWriter::write_script_begin() {
write_new_line();
++m_current_indent;
out() << "<script>";
}
void HtmlWriter::write_script_end() {
--m_current_indent;
write_new_line();
out() << "</script>";
}
void HtmlWriter::write_body_begin(const HtmlElementOptions &options) {
write_new_line();
++m_current_indent;
out() << "<body";
write_element_options(out(), options);
out() << ">";
}
void HtmlWriter::write_body_end() {
--m_current_indent;
write_new_line();
out() << "</body>";
}
void HtmlWriter::write_element_begin(const std::string &name,
const HtmlElementOptions &options) {
write_new_line();
if (options.close_type == HtmlCloseType::standard) {
++m_current_indent;
m_stack.push_back({name, options.inline_element});
}
out() << "<" << name;
write_element_options(out(), options);
if (options.close_type == HtmlCloseType::trailing) {
out() << "/>";
} else {
out() << ">";
}
}
void HtmlWriter::write_element_end(const std::string &name) {
--m_current_indent;
write_new_line();
if (m_stack.empty()) {
throw std::logic_error("stack is empty");
}
if (m_stack.back().name != name) {
throw std::invalid_argument("names do not match");
}
m_stack.pop_back();
out() << "</" << name << ">";
}
bool HtmlWriter::is_inline_mode() const {
return std::ranges::any_of(m_stack, [](const StackElement &element) {
return element.inline_element;
});
}
void HtmlWriter::write_new_line() {
if (!m_format) {
return;
}
if (is_inline_mode()) {
return;
}
out() << '\n';
for (std::uint32_t i = 0; i < m_current_indent; ++i) {
out() << m_indent;
}
}
void HtmlWriter::write_raw(const HtmlWritable &writable, const bool new_line) {
if (new_line) {
write_new_line();
}
write_writable(out(), writable);
}
std::ostream &HtmlWriter::out() { return *m_out; }
} // namespace odr::internal::html