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
use html_parser::{Dom, Node, Result};
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter, Write};
#[derive(Clone, Eq, PartialEq, Debug)]
pub struct Element {
pub tag: String,
pub data: String,
pub attrs: HashMap<String, String>,
pub childs: Vec<Element>,
}
impl Display for Element {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
match self.tag.as_str() {
"" => {
f.write_str("`")?;
f.write_str(&self.data)?;
f.write_str("`")?;
}
_ => {
f.write_str("<")?;
f.write_str(&self.tag)?;
for (k, v) in &self.attrs {
f.write_str(" ")?;
f.write_str(k)?;
f.write_str("=\"")?;
f.write_str(v)?;
f.write_str("\"")?;
}
f.write_str(">")?;
for x in &self.childs {
std::fmt::Display::fmt(x, f)?;
}
f.write_str("</")?;
f.write_str(&self.tag)?;
f.write_str(">")?;
}
}
return Ok(());
}
}
pub fn as_element(args: &Vec<Node>) -> Vec<Element> {
let mut els = vec![];
for x in args {
let mut el = Element {
tag: "".to_string(),
data: "".to_string(),
attrs: HashMap::new(),
childs: vec![],
};
match x {
Node::Text(txt) => {
if txt.is_empty() {
continue;
}
let t = txt.trim();
if t.starts_with("`") || t.ends_with("`") {
el.data = t.replace("`", "").to_string();
} else {
el.data = t.to_string();
}
el.data = el.data.replace("``", "").to_string();
}
Node::Element(element) => {
el.tag = element.name.to_string();
if element.id.is_some() {
el.attrs.insert(
"id".to_string(),
element.id.as_ref().unwrap_or(&String::new()).clone(),
);
}
for (k, v) in &element.attributes {
el.attrs
.insert(k.clone(), v.as_ref().unwrap_or(&String::new()).clone());
}
if !element.children.is_empty() {
let childs = as_element(&element.children);
el.childs = childs;
}
}
Node::Comment(comment) => {
}
}
els.push(el);
}
els
}
pub fn load_html(html: &str) -> Result<Vec<Element>> {
let dom = Dom::parse(html)?;
let els = as_element(&dom.children);
return Ok(els);
}
impl Element {
pub fn child_strings(&self) -> Vec<&str> {
let mut elements = vec![];
for x in &self.childs {
if x.tag.eq("") {
elements.push(x.data.as_str());
}
let v = x.child_strings();
for x in v {
elements.push(x);
}
}
elements
}
pub fn child_string_cup(&self) -> usize {
let mut u = 0;
for x in self.child_strings() {
u += x.len();
}
u
}
}
#[cfg(test)]
mod test {
use crate::codegen::html_loader::load_html;
#[test]
fn test_parser() {
let nodes = load_html(r#"<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "https://raw.githubusercontent.com/rbatis/rbatis/master/rbatis-codegen/mybatis-3-mapper.dtd">
<mapper>
<insert id="insert">
'insert into biz_activity'
<foreach collection="arg" index="key" item="item" open="(" close=")" separator=",">
'${key}'
</foreach>
'values'
<foreach collection="arg" index="key" item="item" open="(" close=")" separator=",">
'${item}'
</foreach>
</insert></mapper>"#).unwrap();
println!("{:?}", nodes);
}
#[test]
fn test_item() {
let nodes = load_html(
r#"
<insert id="insert">
'insert into biz_activity'
<foreach collection="arg" index="key" item="item" open="(" close=")" separator=",">
'${key}'
</foreach>
'values'
<foreach collection="arg" index="key" item="item" open="(" close=")" separator=",">
'${item}'
</foreach>
</insert>"#,
)
.unwrap();
println!("{:?}", nodes);
}
}