diff options
| author | WriterV2 <primary-v2@protonmail.com> | 2026-04-03 15:50:13 +0200 |
|---|---|---|
| committer | WriterV2 <primary-v2@protonmail.com> | 2026-04-03 15:50:13 +0200 |
| commit | 6f9b52954da265066edfca19464d1ef56e29cb53 (patch) | |
| tree | b8f1caf9198df64abcd686e64b1c260d87a38241 /src | |
| parent | c40fb1213ca852df4ec138a84275e277e7f7614b (diff) | |
Add html minifier + refactor file writing
Diffstat (limited to 'src')
| -rw-r--r-- | src/main.rs | 6 | ||||
| -rw-r--r-- | src/page.rs | 14 |
2 files changed, 16 insertions, 4 deletions
diff --git a/src/main.rs b/src/main.rs index 02aac19..1108c23 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,7 +13,7 @@ struct Data { impl page::Page for Vec<Story> { fn page_title() -> String { - String::from("Storys") + String::from("Stories") } fn render_content(self) -> Markup { @@ -44,8 +44,6 @@ fn main() -> Result<()> { let data_file_content = std::fs::read_to_string("data.toml").with_context(|| "Failed to read data.toml")?; let data: Data = toml::from_str(&data_file_content)?; - let stories_html = data.stories.render_page(); - std::fs::write("dist/stories.html", stories_html.into_string()) - .with_context(|| "Failed to write dist/stories.html")?; + data.stories.write_page()?; Ok(()) } diff --git a/src/page.rs b/src/page.rs index 6d788f3..747f243 100644 --- a/src/page.rs +++ b/src/page.rs @@ -1,4 +1,6 @@ +use anyhow::{Context, Result}; use maud::{html, Markup, DOCTYPE}; +use minify_html::{minify, Cfg}; pub trait Page: Sized { fn render_content(self) -> Markup; @@ -107,4 +109,16 @@ pub trait Page: Sized { Self::render_navigation() )(Self::render_content(self))) } + + fn write_page(self) -> Result<()> { + let page_html = self.render_page().into_string(); + let mut cfg = Cfg::new(); + cfg.minify_css = true; + let minified_html = minify(page_html.as_bytes(), &cfg); + let html_path = format!("dist/{}.html", Self::page_title().to_lowercase()); + + std::fs::write(&html_path, minified_html) + .with_context(|| format!("Failed to write {}", html_path))?; + Ok(()) + } } |
