Minify HTML
HTML made smaller for sending: spaces, comments and optional tags out, and the CSS and JavaScript inside minified too.
Version 1.0.0 · 3 functions · 1 crate
Functions
What use minify_html.qwer gives a program, with the types Quillo sees. To take only some, or under other names: use minify_html, minify_html_with from minify_html (Quillo 0.6).
minify_html(html: str) → stror an errorhtml minified: whitespace between tags, comments, quotes and closing tags that browsers do not need are taken out, and <style> and <script> contents minified. The page shows the same; a page or a fragment alike.
minify_html_with(html: str, options: any) → stror an errorThe same with options, each true or false: minify_css, minify_js (both true by default), keep_comments, keep_closing_tags (for HTML other tools will parse) and keep_templates (true: {{ }}, {% %} and <% %> left alone).
minify_html_file(from: str, to: str) → anyor an errorMinifies the HTML file from into to (folders are made): { before, after }, the sizes in bytes.
Get it
$ quillo install minify_html # minify_html.qwer into this folder$ quillo install minify_html --example # and its example, minify_html.qweOr with curl:
$ curl -O https://quillolang.org/plugins/files/minify_html.qwer$ curl -O https://quillolang.org/plugins/files/minify_html.qweThen, in your program, all of it or only what you call:
use minify_html.qweruse minify_html, minify_html_with from minify_html // Quillo 0.6: only theseIt uses this crate, which cargo fetches the first time the plugin is built:
minify-html = "0.16"Example
// Pages minified before they are sent or saved for a static export.//// quillo run minify_html.qwe then open http://localhost:8080/pageuse "minify_html.qwer"let PAGE = `<!DOCTYPE html><html> <head> <title> Menu </title> <style> body { margin: 0px; color: #ff0000; } .price { font-weight: bold; } </style> </head> <body> <!-- the menu of the day --> <h1 class="title">Today's menu</h1> <ul> <li>Soup <span class="price">6.50</span></li> <li>Tart <span class="price">11.00</span></li> </ul> <script> const total = function (a, b) { return a + b; }; console.log( total(6.5, 11) ); </script> </body></html>`route GET /page = respond(200, minify_html(PAGE)?, { "content-type": "text/html; charset=utf-8" })route GET /sizes { write_file("data/menu.html", PAGE) { file: minify_html_file("data/menu.html", "data/export/menu.html")?, with_comments: minify_html_with(PAGE, { keep_comments: true, minify_js: false })?, }}$ quillo run minify_html.qwe$ curl 'localhost:8080/page'$ curl 'localhost:8080/sizes'Source
//! HTML made smaller for sending: spaces, comments and optional tags taken out, and the CSS and JavaScript inside it minified too.//! version: 1.0.0//!//! [dependencies]//! minify-html = "0.16"use serde_json::{json, Value};const MAX_BYTES: usize = 64 * 1024 * 1024;fn cfg(o: &Value) -> minify_html::Cfg { let mut c = minify_html::Cfg::new(); let flag = |k: &str, d: bool| o.get(k).and_then(Value::as_bool).unwrap_or(d); c.minify_css = flag("minify_css", true); c.minify_js = flag("minify_js", true); c.keep_comments = flag("keep_comments", false); c.keep_closing_tags = flag("keep_closing_tags", false); c.keep_html_and_head_opening_tags = flag("keep_html_and_head_opening_tags", false); // Template syntax ({{ x }}, {% x %}, <% x %>) is kept as it is. c.preserve_brace_template_syntax = flag("keep_templates", true); c.preserve_chevron_percent_template_syntax = flag("keep_templates", true); c}fn run(html: &str, o: &Value) -> Result<String, String> { if html.len() > MAX_BYTES { return Err("more than 64 MB of HTML".into()); } String::from_utf8(minify_html::minify(html.as_bytes(), &cfg(o))).map_err(|e| e.to_string())}/// `html` minified: whitespace between tags, comments, quotes and closing tags that browsers do not need are taken out,/// and `<style>` and `<script>` contents minified. The page shows the same; a page or a fragment alike.pub fn minify_html(html: &str) -> Result<String, String> { run(html, &Value::Null)}/// The same with `options`, each true or false: `minify_css`, `minify_js` (both true by default), `keep_comments`, `keep_closing_tags`/// (for HTML other tools will parse) and `keep_templates` (true: `{{ }}`, `{% %}` and `<% %>` left alone).pub fn minify_html_with(html: &str, options: serde_json::Value) -> Result<String, String> { run(html, &options)}/// Minifies the HTML file `from` into `to` (folders are made): `{ before, after }`, the sizes in bytes.pub fn minify_html_file(from: &str, to: &str) -> Result<Value, String> { let html = std::fs::read_to_string(from).map_err(|e| format!("{}: {}", from, e))?; let out = run(&html, &Value::Null)?; if let Some(dir) = std::path::Path::new(to).parent().filter(|d| !d.as_os_str().is_empty()) { std::fs::create_dir_all(dir).map_err(|e| format!("{}: {}", dir.display(), e))?; } std::fs::write(to, &out).map_err(|e| format!("{}: {}", to, e))?; Ok(json!({ "before": html.len(), "after": out.len() }))}Related
js_minify.qwer · 1.0.0JavaScript, CSS and JSON minified: comments and spaces out, for scripts and styles served or embedded in pages.7 views · 8 downloadsCSS inliner css_inline.qwer · 1.0.0CSS inlined into HTML for email: style blocks and extra CSS written into each element's style attribute, with nothing ever fetched.7 views · 6 downloadsCSS minifier css_minify.qwer · 1.0.0CSS made small and ready for the browsers you support (lightningcss): minified with rules merged, vendor prefixes added and nesting and new syntax lowered for a browserslist query, and errors reported by line.8 views · 7 downloadsHTML rewriting html_rewrite.qwer · 1.0.0HTML rewritten as it streams, by CSS selector: links made absolute, attributes set or removed, elements removed or unwrapped, text replaced.8 views · 6 downloadsHTML to text html_text.qwer · 1.0.0HTML as readable plain text for the text part of an email: headings, lists and tables laid out, links listed at the end, and one-line previews.7 views · 7 downloadsFeeds out rss_write.qwer · 1.0.0RSS 2.0, Atom and JSON Feed written from your posts, with dates, authors, categories and full HTML content escaped correctly.8 views · 6 downloadsThread 0 posts
On Minify HTML: questions, tips, corrections, and code that uses it.
No posts yet. The first one opens the thread.