refactor JS integration

This commit is contained in:
Maciej Jur 2024-09-08 19:09:25 +02:00
parent 95e060567e
commit 206f682d16
Signed by: kamov
GPG key ID: 191CBFF5F72ECAFD
11 changed files with 57 additions and 44 deletions

View file

@ -3,6 +3,8 @@ title: Lambda calculus is the DNA of all computation
date: 2024-09-07T20:32:00.281Z date: 2024-09-07T20:32:00.281Z
desc: > desc: >
Lambda calculus can be used to express any computation, but what does it entail? As it turns out first class functions are the single most powerful abstraction. Lambda calculus can be used to express any computation, but what does it entail? As it turns out first class functions are the single most powerful abstraction.
scripts:
- lambda
--- ---
In lambda calculus the entire grammar of the language comprises of just three kinds of expressions In lambda calculus the entire grammar of the language comprises of just three kinds of expressions

View file

@ -13,6 +13,7 @@ pub(crate) fn render_head<'s, 'r>(
sack: &'s Sack, sack: &'s Sack,
title: String, title: String,
styles: &'s [&str], styles: &'s [&str],
js: Option<&'s [String]>,
) -> impl Renderable + 'r ) -> impl Renderable + 'r
where where
's: 'r, 's: 'r,
@ -44,6 +45,12 @@ where
@if matches!(sack.ctx.mode, Mode::Watch) { @if matches!(sack.ctx.mode, Mode::Watch) {
script { (Raw(JS_RELOAD)) } script { (Raw(JS_RELOAD)) }
} }
@if let Some(scripts) = js {
@for script in scripts {
(emit_tag_script(sack, script))
}
}
) )
} }
@ -52,3 +59,11 @@ fn render_style(style: &HashedStyle) -> impl Renderable + '_ {
link rel="stylesheet" href=(style.path.as_str()); link rel="stylesheet" href=(style.path.as_str());
) )
} }
fn emit_tag_script<'a>(sack: &'a Sack, script: &'a str) -> impl Renderable + 'a {
let src = sack.get_script(script).unwrap().path.as_str();
maud_move!(
script type="module" src=(src) defer {}
)
}

View file

@ -81,5 +81,5 @@ pub(crate) fn home(sack: &Sack, main: &str) -> String {
} }
); );
crate::html::page(sack, main, "Home".into()).render().into() crate::html::page(sack, main, "Home".into(), None).render().into()
} }

View file

