Skip to content

Commit 0cce7cb

Browse files
committed
Minify HTML
1 parent fe47763 commit 0cce7cb

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ tower-http = { version = "0.5.2", features = ["fs", "cors"] }
4949
# Futures utilities
5050
futures = "0.3"
5151
# SQLite database access
52-
rusqlite = { version = "0.36.0", features = ["bundled"] }
52+
rusqlite = { version = "0.36.0", features = ["bundled"] }
53+
minify-html = "0.15" # 0.16 has an issue with inline js

src/site_generator.rs

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ use crate::book_scanner::scan_books;
66
use anyhow::{Result, Context};
77
use askama::Template;
88
use std::fs;
9-
use std::path::PathBuf;
9+
use std::path::{Path, PathBuf};
1010
use log::info;
11+
use minify_html::{Cfg, minify};
1112
use std::time::SystemTime;
1213
use chrono::{Local};
1314
use webp;
@@ -115,6 +116,20 @@ impl SiteGenerator {
115116
fn get_last_updated(&self) -> String {
116117
Local::now().format("%Y-%m-%d %H:%M").to_string()
117118
}
119+
120+
// Minifies and writes HTML to disk.
121+
fn write_minify_html<P: AsRef<Path>>(&self, path: P, html: &str) -> Result<()> {
122+
let cfg = Cfg {
123+
minify_js: true,
124+
minify_css: true,
125+
..Default::default()
126+
};
127+
128+
// Attempt minification; on failure fall back to original HTML
129+
let minified = String::from_utf8(minify(html.as_bytes(), &cfg)).unwrap_or_else(|_| html.to_string());
130+
fs::write(path, minified)?;
131+
Ok(())
132+
}
118133

119134
async fn create_directories(&self, books: &[Book], stats_data: &Option<StatisticsData>) -> Result<()> {
120135
fs::create_dir_all(&self.output_dir)?;
@@ -338,7 +353,7 @@ impl SiteGenerator {
338353
};
339354

340355
let html = template.render()?;
341-
fs::write(self.output_dir.join("index.html"), html)?;
356+
self.write_minify_html(self.output_dir.join("index.html"), &html)?;
342357

343358
Ok(())
344359
}
@@ -380,7 +395,7 @@ impl SiteGenerator {
380395
let book_dir = self.books_dir().join(&book.id);
381396
fs::create_dir_all(&book_dir)?;
382397
let book_path = book_dir.join("index.html");
383-
fs::write(book_path, html)?;
398+
self.write_minify_html(book_path, &html)?;
384399

385400
// Generate Markdown export
386401
let md_template = BookMarkdownTemplate {
@@ -465,15 +480,15 @@ impl SiteGenerator {
465480

466481
// Render and write the template
467482
let html = template.render()?;
468-
483+
469484
if render_to_root {
470485
// Write directly to index.html
471-
fs::write(self.output_dir.join("index.html"), html)?;
486+
self.write_minify_html(self.output_dir.join("index.html"), &html)?;
472487
} else {
473488
// Create stats directory and write the index file
474489
let stats_dir = self.output_dir.join("statistics");
475490
fs::create_dir_all(&stats_dir)?;
476-
fs::write(stats_dir.join("index.html"), html)?;
491+
self.write_minify_html(stats_dir.join("index.html"), &html)?;
477492
}
478493

479494
Ok(())
@@ -600,9 +615,9 @@ impl SiteGenerator {
600615

601616
// Render and write the template
602617
let html = template.render()?;
603-
618+
604619
// Write to the calendar directory (already created in create_directories)
605-
fs::write(self.calendar_dir().join("index.html"), html)?;
620+
self.write_minify_html(self.calendar_dir().join("index.html"), &html)?;
606621

607622
Ok(())
608623
}

0 commit comments

Comments
 (0)