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
use std::borrow::Cow;
use std::collections::HashMap;
use crate::compiler::data::{LayoutKind, Text};
#[derive(Clone)]
pub enum Image {
Svg {
kind: LayoutKind,
payload: String,
},
}
impl std::fmt::Debug for Image {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Image::Svg{kind, payload} => {
f.debug_struct("Svg")
.field("kind", &kind)
.field("payload", &String::from("DataNotShown"))
.finish()
}
}
}
}
#[derive(Debug, Clone)]
pub enum ImageType {
Svg,
}
impl Image {
pub fn layout(&self) -> LayoutKind {
match self {
Self::Svg { kind, payload } => kind.clone(),
}
}
pub fn image_type(&self) -> ImageType {
match self {
Self::Svg {..} => ImageType::Svg,
}
}
}
#[derive(Debug, Clone)]
pub struct Element<'a> {
pub name: Text<'a>,
pub attributes: HashMap<Text<'a>, Text<'a>>,
pub children: Vec<Node<'a>>,
}
#[derive(Debug, Clone)]
pub enum Node<'a> {
Element(Element<'a>),
Text(Text<'a>),
Image(Image),
Fragment(Vec<Node<'a>>),
}
impl<'a> Node<'a> {
pub fn new_text(val: &'a str) -> Self {
Node::Text(Text::new(val))
}
pub fn to_html_str(self) -> Text<'a> {
match self {
Node::Text(node) => node,
Node::Element(node) => {
let attributes = node.attributes
.into_iter()
.map(|(left, right)| -> String {
let mut result = String::new();
result.push_str(&format!("{}", left.0));
result.push_str("=");
result.push_str(&format!("{:?}", right.0));
result
})
.collect::<Vec<_>>()
.join(" ");
let attributes = {
if attributes.is_empty() {
Text::default()
} else {
let mut attrs = attributes;
attrs.insert(0, ' ');
Text::from_string(attrs)
}
};
let children = node.children
.into_iter()
.map(Node::to_html_str)
.map(|x| x.0)
.collect::<Vec<_>>()
.join("");
let children = Text::from_string(children);
Text::from_string(format!(
"<{name}{attrs}>{children}</{name}>",
name=node.name,
attrs=attributes,
children=children,
))
}
Node::Fragment(nodes) => {
let children = nodes
.into_iter()
.map(Node::to_html_str)
.map(|x| x.0)
.collect::<Vec<_>>()
.join("");
Text::from_string(children)
}
Node::Image(image) => {
unimplemented!()
}
}
}
}
pub fn render_ast_only<'a>(html: Vec<Node<'a>>) -> String {
html
.into_iter()
.map(Node::to_html_str)
.map(|x| x.0)
.collect::<Vec<_>>()
.join("\n")
}
pub fn render_document<'a>(html: Vec<Node<'a>>) -> String {
let body = html
.into_iter()
.map(Node::to_html_str)
.map(|x| x.0)
.collect::<Vec<_>>()
.join("\n");
let html = String::from(include_str!("../../assets/template.html.txt"));
let html = html.replace("{{deps}}", include_str!("../../assets/deps.html"));
let html = html.replace("{{css}}", include_str!("../../assets/styling.css"));
html.replace("{{body}}", &body)
}
pub fn runtime_dependencies() -> String {
let mut html = String::from(include_str!("../../assets/deps.html"));
let css = format!(
"<style>{}</style>",
include_str!("../../assets/styling.css")
);
html.push_str(&css);
html
}
pub(crate) fn dev() {
}