diff --git a/.gitignore b/.gitignore index 377da94..966af59 100644 --- a/.gitignore +++ b/.gitignore @@ -3,7 +3,11 @@ !assets/ !src/ +!src/cube/ +!src/minify/ !src/pages/ +!src/pages/_404/ +!src/pages/index/ !.gitignore diff --git a/404.html b/404.html deleted file mode 100644 index a236a93..0000000 --- a/404.html +++ /dev/null @@ -1,114 +0,0 @@ - - - - - - - - - RGBCube - - - - - - - - - - - - - - - - - - - - - - - - -
-
-
-
404
-
-
-
-
-
-
-
404
-
-
-
-
-
-
-
404
-
-
-
-
-
-
-
404
-
-
-
-
-
-
-
404
-
-
-
-
-
-
-
404
-
-
-
-
-
-
-
- - - - - diff --git a/Cargo.lock b/Cargo.lock index 0ad748b..ed73c18 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -177,6 +177,12 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "embed-file" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11c84540ea27680af34e655a36b1b3d362f2a1d2088db07760358a01fa9546ad" + [[package]] name = "env_logger" version = "0.10.1" @@ -812,6 +818,7 @@ version = "0.1.0" dependencies = [ "anyhow", "axum", + "embed-file", "env_logger", "log", "maud", diff --git a/Cargo.toml b/Cargo.toml index fee00e0..ae2f1c7 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,6 +11,7 @@ edition = "2021" [dependencies] anyhow = "1.0.75" axum = "0.7.2" +embed-file = "0.1.0" env_logger = "0.10.1" log = "0.4.20" maud = { git = "https://github.com/vidhanio/maud", branch = "patch-1", features = [ "axum" ] } diff --git a/src/constants.rs b/src/constants.rs index 676a370..58fbb31 100644 --- a/src/constants.rs +++ b/src/constants.rs @@ -1 +1 @@ -pub const URL: &'static str = "https://rgbcube.github.io/"; +pub const URL: &str = "https://rgbcube.github.io/"; diff --git a/assets/cube.css b/src/cube/cube.css similarity index 100% rename from assets/cube.css rename to src/cube/cube.css diff --git a/assets/cube.js b/src/cube/cube.js similarity index 100% rename from assets/cube.js rename to src/cube/cube.js diff --git a/src/cube.rs b/src/cube/mod.rs similarity index 87% rename from src/cube.rs rename to src/cube/mod.rs index 881b40f..c0211fa 100644 --- a/src/cube.rs +++ b/src/cube/mod.rs @@ -10,7 +10,7 @@ const FACES: [&str; 6] = ["front", "top", "back", "bottom", "right", "left"]; pub fn create(styling: &str, faces: [Markup; 6]) -> Markup { page::create( html! { - link href="cube.css" rel="stylesheet"; + link href="cube.min.css" rel="stylesheet"; }, html! { style { @@ -26,6 +26,8 @@ pub fn create(styling: &str, faces: [Markup; 6]) -> Markup { } } } + + script src="cube.min.js" {} }, ) } diff --git a/src/main.rs b/src/main.rs index a052191..486f726 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod constants; mod cube; +mod minify; mod page; mod pages; diff --git a/src/minify/css.rs b/src/minify/css.rs new file mode 100644 index 0000000..abeac96 --- /dev/null +++ b/src/minify/css.rs @@ -0,0 +1,44 @@ +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 { + let mut last_char: Vec = " ".chars().collect(); + let mut output = Vec::new(); + + let mut inside_comment = false; + + for c in input.chars() { + // We're in a comment if we find '/*' + if !inside_comment && c == '*' && last_char[0] == '/' { + inside_comment = true; + output.pop(); + } + + // We're no longer in a comment if we find '*/' + if inside_comment && c == '/' && last_char[0] == '*' { + inside_comment = false; + } + + // We should NOT add a char to the output if: + // 1) It's a line break, OR + // 2) The char is a space AND the last char scanned was one of our + // special cases OR + // 3) We're inside a comment + // should_add_char is the negation of that + if !(c == '\n' || (c == ' ' && SPECIAL_CHARS.contains(&last_char[0])) || inside_comment) { + // Remove last char (and don't put it back) if it's a space before + // a special character, or if it's a semicolon before an ending brace + if let Some(last) = output.pop() { + if (!SPECIAL_CHARS.contains(&c) || last != ' ') && (c != '}' || last != ';') { + output.push(last); + } + } + + output.push(c); + } + + last_char[0] = c; + } + + output.iter().collect() +} diff --git a/src/minify/js.rs b/src/minify/js.rs new file mode 100644 index 0000000..fc0a5c8 --- /dev/null +++ b/src/minify/js.rs @@ -0,0 +1,18 @@ +use anyhow::Context; +use minify_js::{ + Session, + TopLevelMode, +}; + +pub fn js(input: &str) -> 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(); + + String::from_utf8(out) + .with_context(|| format!("Failed to create a string from minify::js output: {out:?}.")) + .unwrap() +} diff --git a/src/minify/mod.rs b/src/minify/mod.rs new file mode 100644 index 0000000..b911814 --- /dev/null +++ b/src/minify/mod.rs @@ -0,0 +1,5 @@ +mod css; +pub use css::css; + +mod js; +pub use js::js; diff --git a/src/pages/_404/404.css b/src/pages/_404/404.css new file mode 100644 index 0000000..62addcb --- /dev/null +++ b/src/pages/_404/404.css @@ -0,0 +1,29 @@ +.face { + display: grid; + grid-template-columns: repeat(2, 1fr); + grid-template-rows: repeat(2, 1fr); + box-shadow: 0 0 10px #AAAAAA; +} + +.square { + width: 100%; + height: 100%; +} + +.magenta { + background-color: magenta; +} + +.black { + background-color: black; +} + +.frame { + position: absolute; + + top: 50%; + left: 50%; + transform: translate(-50%, -50%); + + color: black; +} \ No newline at end of file diff --git a/src/pages/_404/mod.rs b/src/pages/_404/mod.rs new file mode 100644 index 0000000..6f4dc45 --- /dev/null +++ b/src/pages/_404/mod.rs @@ -0,0 +1,28 @@ +use std::array; + +use embed_file::embed_string as embed; +use maud::{ + html, + Markup, +}; + +use crate::{ + cube, + minify, +}; + +pub async fn generate() -> Markup { + cube::create( + minify::css(embed!("404.css").as_ref()).as_str(), + array::from_fn(|_| { + (html! { + div class="frame" {} + div class="square black" {} + div class="square magenta" {} + div class="square magenta" {} + div class="square black" {} + }) + .clone() + }), + ) +} diff --git a/src/pages/index.rs b/src/pages/index.rs deleted file mode 100644 index 9d1029a..0000000 --- a/src/pages/index.rs +++ /dev/null @@ -1,103 +0,0 @@ -use maud::{ - html, - Markup, -}; - -use crate::cube; - -pub async fn generate() -> Markup { - cube::create( - STYLING, - [ - html! { - a href="contact" { - div class="frame" { - "contact" - } - } - }, - html! { - a href="github" { - div class="frame" { - "github" - } - } - }, - html! {}, - html! {}, - html! {}, - html! {}, - ], - ) -} - -const STYLING: &str = r#" -.frame:hover { - background-color: #FFFF00; -} - -.face::after { - z-index: -1; - - content: ""; - - height: inherit; - width: inherit; - - position: absolute; -} - -.front { - background: linear-gradient(to bottom, cyan, blue); -} - -.front::after { - background: linear-gradient(to bottom, white, magenta); - mask-image: linear-gradient(to left, magenta, transparent); -} - -.top { - background: linear-gradient(to bottom, lime, cyan); -} - -.top::after { - background: linear-gradient(to bottom, yellow, white); - mask-image: linear-gradient(to left, white, transparent); -} - -.back { - background: linear-gradient(to bottom, yellow, red); -} - -.back::after { - background: linear-gradient(to bottom, lime, black); - mask-image: linear-gradient(to left, black, transparent); -} - -.bottom { - background: linear-gradient(to bottom, blue, black); -} - -.bottom::after { - background: linear-gradient(to bottom, magenta, red); - mask-image: linear-gradient(to left, red, transparent); -} - -.right { - background: linear-gradient(to bottom, white, magenta); -} - -.right::after { - background: linear-gradient(to bottom, yellow, red); - mask-image: linear-gradient(to left, red, transparent); -} - -.left { - background: linear-gradient(to bottom, lime, black); -} - -.left::after { - background: linear-gradient(to bottom, cyan, blue); - mask-image: linear-gradient(to left, blue, transparent); -} -"#; diff --git a/src/pages/index/index.css b/src/pages/index/index.css new file mode 100644 index 0000000..e428d6d --- /dev/null +++ b/src/pages/index/index.css @@ -0,0 +1,68 @@ +.frame:hover { + background-color: #FFFF00; +} + +.face::after { + z-index: -1; + + content: ""; + + height: inherit; + width: inherit; + + position: absolute; +} + +.front { + background: linear-gradient(to bottom, cyan, blue); +} + +.front::after { + background: linear-gradient(to bottom, white, magenta); + mask-image: linear-gradient(to left, magenta, transparent); +} + +.top { + background: linear-gradient(to bottom, lime, cyan); +} + +.top::after { + background: linear-gradient(to bottom, yellow, white); + mask-image: linear-gradient(to left, white, transparent); +} + +.back { + background: linear-gradient(to bottom, yellow, red); +} + +.back::after { + background: linear-gradient(to bottom, lime, black); + mask-image: linear-gradient(to left, black, transparent); +} + +.bottom { + background: linear-gradient(to bottom, blue, black); +} + +.bottom::after { + background: linear-gradient(to bottom, magenta, red); + mask-image: linear-gradient(to left, red, transparent); +} + +.right { + background: linear-gradient(to bottom, white, magenta); +} + +.right::after { + background: linear-gradient(to bottom, yellow, red); + mask-image: linear-gradient(to left, red, transparent); +} + +.left { + background: linear-gradient(to bottom, lime, black); +} + +.left::after { + background: linear-gradient(to bottom, cyan, blue); + mask-image: linear-gradient(to left, blue, transparent); +} \ No newline at end of file diff --git a/src/pages/index/mod.rs b/src/pages/index/mod.rs new file mode 100644 index 0000000..4a951ec --- /dev/null +++ b/src/pages/index/mod.rs @@ -0,0 +1,36 @@ +use embed_file::embed_string as embed; +use maud::{ + html, + Markup, +}; + +use crate::{ + cube, + minify, +}; + +pub async fn generate() -> Markup { + cube::create( + minify::css(embed!("index.css").as_ref()).as_str(), + [ + html! { + a href="contact" { + div class="frame" { + "contact" + } + } + }, + html! { + a href="github" { + div class="frame" { + "github" + } + } + }, + html! {}, + html! {}, + html! {}, + html! {}, + ], + ) +} diff --git a/src/pages/mod.rs b/src/pages/mod.rs index 9d6bda4..aab9f79 100644 --- a/src/pages/mod.rs +++ b/src/pages/mod.rs @@ -7,4 +7,4 @@ macro_rules! import { }; } -import! { index } +import! { _404, index }