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
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.
scripts:
- lambda
---
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,
title: String,
styles: &'s [&str],
js: Option<&'s [String]>,
) -> impl Renderable + 'r
where
's: 'r,
@ -44,6 +45,12 @@ where
@if matches!(sack.ctx.mode, Mode::Watch) {
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());
)
}
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 + '_ {

View file

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

View file

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

View file

@ -12,6 +12,7 @@ pub(crate) struct Post {
#[serde(with = "super::isodate")]
pub(crate) date: DateTime<Utc>,
pub(crate) desc: Option<String>,
pub(crate) scripts: Option<Vec<String>>,
}
impl Content for Post {
@ -49,7 +50,7 @@ impl Content for Post {
}
pub fn post<'s, 'p, 'html>(
metadata: &'p Post,
meta: &'p Post,
parsed: &'p str,
sack: &'s Sack,
outline: Outline,
@ -61,11 +62,11 @@ where
{
let main = maud_move!(
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>(
@ -102,10 +103,8 @@ where
}
@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)) }
),
fm.title.clone(),
Some(&["reveal".into()])
)
.render()
.into()

View file

@ -75,13 +75,13 @@ fn wiki(
}
@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()
.into()
}

View file

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

View file

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