From b1acf1d4d71e5c4c59c100b4c538923d5e8702ed Mon Sep 17 00:00:00 2001 From: Maciej Jur Date: Sat, 27 Jul 2024 15:57:41 +0200 Subject: [PATCH] sri --- Cargo.lock | 38 +++++++--------------------------- src/html/head.rs | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ src/html/mod.rs | 43 +++----------------------------------- src/main.rs | 9 ++++---- src/text/md.rs | 7 ++++--- 5 files changed, 74 insertions(+), 77 deletions(-) create mode 100644 src/html/head.rs diff --git a/Cargo.lock b/Cargo.lock index b1f71c1..83f3b81 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -105,23 +105,18 @@ version = "0.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d902e3d592a523def97af8f317b08ce16b7ab854c1985a0c671e6f15cebc236" -[[package]] -name = "async-trait" -version = "0.1.81" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6e0c28dcc82d7c8ead5cb13beb15405b57b8546e93215673ff8ca0349a028107" -dependencies = [ - "proc-macro2", - "quote", - "syn 2.0.53", -] - [[package]] name = "autocfg" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa" +[[package]] +name = "base64" +version = "0.22.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" + [[package]] name = "biblatex" version = "0.9.3" @@ -727,6 +722,7 @@ dependencies = [ name = "hauchiwa" version = "0.0.3" dependencies = [ + "base64", "camino", "chrono", "glob", @@ -739,7 +735,7 @@ dependencies = [ "rayon", "serde", "serde_json", - "sha256", + "sha2", "tungstenite", ] @@ -776,12 +772,6 @@ version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" -[[package]] -name = "hex" -version = "0.4.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7f24254aa9a54b5c858eaee2f5bccdb46aaf0e486a595ed5fd8f86ba55232a70" - [[package]] name = "html-escape" version = "0.2.13" @@ -1505,18 +1495,6 @@ dependencies = [ "digest", ] -[[package]] -name = "sha256" -version = "1.5.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "18278f6a914fa3070aa316493f7d2ddfb9ac86ebc06fa3b83bffda487e9065b0" -dependencies = [ - "async-trait", - "bytes", - "hex", - "sha2", -] - [[package]] name = "simd-adler32" version = "0.3.7" diff --git a/src/html/head.rs b/src/html/head.rs new file mode 100644 index 0000000..652be79 --- /dev/null +++ b/src/html/head.rs @@ -0,0 +1,54 @@ +use hauchiwa::{HashedStyle, Mode, Sack}; +use hypertext::{html_elements, maud_move, Raw, Renderable}; + +const JS_RELOAD: &str = r#" +const socket = new WebSocket("ws://localhost:1337"); +socket.addEventListener("message", (event) => { + console.log(event); + window.location.reload(); +}); +"#; + +pub(crate) fn render_head<'s, 'r>( + sack: &'s Sack, + title: String, + styles: &'s [&str], +) -> impl Renderable + 'r +where + 's: 'r, +{ + let title = format!("{} | kamoshi.org", title); + let css = sack.get_style("styles").expect("Missing styles"); + let css_r = sack.get_style("reveal").expect("Missing styles"); + let css_p = sack.get_style("leaflet").expect("Missing styles"); + + maud_move!( + meta charset="utf-8"; + meta name="viewport" content="width=device-width, initial-scale=1"; + title { + (title) + } + + // link rel="sitemap" href="/sitemap.xml"; + + (render_style(css)) + (render_style(css_r)) + (render_style(css_p)) + + link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"; + link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"; + link rel="icon" href="/favicon.ico" sizes="any"; + + script type="importmap" {(Raw(sack.get_import_map()))} + + @if matches!(sack.ctx.mode, Mode::Watch) { + script { (Raw(JS_RELOAD)) } + } + ) +} + +fn render_style(style: &HashedStyle) -> impl Renderable + '_ { + maud_move!( + link rel="stylesheet" href=(style.path.as_str()) integrity=(&style.sri); + ) +} diff --git a/src/html/mod.rs b/src/html/mod.rs index 1bb23d6..181e791 100644 --- a/src/html/mod.rs +++ b/src/html/mod.rs @@ -6,12 +6,13 @@ mod post; mod slideshow; mod special; mod wiki; +mod head; use std::collections::HashMap; use camino::Utf8Path; use chrono::Datelike; -use hauchiwa::{LinkDate, Mode, Sack}; +use hauchiwa::{LinkDate, Sack}; use hypertext::{html_elements, maud, maud_move, GlobalAttributes, Raw, Renderable}; pub(crate) use home::home; @@ -19,44 +20,6 @@ pub(crate) use post::Post; pub(crate) use slideshow::Slideshow; pub(crate) use wiki::Wiki; -const JS_RELOAD: &str = r#" -const socket = new WebSocket("ws://localhost:1337"); -socket.addEventListener("message", (event) => { - console.log(event); - window.location.reload(); -}); -"#; - -fn head<'s, 'html>(sack: &'s Sack, title: String) -> impl Renderable + 'html -where - 's: 'html, -{ - let title = format!("{} | kamoshi.org", title); - - maud_move!( - meta charset="utf-8"; - meta name="viewport" content="width=device-width, initial-scale=1"; - title { - (title) - } - - // link rel="sitemap" href="/sitemap.xml"; - - link rel="stylesheet" href="/styles.css"; - link rel="stylesheet" href="/reveal.css"; - link rel="stylesheet" href="/leaflet.css"; - link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png"; - link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"; - link rel="icon" href="/favicon.ico" sizes="any"; - - script type="importmap" {(Raw(sack.get_import_map()))} - - @if matches!(sack.ctx.mode, Mode::Watch) { - script { (Raw(JS_RELOAD)) } - } - ) -} - fn navbar() -> impl Renderable { static ITEMS: &[(&str, &str)] = &[ ("Posts", "/posts/"), @@ -154,7 +117,7 @@ where maud_move!( (Raw("")) html lang="en" { - (head(sack, title)) + (head::render_head(sack, title, &[])) body { (main) diff --git a/src/main.rs b/src/main.rs index 761a837..22dfa60 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,6 +4,7 @@ mod ts; use clap::{Parser, ValueEnum}; use hauchiwa::Website; +use html::{Post, Slideshow, Wiki}; use hypertext::Renderable; #[derive(Parser, Debug, Clone)] @@ -22,10 +23,10 @@ fn main() { let args = Args::parse(); let website = Website::design() - .content::("content/about.md", ["md"].into()) - .content::("content/posts/**/*", ["md", "mdx"].into()) - .content::("content/slides/**/*", ["md", "lhs"].into()) - .content::("content/wiki/**/*", ["md"].into()) + .content::("content/about.md", ["md"].into()) + .content::("content/posts/**/*", ["md", "mdx"].into()) + .content::("content/slides/**/*", ["md", "lhs"].into()) + .content::("content/wiki/**/*", ["md"].into()) .js("search", "./js/search/dist/search.js") .js("photos", "./js/vanilla/photos.js") .js("reveal", "./js/vanilla/reveal.js") diff --git a/src/text/md.rs b/src/text/md.rs index 587bd24..d0b9b12 100644 --- a/src/text/md.rs +++ b/src/text/md.rs @@ -64,7 +64,7 @@ pub fn parse( .into_iter() .map(make_math) .map(make_emoji) - .map(swap_hashed_image(path, &sack.artifacts.images)) + .map(swap_hashed_image(path, sack)) .collect::>(); let stream = make_code(stream) @@ -358,7 +358,7 @@ fn make_emoji(event: Event) -> Event { fn swap_hashed_image<'a>( dir: &'a Utf8Path, - hash: &'a HashMap, + sack: &'a Sack, ) -> impl Fn(Event) -> Event + 'a { move |event| match event { Event::Start(start) => match start { @@ -369,7 +369,8 @@ fn swap_hashed_image<'a>( id, } => { let rel = dir.join(dest_url.as_ref()); - let hashed = hash.get(&rel).map(|path| path.as_str().to_owned().into()); + let img = sack.get_image(&rel); + let hashed = img.map(|path| path.as_str().to_owned().into()); Event::Start(Tag::Image { link_type, dest_url: hashed.unwrap_or(dest_url),