aboutsummaryrefslogtreecommitdiff
path: root/code/src/html.rs
blob: bdc9a787504815aa7887e85db537e6a733ada343 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
use crate::{article_metadata, get_articles, markdown::blocks_to_html};
use laby::{html, iter, li, ul, Render};
use std::fs::read_to_string;

pub fn scaffold(title: String, body: impl Render) -> impl Render {
    html!(
        head!(
            link!(rel = "stylesheet", href = "./style.css"),
            title!(format!("{} - metamuffin's blog", title))
        ),
        body!(
            nav!(h2!("metamuffin's blog"), a!(href = "./index.html", "index")),
            article!(body),
            footer!(p!("written by metamuffin, licensed under CC-BY-ND-4.0"))
        )
    )
}

pub fn article(path: String) -> impl Render {
    scaffold(
        article_metadata(path.clone().into()).title,
        laby::raw!(blocks_to_html(markdown::tokenize(
            &read_to_string(path).unwrap()
        ))),
    )
}

pub fn index(root: &str) -> impl Render {
    scaffold(
        "index".to_string(),
        ul!(iter!({
            get_articles(&root).into_iter().map(|meta| {
                li!(
                    meta.date.to_string(),
                    ": ",
                    a!(
                        href = format!("./{}", meta.path.to_str().unwrap().replace(".md", ".html")),
                        meta.title
                    )
                )
            })
        })),
    )
}