This commit is contained in:
Maciej Jur 2024-07-27 15:57:41 +02:00
parent 1761d75ddc
commit b1acf1d4d7
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
5 changed files with 74 additions and 77 deletions

38
Cargo.lock generated
View file

@ -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"

54
src/html/head.rs Normal file
View file

@ -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);
)
}

View file

@ -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("<!DOCTYPE html>"))
html lang="en" {
(head(sack, title))
(head::render_head(sack, title, &[]))
body {
(main)

View file

@ -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::<crate::html::Post>("content/about.md", ["md"].into())
.content::<crate::html::Post>("content/posts/**/*", ["md", "mdx"].into())
.content::<crate::html::Slideshow>("content/slides/**/*", ["md", "lhs"].into())
.content::<crate::html::Wiki>("content/wiki/**/*", ["md"].into())
.content::<Post>("content/about.md", ["md"].into())
.content::<Post>("content/posts/**/*", ["md", "mdx"].into())
.content::<Slideshow>("content/slides/**/*", ["md", "lhs"].into())
.content::<Wiki>("content/wiki/**/*", ["md"].into())
.js("search", "./js/search/dist/search.js")
.js("photos", "./js/vanilla/photos.js")
.js("reveal", "./js/vanilla/reveal.js")

View file

@ -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::<Vec<_>>();
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<Utf8PathBuf, Utf8PathBuf>,
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),