can now convert from html to ir and back

This commit is contained in:
2017-08-25 17:27:57 +02:00
parent 10d910d8dc
commit a45eda2bd4
5 changed files with 363 additions and 58 deletions

View File

@@ -1,7 +1,9 @@
use std::collections::HashMap;
use std;
use css;
struct Attr {
pub struct Attr {
attrs: HashMap<String, String>,
}
@@ -10,27 +12,32 @@ struct EData {
attr: Attr,
}
struct SData {
attr: Attr,
content: css::Stylesheet,
}
enum NType {
Text(String),
Comment(String),
Element(EData),
Stylesheet(SData)
}
struct Node {
pub struct Node {
children: Vec<Node>,
ntype: NType,
}
fn text(d: String) -> Node {
pub fn text(d: String) -> Node {
Node { children: Vec::new(), ntype: NType::Text(d) }
}
fn comment(d: String) -> Node {
pub fn comment(d: String) -> Node {
Node { children: Vec::new(), ntype: NType::Comment(d) }
}
fn elem(name: String, attr: Attr, children: Vec<Node>) -> Node {
pub fn elem(name: String, attr: Attr, children: Vec<Node>) -> Node {
Node {
children: children,
ntype: NType::Element(EData {
@@ -40,20 +47,35 @@ fn elem(name: String, attr: Attr, children: Vec<Node>) -> Node {
}
}
pub fn attr(attrs: HashMap<String, String>) -> Attr {
Attr { attrs }
}
pub fn style(style: String, attr: Attr) -> Node {
Node {
children: Vec::new(),
ntype: NType::Stylesheet(SData {
content: css::parse(style),
attr: attr,
})
}
}
impl std::fmt::Display for Node {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.ntype {
NType::Text(ref s) => write!(f, "{}", s),
NType::Comment(ref s) => write!(f, "<!--{}-->", s),
NType::Element(ref d) => {
write!(f, "<{}", d.name);
write!(f, "{}", d.attr);
write!(f, ">");
write!(f, "<{}{}>", d.name, d.attr);
for child in self.children.iter() {
write!(f, "{}", child);
}
write!(f, "</{}>", d.name)
},
NType::Stylesheet(ref s) => {
write!(f, "<style {}>{}</style>", s.attr, s.content)
}
}
}
}
@@ -66,8 +88,3 @@ impl std::fmt::Display for Attr {
return Result::Ok(())
}
}
/*fn main() {
let e = elem("html".to_string(), Attr{attrs:"ab".chars().map(|c| (c.to_string(), c.to_string())).collect::<HashMap<_, _>>()}, vec![elem("body".to_string(), Attr{attrs:HashMap::new()}, vec![text("hi".to_string())])]);
println!("{}", e)
}*/