1
Fork 0
mirror of https://github.com/RGBCube/rgbcube.github.io synced 2025-05-25 18:45:06 +00:00

Clean up minify functions

This commit is contained in:
RGBCube 2023-12-20 22:27:35 +03:00
parent 9336ad99b0
commit 80c8f36435
No known key found for this signature in database
5 changed files with 26 additions and 17 deletions

View file

@ -1,20 +1,26 @@
use embed_file::embed_string as embed;
use maud::{ use maud::{
html, html,
Markup, Markup,
}; };
use crate::page; use crate::{
minify,
page,
};
const FACES: [&str; 6] = ["front", "top", "back", "bottom", "right", "left"]; const FACES: [&str; 6] = ["front", "top", "back", "bottom", "right", "left"];
pub fn create(styling: &str, faces: [Markup; 6]) -> Markup { pub fn create<S: AsRef<str>>(styling: S, faces: [Markup; 6]) -> Markup {
page::create( page::create(
html! { html! {
link href="cube.min.css" rel="stylesheet"; style {
(minify::css(embed!("cube.css")))
}
}, },
html! { html! {
style { style {
(styling) (styling.as_ref())
} }
div class="scene" { div class="scene" {
@ -27,7 +33,9 @@ pub fn create(styling: &str, faces: [Markup; 6]) -> Markup {
} }
} }
script src="cube.min.js" {} script {
(minify::js(embed!("cube.js")))
}
}, },
) )
} }

View file

@ -1,13 +1,13 @@
const SPECIAL_CHARS: [char; 8] = ['{', '}', ':', ';', ' ', '\n', '!', '>']; const SPECIAL_CHARS: [char; 8] = ['{', '}', ':', ';', ' ', '\n', '!', '>'];
// Taken from https://github.com/amgarrett09/rust-css-minifier/blob/master/src/minify/mod.rs. // Taken from https://github.com/amgarrett09/rust-css-minifier/blob/master/src/minify/mod.rs.
pub fn css(input: &str) -> String { pub fn css<S: AsRef<str>>(input: S) -> String {
let mut last_char: Vec<char> = " ".chars().collect(); let mut last_char: Vec<char> = " ".chars().collect();
let mut output = Vec::new(); let mut output = Vec::new();
let mut inside_comment = false; let mut inside_comment = false;
for c in input.chars() { for c in input.as_ref().chars() {
// We're in a comment if we find '/*' // We're in a comment if we find '/*'
if !inside_comment && c == '*' && last_char[0] == '/' { if !inside_comment && c == '*' && last_char[0] == '/' {
inside_comment = true; inside_comment = true;

View file

@ -1,18 +1,19 @@
use anyhow::Context;
use minify_js::{ use minify_js::{
Session, Session,
TopLevelMode, TopLevelMode,
}; };
pub fn js(input: &str) -> String { pub fn js<S: AsRef<str>>(input: S) -> String {
let session = Session::new(); let session = Session::new();
let mut out = Vec::new(); let mut out = Vec::new();
minify_js::minify(&session, TopLevelMode::Module, input.as_bytes(), &mut out) minify_js::minify(
.with_context(|| format!("Failed to minify::js: {code}.")) &session,
.unwrap(); TopLevelMode::Module,
input.as_ref().as_bytes(),
&mut out,
)
.unwrap();
String::from_utf8(out) String::from_utf8(out).unwrap()
.with_context(|| format!("Failed to create a string from minify::js output: {out:?}."))
.unwrap()
} }

View file

@ -13,7 +13,7 @@ use crate::{
pub async fn generate() -> Markup { pub async fn generate() -> Markup {
cube::create( cube::create(
minify::css(embed!("404.css").as_ref()).as_str(), minify::css(embed!("404.css")),
array::from_fn(|_| { array::from_fn(|_| {
(html! { (html! {
div class="frame" {} div class="frame" {}

View file

@ -11,7 +11,7 @@ use crate::{
pub async fn generate() -> Markup { pub async fn generate() -> Markup {
cube::create( cube::create(
minify::css(embed!("index.css").as_ref()).as_str(), minify::css(embed!("index.css")),
[ [
html! { html! {
a href="contact" { a href="contact" {