1
Fork 0
mirror of https://github.com/RGBCube/rgbcube.github.io synced 2025-05-14 05:54:58 +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::{
html,
Markup,
};
use crate::page;
use crate::{
minify,
page,
};
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(
html! {
link href="cube.min.css" rel="stylesheet";
style {
(minify::css(embed!("cube.css")))
}
},
html! {
style {
(styling)
(styling.as_ref())
}
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', '!', '>'];
// 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 output = Vec::new();
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 '/*'
if !inside_comment && c == '*' && last_char[0] == '/' {
inside_comment = true;

View file

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

View file

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

View file

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