-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstructs.rs
291 lines (266 loc) · 7.84 KB
/
structs.rs
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
use std::collections::HashMap;
/// Represents the different types of HTML elements that the library supports.
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub enum NodeType {
Html,
Head,
Style,
Link,
Script,
Meta,
Title,
Body,
H1,
H2,
H3,
H4,
H5,
H6,
P,
Div,
Strong,
Em,
A,
Ul,
Ol,
Li,
Pre,
Code,
Hr,
Br,
Blockquote,
#[default]
Text,
Comment,
Unknown(String),
}
impl NodeType {
pub fn is_special_tag(&self) -> bool {
use NodeType::*;
matches!(self, Blockquote | Ul | Ol)
}
pub fn from_tag_str(input: &str) -> Self {
use NodeType::*;
match input.to_lowercase().as_str() {
"html" => Html,
"head" => Head,
"style" => Style,
"link" => Link,
"script" => Script,
"meta" => Meta,
"title" => Title,
"body" => Body,
"h1" => H1,
"h2" => H2,
"h3" => H3,
"h4" => H4,
"h5" => H5,
"h6" => H6,
"p" => P,
"div" => Div,
"strong" => Strong,
"em" => Em,
"a" => A,
"ul" => Ul,
"ol" => Ol,
"li" => Li,
"pre" => Pre,
"code" => Code,
"hr" => Hr,
"br" => Br,
"blockquote" => Blockquote,
unknown => Unknown(unknown.to_string()),
}
}
}
/// Represents a node in the HTML tree.
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Node {
pub tag_name: Option<NodeType>,
pub value: Option<String>,
pub attributes: Option<Attributes>,
pub within_special_tag: Option<Vec<NodeType>>,
pub children: Vec<Node>,
}
impl Node {
/// Checks whether the node is within any of the special tags passed in
pub fn is_in_special_tag(&self, tags: &[NodeType]) -> bool {
if let Some(within_special_tag) = &self.within_special_tag {
within_special_tag.iter().any(|tag| tags.contains(tag))
} else {
false
}
}
/// Returns the leading spaces if there is any
/// This is used to format the output of the unordered and ordered lists
pub fn leading_spaces(&self) -> String {
let ul_or_ol = &[NodeType::Ul, NodeType::Ol];
if let Some(within_special_tag) = &self.within_special_tag {
" ".repeat(
(within_special_tag
.iter()
.filter(|tag| ul_or_ol.contains(tag))
.count()
- 1)
* 2,
)
} else {
String::new()
}
}
/// Creates a new Node from tag_name, value, attributes, within_special_tag and children
pub fn new(
tag_name: Option<NodeType>,
value: Option<String>,
attributes: Option<Attributes>,
within_special_tag: Option<Vec<NodeType>>,
children: Vec<Node>,
) -> Self {
Node {
tag_name,
value,
attributes,
within_special_tag,
children,
}
}
}
/// Represents the Attributes of an HTML element.
#[derive(Debug, PartialEq, Eq, Clone, Default)]
pub struct Attributes {
pub(crate) id: Option<String>,
pub(crate) class: Option<String>,
pub(crate) href: Option<String>,
pub(crate) attributes: HashMap<String, AttributeValues>,
}
impl Attributes {
/// Creates a new Attributes struct from id, class and attributes
pub fn new() -> Self {
Attributes {
id: None,
class: None,
href: None,
attributes: HashMap::new(),
}
}
/// Returns the attribute value of the key passed in
pub fn get(&self, key: &str) -> Option<AttributeValues> {
match key {
"id" => self.id.as_ref().map(|id| AttributeValues::from(id.clone())),
"class" => self
.class
.as_ref()
.map(|class| AttributeValues::from(class.clone())),
_ => self.attributes.get(key).cloned(),
}
}
/// Returns the id attribute of the element
pub fn get_id(&self) -> Option<&String> {
self.id.as_ref()
}
/// Returns the class attribute of the element
pub fn get_class(&self) -> Option<&String> {
self.class.as_ref()
}
/// Return the href attribute of the element
pub fn get_href(&self) -> Option<String> {
self.get("href").and_then(|value| match value {
AttributeValues::String(href) => Some(href),
_ => None,
})
}
/// Returns the attributes of the element
pub fn contains(&self, key: &str) -> bool {
match key {
"id" => self.id.is_some(),
"class" => self.class.is_some(),
_ => self.attributes.contains_key(key),
}
}
/// Inserts a new attribute into the element with the key and value passed in
pub fn insert(&mut self, key: String, value: AttributeValues) {
match key.as_str() {
"id" => self.id = Some(value.to_string()),
"class" => self.class = Some(value.to_string()),
_ => {
self.attributes.insert(key, value);
}
}
}
/// Returns whether the element attributes are empty
pub fn is_empty(&self) -> bool {
self.id.is_none() && self.class.is_none() && self.attributes.is_empty()
}
/// Inserts attributes into the element from a tuple vector
pub fn from(vec: Vec<(String, AttributeValues)>) -> Self {
let mut attributes = Attributes::new();
for (key, value) in vec {
attributes.insert(key, value);
}
attributes
}
}
/// Represents the different types of attribute values that the library supports.
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum AttributeValues {
/// Represents a string attribute value.
String(String),
/// Represents a boolean attribute value.
Bool(bool),
/// Represents an integer attribute value.
Number(i32),
}
impl AttributeValues {
/// Creates a new `AttributeValues` instance from a value that can be converted into `AttributeValues`.
///
/// This function uses the `Into` trait to allow for flexible type conversion.
///
/// # Examples
///
/// ```
/// # use html2md_rs::structs::AttributeValues;
/// let string_value = AttributeValues::from("Hello, world!");
/// let bool_value = AttributeValues::from(true);
/// let number_value = AttributeValues::from(42);
/// ```
pub fn from<T: Into<AttributeValues>>(value: T) -> Self {
value.into()
}
}
impl From<String> for AttributeValues {
/// Converts a `String` into an `AttributeValues::String`.
fn from(value: String) -> Self {
AttributeValues::String(value)
}
}
impl From<&str> for AttributeValues {
/// Converts a string slice (`&str`) into an `AttributeValues::String`.
fn from(value: &str) -> Self {
AttributeValues::String(value.to_string())
}
}
impl From<bool> for AttributeValues {
/// Converts a `bool` into an `AttributeValues::Bool`.
fn from(value: bool) -> Self {
AttributeValues::Bool(value)
}
}
impl From<i32> for AttributeValues {
/// Converts an `i32` into an `AttributeValues::Number`.
fn from(value: i32) -> Self {
AttributeValues::Number(value)
}
}
impl std::fmt::Display for AttributeValues {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
AttributeValues::String(value) => write!(f, "{}", value),
AttributeValues::Bool(value) => write!(f, "{}", value),
AttributeValues::Number(value) => write!(f, "{}", value),
}
}
}
#[derive(Debug, Default)]
pub struct ToMdConfig {
pub ignore_rendering: Vec<NodeType>,
}