@ -27,7 +27,7 @@ where
} }
); );
page(sack, list, title) page(sack, list, title, None)
} }
fn section(year: i32, group: &[LinkDate]) -> impl Renderable + '_ { fn section(year: i32, group: &[LinkDate]) -> impl Renderable + '_ {

View file

@ -24,14 +24,13 @@ pub(crate) fn show_outline(outline: Outline) -> impl Renderable {
) )
} }
/// Render the bibliography for a document pub(crate) fn emit_bibliography(bib: Vec<String>) -> impl Renderable {
pub(crate) fn show_bibliography(bib: Vec<String>) -> impl Renderable {
maud_move!( maud_move!(
section .markdown { section .bibliography.markdown {
h2 { h2 {
"Bibliography" "Bibliography"
} }
ol .bibliography { ol {
@for item in bib { @for item in bib {
li { li {
(Raw(item)) (Raw(item))

View file

@ -113,6 +113,7 @@ fn bare<'s, 'p, 'html>(
sack: &'s Sack, sack: &'s Sack,
main: impl Renderable + 'p, main: impl Renderable + 'p,
title: String, title: String,
js: Option<&'s [String]>,
) -> impl Renderable + 'html ) -> impl Renderable + 'html
where where
's: 'html, 's: 'html,
@ -121,7 +122,7 @@ where
maud_move!( maud_move!(
(Raw("<!DOCTYPE html>")) (Raw("<!DOCTYPE html>"))
html lang="en" { html lang="en" {
(head::render_head(sack, title, &[])) (head::render_head(sack, title, &[], js))
body { body {
(main) (main)
@ -145,13 +146,14 @@ where
(main) (main)
); );
bare(sack, main, title) bare(sack, main, title, None)
} }
fn page<'s, 'p, 'html>( fn page<'s, 'p, 'html>(
sack: &'s Sack, sack: &'s Sack,
main: impl Renderable + 'p, main: impl Renderable + 'p,
title: String, title: String,
js: Option<&'s [String]>,
) -> impl Renderable + 'html ) -> impl Renderable + 'html
where where
's: 'html, 's: 'html,
@ -163,7 +165,7 @@ where
(footer(sack)) (footer(sack))
); );
bare(sack, main, title) bare(sack, main, title, js)
} }
pub(crate) fn to_list(sack: &Sack, list: Vec<LinkDate>, title: String) -> String { pub(crate) fn to_list(sack: &Sack, list: Vec<LinkDate>, title: String) -> String {
@ -203,18 +205,17 @@ where
) )
} }
pub(crate) fn search<'s, 'html>(sack: &'s Sack) -> impl Renderable + 'html pub(crate) fn search<'s, 'html>(sack: &'s Sack) -> String {
where
's: 'html,
{
page( page(
sack, sack,
maud!( maud!(
main #app {} main #app {}
script type="module" { (Raw("import 'search';")) }
), ),
String::from("Search"), String::from("Search"),
Some(&["search".into()])
) )
.render()
.into()
} }
@ -244,7 +245,7 @@ impl Content for Flox {
outline: Outline, outline: Outline,
bibliography: Bibliography, bibliography: Bibliography,
) -> String { ) -> String {
flox(&self.title, parsed, sack, outline, bibliography).render().into() flox(&self.title, parsed, sack, outline, bibliography)
} }
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> { fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> {
@ -260,19 +261,15 @@ impl Content for Flox {
} }
pub(crate) fn flox<'p, 's, 'html>( pub(crate) fn flox<'p, 's, 'html>(
title: &'p str, title: &str,
parsed: &'p str, parsed: &str,
sack: &'s Sack, sack: &Sack,
outline: Outline, outline: Outline,
bibliography: Bibliography, bibliography: Bibliography,
) -> impl Renderable + 'html ) -> String {
where
'p: 'html,
's: 'html,
{
page( page(
sack, sack,
maud!( maud_move!(
main { main {
div .flox-playground { div .flox-playground {
div .cell { div .cell {
@ -291,8 +288,10 @@ where
} }
(article(title, parsed, sack, outline, bibliography)) (article(title, parsed, sack, outline, bibliography))
} }
script type="module" { (Raw("import 'editor';")) }
), ),
String::from("Flox"), String::from("Flox"),
Some(&["editor".into()])
) )
.render()
.into()
} }

View file

@ -12,6 +12,7 @@ pub(crate) struct Post {
#[serde(with = "super::isodate")] #[serde(with = "super::isodate")]
pub(crate) date: DateTime<Utc>, pub(crate) date: DateTime<Utc>,
pub(crate) desc: Option<String>, pub(crate) desc: Option<String>,
pub(crate) scripts: Option<Vec<String>>,
} }
impl Content for Post { impl Content for Post {
@ -49,7 +50,7 @@ impl Content for Post {
} }
pub fn post<'s, 'p, 'html>( pub fn post<'s, 'p, 'html>(
metadata: &'p Post, meta: &'p Post,
parsed: &'p str, parsed: &'p str,
sack: &'s Sack, sack: &'s Sack,
outline: Outline, outline: Outline,
@ -61,11 +62,11 @@ where
{ {
let main = maud_move!( let main = maud_move!(
main { main {
(article(&metadata.title, parsed, sack, outline, bibliography)) (article(&meta.title, parsed, sack, outline, bibliography))
} }
); );
crate::html::page(sack, main, metadata.title.clone()) crate::html::page(sack, main, meta.title.clone(), meta.scripts.as_deref())
} }
pub fn article<'p, 's, 'html>( pub fn article<'p, 's, 'html>(
@ -102,10 +103,8 @@ where
} }
@if let Some(bib) = bibliography.0 { @if let Some(bib) = bibliography.0 {
(crate::html::misc::show_bibliography(bib)) (crate::html::misc::emit_bibliography(bib))
} }
script type="module" {(Raw(r#"import "lambda";"#))}
} }
} }
) )

View file

@ -77,13 +77,10 @@ pub fn show(fm: &Slideshow, sack: &Sack, slides: &str) -> String {
} }
} }
script type="module" {
(Raw("import 'reveal'; import 'search';"))
}
style { (Raw(CSS)) } style { (Raw(CSS)) }
), ),
fm.title.clone(), fm.title.clone(),
Some(&["reveal".into()])
) )
.render() .render()
.into() .into()

View file

@ -75,13 +75,13 @@ fn wiki(
} }
@if let Some(bib) = bibliography.0 { @if let Some(bib) = bibliography.0 {
(crate::html::misc::show_bibliography(bib)) (crate::html::misc::emit_bibliography(bib))
} }
} }
} }
); );
crate::html::page(sack, main, matter.title.to_owned()) crate::html::page(sack, main, matter.title.to_owned(), None)
.render() .render()
.into() .into()
} }

View file

@ -38,7 +38,7 @@ fn main() {
"map/index.html".into(), "map/index.html".into(),
) )
.add_virtual( .add_virtual(
|sack| crate::html::search(sack).render().to_owned().into(), |sack| crate::html::search(sack),
"search/index.html".into(), "search/index.html".into(),
) )
.add_virtual( .add_virtual(

View file

@ -1,9 +1,11 @@
.bibliography { .bibliography {
li { margin-top: 1em;
padding-left: 0.7em;
&::marker { li {
content: '[' counter(list-item) ']'; padding-left: 0.7em;
}
} &::marker {
content: "[" counter(list-item) "]";
}
}
} }