Added html elements

This commit is contained in:
2025-10-05 23:29:00 +02:00
parent 59305ce83c
commit af6fb8fd68
2 changed files with 214 additions and 0 deletions

63
src/html/mod.rs Normal file
View File

@@ -0,0 +1,63 @@
pub(crate) mod elements;
pub(crate) trait Render {
fn render(&self) -> String;
}
pub(crate) struct Page {
title: String,
head: Vec<Box<dyn Render>>,
body: Vec<Box<dyn Render>>,
}
impl Render for Vec<Box<dyn Render>> {
fn render(&self) -> String {
let mut result = String::new();
for element in self {
let render = element.render();
result.push_str(&render);
}
result
}
}
impl Render for Page {
fn render(&self) -> String {
format!(
"\
<!DOCTYPE html>
<html lang=\"en\">
<head>
<meta charset=\"UTF-8\">
<title>{}</title>
<meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0\" />
{}
</head>
<body>
{}
</body>
</html>",
self.title,
self.head.render(),
self.body.render()
)
}
}
impl Page {
pub(crate) fn new(title: String) -> Self {
Page {
title,
head: vec![],
body: vec![],
}
}
pub(crate) fn append_element_to_body(&mut self, element: impl Render + 'static) {
self.body.push(Box::new(element));
}
pub(crate) fn append_element_to_head(&mut self, element: impl Render + 'static) {
self.head.push(Box::new(element));
}
}