-
-
Notifications
You must be signed in to change notification settings - Fork 181
Expand file tree
/
Copy pathinline.rs
More file actions
221 lines (194 loc) · 7.56 KB
/
inline.rs
File metadata and controls
221 lines (194 loc) · 7.56 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
use crate::{
markdown::text_style::{Color, TextStyle},
theme::raw::{ParseColorError, RawColor},
};
use std::{borrow::Cow, str, str::Utf8Error};
use tl::Attributes;
pub(crate) struct InlineHtmlParseOptions {
pub(crate) strict: bool,
}
impl Default for InlineHtmlParseOptions {
fn default() -> Self {
Self { strict: true }
}
}
#[derive(Default)]
pub(crate) struct InlineHtmlParser {
options: InlineHtmlParseOptions,
}
impl InlineHtmlParser {
pub(crate) fn parse(self, input: &str) -> Result<HtmlInline, ParseInlineHtmlError> {
if input.starts_with("</") {
if input.starts_with("</span") {
return Ok(HtmlInline::CloseTag { tag: HtmlInlineTag::Span });
} else if input.starts_with("</sup") {
return Ok(HtmlInline::CloseTag { tag: HtmlInlineTag::Sup });
} else {
return Err(ParseInlineHtmlError::UnsupportedClosingTag(input.to_string()));
}
}
let dom = tl::parse(input, Default::default())?;
let top = dom.children().iter().next().ok_or(ParseInlineHtmlError::NoTags)?;
let node = top.get(dom.parser()).expect("failed to get");
let tag = node.as_tag().ok_or(ParseInlineHtmlError::NoTags)?;
let (output_tag, base_style) = match tag.name().as_bytes() {
b"span" => (HtmlInlineTag::Span, TextStyle::default()),
b"sup" => (HtmlInlineTag::Sup, TextStyle::default().superscript()),
_ => return Err(ParseInlineHtmlError::UnsupportedHtml),
};
let style = self.parse_attributes(tag.attributes())?;
Ok(HtmlInline::OpenTag { style: style.merged(&base_style), tag: output_tag })
}
fn parse_attributes(&self, attributes: &Attributes) -> Result<TextStyle<RawColor>, ParseInlineHtmlError> {
let mut style = TextStyle::default();
for (name, value) in attributes.iter() {
let value = value.unwrap_or(Cow::Borrowed(""));
match name.as_ref() {
"style" => self.parse_css_attribute(&value, &mut style)?,
"class" => {
style = style.fg_color(RawColor::ForegroundClass(value.to_string()));
style = style.bg_color(RawColor::BackgroundClass(value.to_string()));
}
_ => {
if self.options.strict {
return Err(ParseInlineHtmlError::UnsupportedTagAttribute(name.to_string()));
}
}
}
}
Ok(style)
}
fn parse_css_attribute(
&self,
attribute: &str,
style: &mut TextStyle<RawColor>,
) -> Result<(), ParseInlineHtmlError> {
for attribute in attribute.split(';') {
let attribute = attribute.trim();
if attribute.is_empty() {
continue;
}
let (key, value) = attribute.split_once(':').ok_or(ParseInlineHtmlError::NoColonInAttribute)?;
let key = key.trim();
let value = value.trim();
match key {
"color" => style.colors.foreground = Some(Self::parse_color(value)?),
"background-color" => style.colors.background = Some(Self::parse_color(value)?),
_ => {
if self.options.strict {
return Err(ParseInlineHtmlError::UnsupportedCssAttribute(key.into()));
}
}
}
}
Ok(())
}
fn parse_color(input: &str) -> Result<RawColor, ParseInlineHtmlError> {
if input.starts_with('#') {
let color = input.strip_prefix('#').unwrap().parse()?;
if matches!(color, RawColor::Color(Color::Rgb { .. })) { Ok(color) } else { Ok(input.parse()?) }
} else {
let color = input.parse::<RawColor>()?;
if matches!(color, RawColor::Color(Color::Rgb { .. })) {
Err(ParseInlineHtmlError::InvalidColor("missing '#' in rgb color".into()))
} else {
Ok(color)
}
}
}
}
#[derive(Debug, PartialEq)]
pub(crate) enum HtmlInline {
OpenTag { style: TextStyle<RawColor>, tag: HtmlInlineTag },
CloseTag { tag: HtmlInlineTag },
}
#[derive(Clone, Debug, PartialEq)]
pub(crate) enum HtmlInlineTag {
Span,
Sup,
}
#[derive(Debug, thiserror::Error)]
pub(crate) enum ParseInlineHtmlError {
#[error("parsing html failed: {0}")]
ParsingHtml(#[from] tl::ParseError),
#[error("no html tags found")]
NoTags,
#[error("non utf8 content: {0}")]
NotUtf8(#[from] Utf8Error),
#[error("attribute has no ':'")]
NoColonInAttribute,
#[error("invalid color: {0}")]
InvalidColor(String),
#[error("invalid css attribute: {0}")]
UnsupportedCssAttribute(String),
#[error("HTML can only contain span and sup tags")]
UnsupportedHtml,
#[error("unsupported tag attribute: {0}")]
UnsupportedTagAttribute(String),
#[error("unsupported closing tag: {0}")]
UnsupportedClosingTag(String),
}
impl From<ParseColorError> for ParseInlineHtmlError {
fn from(e: ParseColorError) -> Self {
Self::InvalidColor(e.to_string())
}
}
#[cfg(test)]
mod tests {
use super::*;
use rstest::rstest;
#[test]
fn parse_style() {
let tag = InlineHtmlParser::default()
.parse(r#"<span style="color: red; background-color: black">"#)
.expect("parse failed");
let HtmlInline::OpenTag { style, tag: HtmlInlineTag::Span } = tag else { panic!("not an open tag") };
assert_eq!(style, TextStyle::default().bg_color(Color::Black).fg_color(Color::Red));
}
#[test]
fn parse_sup() {
let tag = InlineHtmlParser::default().parse(r#"<sup>"#).expect("parse failed");
let HtmlInline::OpenTag { style, tag: HtmlInlineTag::Sup } = tag else { panic!("not an open tag") };
assert_eq!(style, TextStyle::default().superscript());
}
#[test]
fn parse_class() {
let tag = InlineHtmlParser::default().parse(r#"<span class="foo">"#).expect("parse failed");
let HtmlInline::OpenTag { style, tag: HtmlInlineTag::Span } = tag else { panic!("not an open tag") };
assert_eq!(
style,
TextStyle::default()
.bg_color(RawColor::BackgroundClass("foo".into()))
.fg_color(RawColor::ForegroundClass("foo".into()))
);
}
#[rstest]
#[case::span("</span>", HtmlInlineTag::Span)]
#[case::sup("</sup>", HtmlInlineTag::Sup)]
fn parse_end_tag(#[case] input: &str, #[case] tag: HtmlInlineTag) {
let inline = InlineHtmlParser::default().parse(input).expect("parse failed");
assert_eq!(inline, HtmlInline::CloseTag { tag });
}
#[rstest]
#[case::invalid_start_tag("<div>")]
#[case::invalid_end_tag("</div>")]
#[case::invalid_attribute("<span foo=\"bar\">")]
#[case::invalid_attribute("<span style=\"bleh: 42\"")]
#[case::invalid_color("<span style=\"color: 42\"")]
fn parse_invalid_html(#[case] input: &str) {
InlineHtmlParser::default().parse(input).expect_err("parse succeeded");
}
#[rstest]
#[case::rgb("#ff0000", Color::Rgb{r: 255, g: 0, b: 0})]
#[case::red("red", Color::Red)]
fn parse_color(#[case] input: &str, #[case] expected: Color) {
let color = InlineHtmlParser::parse_color(input).expect("parse failed");
assert_eq!(color, expected.into());
}
#[rstest]
#[case::rgb("ff0000")]
#[case::red("#red")]
fn parse_invalid_color(#[case] input: &str) {
InlineHtmlParser::parse_color(input).expect_err("parse succeeded");
}
}