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
use base64::{Engine as _, engine::general_purpose};
use std::{borrow::Cow, fmt::Write as _};
use html5ever::{Attribute, ParseOpts, parse_document, tendril::TendrilSink};
use markup5ever_rcdom::{Handle, NodeData, RcDom};
#[derive(Debug, Default)]
struct Context {
tag_stack: Vec<Option<Box<str>>>,
output: String,
}
#[must_use]
#[allow(clippy::missing_panics_doc)]
pub fn parse_html(html: &str) -> String {
let dom = parse_document(RcDom::default(), ParseOpts::default())
.from_utf8()
.read_from(&mut html.as_bytes())
// SAFETY: we are reading from a string
.unwrap();
let mut ctx = Context::default();
walk(&dom.document, &mut ctx);
cleanup(&ctx.output)
}
fn cleanup(output: &str) -> String {
output.trim().to_owned()
}
#[allow(clippy::too_many_lines)]
fn walk(node: &Handle, ctx: &mut Context) {
match &node.data {
NodeData::Document
| NodeData::Doctype { .. }
| NodeData::ProcessingInstruction { .. }
| NodeData::Comment { .. } => walk_descendants(node, ctx, None),
NodeData::Text { contents } => {
// Consider:
// - inside <pre> or <code> tags
// - trimmed len == 0
// - last char is a space or a newline
// - escaping text
// - remove excess whitespace, newlines, and carriage returns
let text = contents.borrow();
let escaped_text = escape_html(text.trim());
ctx.output.push_str(&escaped_text);
}
NodeData::Element { name, attrs, .. } => {
// Consider:
// - inside <pre>
let tag_name = name.local.as_ref();
match tag_name {
"hr" | "q" | "cite" | "details" | "summary" | "pre" | "code" | "sub" | "sup"
| "table" | "iframe" => {
todo!("{tag_name}")
}
"div" | "section" | "header" | "footer" => {
ctx.output.push_str("\n\n");
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push_str("\n\n");
}
"li" => {
let mut tag_iter = ctx.tag_stack.iter().rev().filter_map(|t| {
let t = t.as_deref();
if matches!(t, Some("ol" | "ul" | "menu")) {
t
} else {
None
}
});
let parent_tag = tag_iter.next();
let tag_level = tag_iter.count();
match parent_tag {
Some("ol") => {
ctx.output
.write_fmt(format_args!("{: <width$}+ ", "", width = tag_level * 2))
// SAFETY: we are writing to a String
.unwrap();
}
Some("ul" | "menu") | None => {
ctx.output
.write_fmt(format_args!("{: <width$}- ", "", width = tag_level * 2))
// SAFETY: we are writing to a String
.unwrap();
}
_ => unreachable!(),
}
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push('\n');
}
"ol" | "ul" | "menu" => {
ctx.output.push('\n');
if ctx
.tag_stack
.iter()
.rev()
.filter_map(|t| {
let t = t.as_deref();
if matches!(t, Some("ol" | "ul" | "menu")) {
t
} else {
None
}
})
.count()
== 0
{
ctx.output.push('\n');
}
// TODO: extra newline if not inside a list
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push_str("\n\n");
}
"s" | "del" => {
// TODO: handle spaces
ctx.output.push_str("#strike[");
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push(']');
}
"b" | "strong" => {
// TODO: handle spaces
ctx.output.push('*');
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push('*');
}
"i" | "em" => {
// TODO: handle spaces
ctx.output.push('_');
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push('_');
}
"u" | "ins" => {
// TODO: handle spaces
ctx.output.push_str("#underline[");
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push(']');
}
"blockquote" => {
ctx.output.push_str("\n\n#quote(block: true)[\n");
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push_str("\n]\n\n");
}
level @ ("h1" | "h2" | "h3" | "h4" | "h5" | "h6") => {
let level = usize::from(level.as_bytes()[1] - b'0');
ctx.output
.write_fmt(format_args!("{:=<width$} ", "", width = level))
// SAFETY: we are writing to a String
.unwrap();
walk_descendants(node, ctx, Some(Box::from(tag_name)));
if let Some(id) = get_attr_value(&attrs.borrow(), "id") {
ctx.output
// TODO: escape?
.write_fmt(format_args!(" <{id}>\n"))
// SAFETY: we are writing to a String
.unwrap();
}
}
"html" | "head" | "body" => walk_descendants(node, ctx, Some(Box::from(tag_name))),
"p" => {
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push_str("\n\n");
}
"br" => ctx.output.push_str("\\\n"),
"a" => {
if let Some(href) = get_attr_value(&attrs.borrow(), "href") {
ctx.output
// TODO: escape href ?
.write_fmt(format_args!(r#"#link("{href}")["#))
// SAFETY: we are writing to a string
.unwrap();
walk_descendants(node, ctx, Some(Box::from(tag_name)));
ctx.output.push(']');
} else {
walk_descendants(node, ctx, Some(Box::from(tag_name)));
}
}
"img" => {
let attrs = attrs.borrow();
// TODO: check if the escaping is correct
let src = get_attr_value(&attrs, "src").map(|x| {
let cleared = x.chars().filter(|c| !c.is_whitespace()).collect::<String>();
if let Some(stripped) = cleared.strip_prefix("data:") {
let uri_scheme: Vec<&str> = stripped.split(';').collect();
assert!(
uri_scheme[0].starts_with("image/"),
"Image tag `src` in URI scheme isn't image data."
);
let data_part: Vec<&str> =
uri_scheme.last().unwrap().split(',').collect();
match data_part[0] {
"base64" => {
let data = general_purpose::STANDARD
.decode(data_part[1])
.expect("Image tag `src` in URI scheme doesn't contain valid base64");
return format!(
"bytes(({}))",
data.iter()
.map(std::string::ToString::to_string)
.collect::<Vec<_>>()
.join(", ")
);
}
"image/svg+xml" => todo!(),
_ => panic!(
"Image tag `src` URI scheme encoding of `{}` isn't supported.",
data_part[0]
),
}
}
format!("\"{}\"", escape_quotes(x))
});
let alt = get_attr_value(&attrs, "alt");
match (src, alt) {
(Some(src), Some(alt)) => {
ctx.output
.write_fmt(format_args!(
r#"#figure(caption: [{alt}], image(alt: "{}", {}))"#,
escape_quotes(alt),
&src
))
// SAFETY: we are writing to a string
.unwrap();
}
(Some(src), None) => {
// TODO: test the escaping
ctx.output
.write_fmt(format_args!(r"#figure(caption: none, image({}))", &src))
// SAFETY: we are writing to a string
.unwrap();
}
_ => {}
}
}
_ => {
todo!()
}
}
}
}
}
fn get_attr_value<'a>(attrs: &'a [Attribute], name: &str) -> Option<&'a str> {
attrs
.iter()
.find(|attr| attr.name.local.as_ref() == name)
.map(|attr| attr.value.as_ref())
}
fn walk_descendants(node: &Handle, ctx: &mut Context, tag_name: Option<Box<str>>) {
ctx.tag_stack.push(tag_name);
for child in node.children.borrow().iter() {
walk(child, ctx);
}
ctx.tag_stack.pop();
}
fn escape_quotes(html: &str) -> Cow<'_, str> {
if !html.contains('"') {
return Cow::Borrowed(html);
}
let mut escaped = vec![];
let bytes = html.as_bytes();
for &ch in bytes {
if matches!(ch, b'"') {
escaped.push(b'\\');
}
escaped.push(ch);
}
Cow::Owned(
String::from_utf8(escaped)
// SAFETY: we started with valid utf8
.unwrap(),
)
}
fn escape_html(html: &str) -> Cow<'_, str> {
if !html.contains(['*', '_', '<', '>']) && !html.starts_with(['=', '-', '+']) {
return Cow::Borrowed(html);
}
let mut escaped = vec![];
let bytes = html.as_bytes();
if matches!(bytes, [b'=' | b'-' | b'+', ..]) {
escaped.push(b'\\');
}
for &ch in bytes {
if matches!(ch, b'*' | b'_' | b'<' | b'>') {
escaped.push(b'\\');
}
escaped.push(ch);
}
Cow::Owned(
String::from_utf8(escaped)
// SAFETY: we started with valid utf8
.unwrap(),
)
}