1use crate::components::Components;
13
14mod container;
15mod icon;
16mod text;
17
18pub use container::*;
19pub use icon::*;
20pub use text::*;
21
22pub fn html_builder() -> HtmlBuilder {
24 HtmlBuilder::default()
25}
26
27#[derive(Debug, Default, Clone, PartialEq)]
32pub struct HtmlBuilder {
33 css_font_import_urls: String,
38 component: Components,
40}
41
42impl HtmlBuilder {
44 pub fn get_css_font_import_urls(&self) -> &str {
46 &self.css_font_import_urls
47 }
48
49 pub fn css_font_import_urls(mut self, css_font_import_urls: String) -> Self {
51 self.css_font_import_urls = css_font_import_urls;
52 self
53 }
54
55 pub fn css_disable_default_browser_css(&self) -> &str {
57 "* { margin: 0; padding: 0; box-sizing: border-box; overflow: clip; } html, body { height: 100%; line-height: 1.5; } body { background: none; color: inherit; text-align: inherit; } h1, h2, h3, h4, h5, h6 { font-size: inherit; font-weight: inherit; margin: 0; } p { margin: 0; } ul, ol { list-style: none; } a { text-decoration: none; color: inherit; } "
58 }
59
60 pub fn build_style(&self) -> String {
62 let css_font_import_urls = self.get_css_font_import_urls();
63 let disable_default = self.css_disable_default_browser_css();
64 format!(r#"<style>{disable_default}{css_font_import_urls}</style>"#)
65 }
66}
67
68impl HtmlBuilder {
70 pub fn component(mut self, component: impl Into<Components>) -> Self {
72 self.component = component.into();
73 self
74 }
75
76 pub fn get_component(&self) -> &Components {
78 &self.component
79 }
80}
81
82impl HtmlBuilder {
84 pub fn render_component(component: &Components) -> String {
89 match component {
90 Components::Container(component) => container_html(component),
91 Components::Text(component) => text_html(component),
92 Components::Icon(component) => icon_html(component),
93 }
94 }
95
96 pub fn render(&self) -> String {
98 let component = self.get_component();
99 Self::render_component(component)
100 }
101
102 pub fn build(&self) -> String {
104 let render = self.render();
105 let style = self.build_style();
106 format!(
107 r#"{render}
108 {style}"#
109 )
110 }
111
112 pub fn build_as_html(&self) -> String {
114 let render = self.render();
115 let style = self.build_style();
116 format!(r#"<html><head>{style}</head><body>{render}</body></html>"#)
117 }
118}