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
use convert_case::{Case, Casing};
use dioxus_rsx::{
BodyNode, CallBody, Component, Element, ElementAttr, ElementAttrNamed, IfmtInput,
};
pub use html_parser::{Dom, Node};
use proc_macro2::{Ident, Span};
use syn::{punctuated::Punctuated, LitStr};
pub fn rsx_from_html(dom: &Dom) -> CallBody {
CallBody {
roots: dom.children.iter().filter_map(rsx_node_from_html).collect(),
}
}
pub fn rsx_node_from_html(node: &Node) -> Option<BodyNode> {
match node {
Node::Text(text) => Some(BodyNode::Text(ifmt_from_text(text))),
Node::Element(el) => {
let el_name = el.name.to_case(Case::Snake);
let el_name = Ident::new(el_name.as_str(), Span::call_site());
let mut attributes: Vec<_> = el
.attributes
.iter()
.map(|(name, value)| {
let ident = if matches!(name.as_str(), "for" | "async" | "type" | "as") {
Ident::new_raw(name.as_str(), Span::call_site())
} else {
let new_name = name.to_case(Case::Snake);
Ident::new(new_name.as_str(), Span::call_site())
};
ElementAttrNamed {
el_name: el_name.clone(),
attr: ElementAttr::AttrText {
value: ifmt_from_text(value.as_deref().unwrap_or("false")),
name: ident,
},
}
})
.collect();
let class = el.classes.join(" ");
if !class.is_empty() {
attributes.push(ElementAttrNamed {
el_name: el_name.clone(),
attr: ElementAttr::AttrText {
name: Ident::new("class", Span::call_site()),
value: ifmt_from_text(&class),
},
});
}
if let Some(id) = &el.id {
attributes.push(ElementAttrNamed {
el_name: el_name.clone(),
attr: ElementAttr::AttrText {
name: Ident::new("id", Span::call_site()),
value: ifmt_from_text(id),
},
});
}
let children = el.children.iter().filter_map(rsx_node_from_html).collect();
Some(BodyNode::Element(Element {
name: el_name,
children,
attributes,
_is_static: false,
key: None,
brace: Default::default(),
}))
}
Node::Comment(_) => None,
}
}
pub fn collect_svgs(children: &mut [BodyNode], out: &mut Vec<BodyNode>) {
for child in children {
match child {
BodyNode::Component(comp) => collect_svgs(&mut comp.children, out),
BodyNode::Element(el) if el.name == "svg" => {
let mut segments = Punctuated::new();
segments.push(Ident::new("icons", Span::call_site()).into());
let new_name: Ident = Ident::new(&format!("icon_{}", out.len()), Span::call_site());
segments.push(new_name.clone().into());
let mut new_comp = BodyNode::Component(Component {
name: syn::Path {
leading_colon: None,
segments,
},
prop_gen_args: None,
fields: vec![],
children: vec![],
manual_props: None,
brace: Default::default(),
});
std::mem::swap(child, &mut new_comp);
out.push(new_comp);
}
BodyNode::Element(el) => collect_svgs(&mut el.children, out),
_ => {}
}
}
}
fn ifmt_from_text(text: &str) -> IfmtInput {
IfmtInput {
source: Some(LitStr::new(text, Span::call_site())),
segments: vec![],
}
}