sauron_syntax/
lib.rs

1#![deny(
2    missing_docs,
3    missing_debug_implementations,
4    missing_copy_implementations,
5    trivial_casts,
6    trivial_numeric_casts,
7    unstable_features,
8    unused_import_braces
9)]
10//! A utility crate which provides conversion of html text into sauron view syntax
11//!
12use sauron_parse::ParseError;
13pub use to_syntax::ToSyntax;
14
15mod to_syntax;
16
17/// converts html to sauron view syntax
18pub fn html_to_syntax(
19    html: &str,
20    use_macro: bool,
21) -> Result<String, ParseError> {
22    log::info!("input: {}", html);
23    println!("input: {}", html);
24    match sauron_parse::parse::<()>(html) {
25        Ok(root_node) => {
26            let mut buffer = String::new();
27            if let Some(root_node) = root_node {
28                root_node.to_syntax(&mut buffer, use_macro, 0)?;
29            }
30            Ok(buffer)
31        }
32        Err(e) => {
33            log::error!("error: {}", e);
34            Err(e)
35        }
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn simpe_convert() {
45        let input = r#"
46        <div>content1</div>
47        <div>content2</div>
48        <div>content3</div>
49            "#;
50
51        let expected = r#"html!([],[
52    div!([],[text("content1")]),
53    div!([],[text("content2")]),
54    div!([],[text("content3")]),
55])"#;
56        let syntax = html_to_syntax(input, true).expect("must not fail");
57        println!("syntax: {}", syntax);
58        assert_eq!(expected, syntax);
59    }
60
61    #[test]
62    fn simple_html_parse() {
63        let input = r#"<html lang="en">
64<head>
65    <meta charset="UTF-8">
66    <meta name="viewport" content="width=device-width, initial-scale=1">
67    <title>Interactive sauron app</title>
68    <style type="text/css">
69        body {
70            font-family: "Fira Sans", "Courier New";
71        }
72    </style>
73</head>
74<body style='margin:0;padding:0;width:100%;height:100%;'>
75  <div id="web-app" style='width:100%;height:100%;'>
76      #HTML_INSERTED_HERE_BY_SERVER#
77  </div>
78  <!-- This is a comment -->
79</body>
80</html>"#;
81        let expected = r#"html!([lang("en"),],[
82    head!([],[
83        meta!([charset("UTF-8"),],[]),
84        meta!([name("viewport"),content("width=device-width, initial-scale=1"),],[]),
85        title!([],[text("Interactive sauron app")]),
86        style!([r#type("text/css"),],[text("
87        body {
88            font-family: "Fira Sans", "Courier New";
89        }
90    ")]),
91    ]),
92    body!([style("margin:0;padding:0;width:100%;height:100%;"),],[
93        div!([id("web-app"),style("width:100%;height:100%;"),],[text("
94      #HTML_INSERTED_HERE_BY_SERVER#
95  ")]),
96    ]),
97])"#;
98        let syntax = html_to_syntax(input, true).expect("must not fail");
99        println!("syntax: {}", syntax);
100        assert_eq!(expected, syntax);
101    }
102
103    #[test]
104    fn simple_svg_parse() {
105        let input = r#"
106<svg height="400" viewBox="0 0 600 400" width="600" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink">
107    <defs>
108        <filter id="shadow">
109            <feDropShadow dx="2" dy="1" stdDeviation="0.2"></feDropShadow>
110        </filter>
111    </defs>
112    <image height="400" xlink:href="data:image/jpeg;base64,/9j/4AAQSkZJRgABA" width="600" x="0" y="0"></image>
113    <text fill="red" font-family="monospace" font-size="40" stroke="white" stroke-width="1" style="filter:url(#shadow);" x="65" y="55">John Smith</text>
114    <text fill="white" font-family="monospace" font-size="20" style="filter:url(#shadow);" x="100" y="100">10101011</text>
115    <text fill="red" font-family="monospace" font-size="50" style="filter:url(#shadow);" width="500" x="20" y="200">Happy birthday</text>
116</svg>
117"#;
118        let expected = r#"html!([],[
119    svg!([height(400),viewBox("0 0 600 400"),width(600),xmlns("http://www.w3.org/2000/svg"),],[
120        defs!([],[
121            filter!([id("shadow"),],[
122                feDropShadow!([dx(2),dy(1),stdDeviation(0.2),],[]),
123            ]),
124        ]),
125        image!([height(400),href("data:image/jpeg;base64,/9j/4AAQSkZJRgABA"),width(600),x(0),y(0),],[]),
126        text!([fill("red"),font_family("monospace"),font_size(40),stroke("white"),stroke_width(1),style("filter:url(#shadow);"),x(65),y(55),],[text("John Smith")]),
127        text!([fill("white"),font_family("monospace"),font_size(20),style("filter:url(#shadow);"),x(100),y(100),],[text("10101011")]),
128        text!([fill("red"),font_family("monospace"),font_size(50),style("filter:url(#shadow);"),width(500),x(20),y(200),],[text("Happy birthday")]),
129    ]),
130])"#;
131        let syntax = html_to_syntax(input, true).expect("must not fail");
132        println!("syntax: {}", syntax);
133        assert_eq!(expected, syntax);
134    }
135}