Compare commits

..

No commits in common. "1c2e87aa2509d06a3cf74da01b08143bd3381f41" and "46705d707f144764f90d5f0082c4df63b07f8b1e" have entirely different histories.

15 changed files with 1307 additions and 1411 deletions

View file

@ -1,2 +0,0 @@
tab_spaces = 4
hard_tabs = true

View file

@ -1,6 +1,6 @@
use hypertext::{html_elements, maud, maud_move, GlobalAttributes, Raw, Renderable}; use hypertext::{html_elements, maud, maud_move, GlobalAttributes, Raw, Renderable};
use crate::{pipeline::Sack, text::md::parse, LinkDate, Linkable}; use crate::text::md::parse;
const INTRO: &str = r#" const INTRO: &str = r#"
## ##
@ -15,7 +15,7 @@ const INTRO: &str = r#"
"#; "#;
fn intro() -> impl Renderable { fn intro() -> impl Renderable {
let (_, html, _) = parse(INTRO.into(), None); let (_, html, _) = parse(INTRO, None);
maud!( maud!(
section .p-card.intro-jp lang="ja-JP" { section .p-card.intro-jp lang="ja-JP" {
(Raw(html)) (Raw(html))
@ -23,6 +23,19 @@ fn intro() -> impl Renderable {
) )
} }
fn kanji() -> impl Renderable {
maud!(
section .p-card {
h2 .p-card__heading {
"Kanji of the Day"
}
div {
// <Widget client:load/>
}
}
)
}
fn photo() -> impl Renderable { fn photo() -> impl Renderable {
maud!( maud!(
section .p-card.home-card-image { section .p-card.home-card-image {
@ -38,38 +51,10 @@ fn photo() -> impl Renderable {
) )
} }
fn latest(sack: &Sack) -> impl Renderable { pub fn home<'data, 'home, R>(main: R) -> impl Renderable + 'home
let links = {
let mut links = sack.get_links("**");
links.sort_by(|a, b| b.date.cmp(&a.date));
links
};
maud_move!(
section .p-card {
h2 .p-card__heading {
"Latest"
}
ol .p-card__latest {
@for link in links.iter().take(5) {
li {
a href=(link.link.path.as_str()) {
(&link.link.name)
}
}
}
}
}
)
}
pub(crate) fn home<'s, 'p, 'html>(
sack: &'s Sack,
main: impl Renderable + 'p,
) -> impl Renderable + 'html
where where
's: 'html, 'data: 'home,
'p: 'html, R: Renderable + 'data,
{ {
let main = maud_move!( let main = maud_move!(
main .l-home { main .l-home {
@ -80,10 +65,9 @@ where
(intro()) (intro())
// (kanji()) // (kanji())
(photo()) (photo())
(latest(sack))
} }
} }
); );
crate::html::page(sack, main, "Home".into()) crate::html::page("Home", main, None)
} }

View file

@ -1,24 +1,20 @@
use crate::{html::page, LinkDate};
use camino::Utf8PathBuf;
use chrono::{DateTime, Utc};
use hypertext::{html_elements, maud_move, GlobalAttributes, Renderable}; use hypertext::{html_elements, maud_move, GlobalAttributes, Renderable};
use crate::html::page; pub fn list<'data, 'list>(
use crate::pipeline::Sack; title: &'data str,
use crate::LinkDate; groups: &'data [(i32, Vec<LinkDate>)],
) -> impl Renderable + 'list
pub fn list<'s, 'g, 'html>(
sack: &'s Sack,
groups: &'g [(i32, Vec<LinkDate>)],
title: String,
) -> impl Renderable + 'html
where where
's: 'html, 'data: 'list,
'g: 'html,
{ {
let heading = title.clone();
let list = maud_move!( let list = maud_move!(
main .page-list-main { main .page-list-main {
article .page-list { article .page-list {
header .markdown { header .markdown {
h1 { (heading) } h1 { (title) }
} }
@for (year, group) in groups { @for (year, group) in groups {
@ -28,7 +24,7 @@ where
} }
); );
page(sack, list, title) page(title, list, None)
} }
fn section(year: i32, group: &[LinkDate]) -> impl Renderable + '_ { fn section(year: i32, group: &[LinkDate]) -> impl Renderable + '_ {

View file

@ -3,6 +3,7 @@ use hypertext::{html_elements, maud_move, GlobalAttributes, Raw, Renderable};
use crate::pipeline::{Sack, TreePage}; use crate::pipeline::{Sack, TreePage};
use crate::text::md::Outline; use crate::text::md::Outline;
/// Render the outline for a document /// Render the outline for a document
pub(crate) fn show_outline(outline: Outline) -> impl Renderable { pub(crate) fn show_outline(outline: Outline) -> impl Renderable {
maud_move!( maud_move!(

View file

@ -13,13 +13,13 @@ use camino::Utf8Path;
use chrono::Datelike; use chrono::Datelike;
use hypertext::{html_elements, maud, maud_move, GlobalAttributes, Raw, Renderable}; use hypertext::{html_elements, maud, maud_move, GlobalAttributes, Raw, Renderable};
use crate::REPO;
pub(crate) use home::home; pub(crate) use home::home;
pub(crate) use post::Post; pub(crate) use post::Post;
pub(crate) use slideshow::Slideshow; pub(crate) use slideshow::Slideshow;
pub(crate) use wiki::Wiki; pub(crate) use wiki::Wiki;
use crate::{pipeline::Sack, Mode};
const JS_RELOAD: &str = r#" const JS_RELOAD: &str = r#"
const socket = new WebSocket("ws://localhost:1337"); const socket = new WebSocket("ws://localhost:1337");
socket.addEventListener("message", (event) => { socket.addEventListener("message", (event) => {
@ -37,10 +37,7 @@ const JS_IMPORTS: &str = r#"
} }
"#; "#;
fn head<'s, 'html>(sack: &'s Sack, title: String) -> impl Renderable + 'html fn head(title: &str) -> impl Renderable + '_ {
where
's: 'html,
{
let title = format!("{} | kamoshi.org", title); let title = format!("{} | kamoshi.org", title);
maud_move!( maud_move!(
@ -63,9 +60,7 @@ where
script type="importmap" {(Raw(JS_IMPORTS))} script type="importmap" {(Raw(JS_IMPORTS))}
@if matches!(sack.ctx.mode, Mode::Watch) {
script { (Raw(JS_RELOAD)) } script { (Raw(JS_RELOAD)) }
}
) )
} }
@ -114,17 +109,14 @@ fn navbar() -> impl Renderable {
) )
} }
pub fn footer<'s, 'html>(sack: &'s Sack) -> impl Renderable + 'html pub fn footer(path: Option<&Utf8Path>) -> impl Renderable {
where let copy = format!("Copyright &copy; {} Maciej Jur", &REPO.year);
's: 'html,
{
let copy = format!("Copyright &copy; {} Maciej Jur", &sack.ctx.year);
let mail = "maciej@kamoshi.org"; let mail = "maciej@kamoshi.org";
let href = format!("mailto:{}", mail); let href = format!("mailto:{}", mail);
let link = Utf8Path::new(&sack.ctx.link) let link = Utf8Path::new(&REPO.link)
.join("src/commit") .join("src/commit")
.join(&sack.ctx.hash); .join(&REPO.hash);
let link = match sack.get_file() { let link = match path {
Some(path) => link.join(path), Some(path) => link.join(path),
None => link, None => link,
}; };
@ -141,10 +133,10 @@ where
} }
div .repo { div .repo {
a href=(link.as_str()) { a href=(link.as_str()) {
(&sack.ctx.hash) (&REPO.hash)
} }
div { div {
(&sack.ctx.date) (&REPO.date)
} }
} }
a .right.footer__cc-wrap rel="license" href="http://creativecommons.org/licenses/by/4.0/" { a .right.footer__cc-wrap rel="license" href="http://creativecommons.org/licenses/by/4.0/" {
@ -154,19 +146,15 @@ where
) )
} }
fn bare<'s, 'p, 'html>( fn bare<'data, 'html, R>(title: &'data str, main: R) -> impl Renderable + 'html
sack: &'s Sack, where
main: impl Renderable + 'p, 'data : 'html,
title: String, R: Renderable + 'data
) -> impl Renderable + 'html
where
's: 'html,
'p: 'html,
{ {
maud_move!( maud_move!(
(Raw("<!DOCTYPE html>")) (Raw("<!DOCTYPE html>"))
html lang="en" { html lang="en" {
(head(sack, title)) (head(title))
body { body {
(main) (main)
@ -175,30 +163,31 @@ where
) )
} }
fn page<'s, 'p, 'html>( fn page<'data, 'main, 'html, T>(
sack: &'s Sack, title: &'data str,
main: impl Renderable + 'p, main: T,
title: String, path: Option<&'data Utf8Path>,
) -> impl Renderable + 'html ) -> impl Renderable + 'html
where where
's: 'html, 'main : 'html,
'p: 'html, 'data : 'html,
T: Renderable + 'main
{ {
maud_move!( maud_move!(
(Raw("<!DOCTYPE html>")) (Raw("<!DOCTYPE html>"))
html lang="en" { html lang="en" {
(head(sack, title)) (head(title))
body { body {
(navbar()) (navbar())
(main) (main)
(footer(sack)) (footer(path))
} }
} }
) )
} }
pub(crate) fn to_list(sack: &Sack, list: Vec<crate::LinkDate>, title: String) -> String { pub(crate) fn to_list(list: Vec<crate::LinkDate>) -> String {
let mut groups = HashMap::<i32, Vec<_>>::new(); let mut groups = HashMap::<i32, Vec<_>>::new();
for page in list { for page in list {
@ -215,15 +204,12 @@ pub(crate) fn to_list(sack: &Sack, list: Vec<crate::LinkDate>, title: String) ->
groups.sort_by(|a, b| b.0.cmp(&a.0)); groups.sort_by(|a, b| b.0.cmp(&a.0));
list::list(sack, &groups, title).render().into() list::list("", &groups).render().into()
} }
pub(crate) fn map<'s, 'html>(sack: &'s Sack) -> impl Renderable + 'html pub(crate) fn map() -> impl Renderable {
where
's: 'html,
{
page( page(
sack, "Map",
maud!( maud!(
main { main {
div #map style="height: 100%; width: 100%" {} div #map style="height: 100%; width: 100%" {}
@ -233,20 +219,17 @@ where
} }
} }
), ),
String::from("Map"), None,
) )
} }
pub(crate) fn search<'s, 'html>(sack: &'s Sack) -> impl Renderable + 'html pub(crate) fn search() -> impl Renderable {
where
's: 'html,
{
page( page(
sack, "Search",
maud!( maud!(
main #app {} main #app {}
script type="module" src="/js/search/dist/search.js" {} script type="module" src="/js/search/dist/search.js" {}
), ),
String::from("Search"), None,
) )
} }

View file

@ -6,7 +6,7 @@ use serde::Deserialize;
use crate::pipeline::{Content, Sack}; use crate::pipeline::{Content, Sack};
use crate::text::md::Outline; use crate::text::md::Outline;
use crate::{LinkDate, Linkable}; use crate::{Linkable, LinkDate};
/// Represents a simple post. /// Represents a simple post.
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
@ -18,22 +18,24 @@ pub(crate) struct Post {
} }
impl Content for Post { impl Content for Post {
fn parse(data: String, lib: Option<&Library>) -> (Outline, String, Option<Vec<String>>) { fn parse(data: &str, lib: Option<&Library>) -> (Outline, String, Option<Vec<String>>) {
crate::text::md::parse(data, lib) crate::text::md::parse(data, lib)
} }
fn render<'s, 'p, 'html>( fn transform<'f, 'm, 's, 'html, T>(
self, &'f self,
sack: &'s Sack, content: T,
parsed: impl Renderable + 'p,
outline: Outline, outline: Outline,
sack: &'s Sack,
bib: Option<Vec<String>>, bib: Option<Vec<String>>,
) -> impl Renderable + 'html ) -> impl Renderable + 'html
where where
'f: 'html,
'm: 'html,
's: 'html, 's: 'html,
'p: 'html, T: Renderable + 'm,
{ {
post(self, sack, parsed, outline, bib) post(self, content, outline, bib, sack)
} }
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> { fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> {
@ -48,18 +50,19 @@ impl Content for Post {
} }
} }
pub fn post<'s, 'p, 'html>( pub fn post<'f, 'm, 's, 'html, T>(
fm: Post, fm: &'f Post,
sack: &'s Sack, content: T,
content: impl Renderable + 'p,
outline: Outline, outline: Outline,
bib: Option<Vec<String>>, bib: Option<Vec<String>>,
sack: &'s Sack,
) -> impl Renderable + 'html ) -> impl Renderable + 'html
where where
'f: 'html,
'm: 'html,
's: 'html, 's: 'html,
'p: 'html, T: Renderable + 'm
{ {
let heading = fm.title.clone();
let main = maud_move!( let main = maud_move!(
main .wiki-main { main .wiki-main {
@ -76,7 +79,7 @@ where
article .wiki-article /*class:list={classlist)*/ { article .wiki-article /*class:list={classlist)*/ {
header class="markdown" { header class="markdown" {
h1 #top { (heading) } h1 #top { (fm.title.clone()) }
} }
section .wiki-article__markdown.markdown { section .wiki-article__markdown.markdown {
(content) (content)
@ -89,5 +92,5 @@ where
} }
); );
crate::html::page(sack, main, fm.title.clone()) crate::html::page(&fm.title, main, sack.get_file())
} }

View file

@ -1,20 +1,13 @@
use camino::Utf8PathBuf; use camino::Utf8PathBuf;
use chrono::{DateTime, Utc}; use chrono::{DateTime, Utc};
use hayagriva::Library; use hayagriva::Library;
use hypertext::{html_elements, maud_move, GlobalAttributes, Raw, Renderable}; use hypertext::{html_elements, maud_move, Renderable, GlobalAttributes, Raw};
use serde::Deserialize; use serde::Deserialize;
use crate::pipeline::{Content, Sack}; use crate::pipeline::{Content, Sack};
use crate::text::md::Outline; use crate::text::md::Outline;
use crate::{Link, LinkDate, Linkable}; use crate::{Link, LinkDate, Linkable};
const CSS: &str = r#"
.slides img {
margin-left: auto;
margin-right: auto;
max-height: 60vh;
}
"#;
/// Represents a slideshow /// Represents a slideshow
#[derive(Deserialize, Debug, Clone)] #[derive(Deserialize, Debug, Clone)]
@ -26,42 +19,19 @@ pub(crate) struct Slideshow {
} }
impl Content for Slideshow { impl Content for Slideshow {
fn parse(data: String, _: Option<&Library>) -> (Outline, String, Option<Vec<String>>) { fn transform<'f, 'm, 's, 'html, T>(
let html = data &'f self,
.split("\n-----\n") content: T,
.map(|chunk| {
chunk
.split("\n---\n")
.map(|s| crate::text::md::parse(s.to_owned(), None))
.map(|e| e.1)
.collect::<Vec<_>>()
})
.map(|stack| match stack.len() > 1 {
true => format!(
"<section>{}</section>",
stack
.into_iter()
.map(|slide| format!("<section>{slide}</section>"))
.collect::<String>()
),
false => format!("<section>{}</section>", stack[0]),
})
.collect::<String>();
(Outline(vec![]), html, None)
}
fn render<'s, 'p, 'html>(
self,
sack: &'s Sack,
parsed: impl Renderable + 'p,
_: Outline, _: Outline,
_: Option<Vec<String>>, _: &'s Sack,
_bib: Option<Vec<String>>,
) -> impl Renderable + 'html ) -> impl Renderable + 'html
where where
'f: 'html,
'm: 'html,
's: 'html, 's: 'html,
'p: 'html, T: Renderable + 'm {
{ show(self, content)
show(self, sack, parsed)
} }
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> { fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> {
@ -74,20 +44,28 @@ impl Content for Slideshow {
date: self.date.to_owned(), date: self.date.to_owned(),
})) }))
} }
fn parse(data: &str, _: Option<&Library>) -> (Outline, String, Option<Vec<String>>) {
let html = data
.split("\n-----\n")
.map(|chunk| chunk.split("\n---\n").map(|s| crate::text::md::parse(s, None)).map(|e| e.1).collect::<Vec<_>>())
.map(|stack| match stack.len() > 1 {
true => format!("<section>{}</section>", stack.into_iter().map(|slide| format!("<section>{slide}</section>")).collect::<String>()),
false => format!("<section>{}</section>", stack[0])
})
.collect::<String>();
(Outline(vec![]), html, None)
}
} }
pub fn show<'s, 'p, 'html>( pub fn show<'data, 'show>(
fm: Slideshow, fm: &'data Slideshow,
sack: &'s Sack, slides: impl Renderable + 'data
slides: impl Renderable + 'p, ) -> impl Renderable + 'show
) -> impl Renderable + 'html where
where 'data: 'show
's: 'html,
'p: 'html,
{ {
crate::html::bare( crate::html::bare(&fm.title, maud_move!(
sack,
maud_move!(
div .reveal { div .reveal {
div .slides { div .slides {
(slides) (slides)
@ -98,8 +76,12 @@ where
(Raw("import 'reveal';")) (Raw("import 'reveal';"))
} }
style { (Raw(CSS)) } style {r#"
), .slides img {
fm.title.clone(), margin-left: auto;
) margin-right: auto;
max-height: 60vh;
}
"#}
))
} }

View file

@ -14,22 +14,19 @@ pub struct Wiki {
} }
impl Content for Wiki { impl Content for Wiki {
fn parse(data: String, lib: Option<&Library>) -> (Outline, String, Option<Vec<String>>) { fn transform<'f, 'm, 's, 'html, T>(
crate::text::md::parse(data, lib) &'f self,
} content: T,
fn render<'s, 'p, 'html>(
self,
sack: &'s Sack,
parsed: impl Renderable + 'p,
outline: Outline, outline: Outline,
sack: &'s Sack,
bib: Option<Vec<String>>, bib: Option<Vec<String>>,
) -> impl Renderable + 'html ) -> impl Renderable + 'html
where where
'f: 'html,
'm: 'html,
's: 'html, 's: 'html,
'p: 'html, T: Renderable + 'm {
{ wiki(self, content, outline, sack, bib)
wiki(self, sack, parsed, outline, bib)
} }
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> { fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> {
@ -39,20 +36,24 @@ impl Content for Wiki {
desc: None, desc: None,
})) }))
} }
fn parse(data: &str, lib: Option<&Library>) -> (Outline, String, Option<Vec<String>>) {
crate::text::md::parse(data, lib)
}
} }
fn wiki<'s, 'p, 'html>( fn wiki<'data, 'html, 'sack, T>(
matter: Wiki, fm: &'data Wiki,
sack: &'s Sack, content: T,
parsed: impl Renderable + 'p,
_: Outline, _: Outline,
sack: &'sack Sack,
bib: Option<Vec<String>>, bib: Option<Vec<String>>,
) -> impl Renderable + 'html ) -> impl Renderable + 'html
where where
's: 'html, 'sack: 'html,
'p: 'html, 'data: 'html,
T: Renderable + 'data
{ {
let heading = matter.title.clone();
let main = maud_move!( let main = maud_move!(
main .wiki-main { main .wiki-main {
@ -74,10 +75,10 @@ where
article .wiki-article /*class:list={classlist)*/ { article .wiki-article /*class:list={classlist)*/ {
header class="markdown" { header class="markdown" {
h1 #top { (heading) } h1 #top { (fm.title.clone()) }
} }
section .wiki-article__markdown.markdown { section .wiki-article__markdown.markdown {
(parsed) (content)
} }
@if let Some(bib) = bib { @if let Some(bib) = bib {
@ -87,5 +88,5 @@ where
} }
); );
crate::html::page(sack, main, matter.title.to_owned()) crate::html::page(&fm.title, main, sack.get_file())
} }

View file

@ -1,26 +1,25 @@
mod build; mod build;
mod html; mod html;
mod md;
mod pipeline; mod pipeline;
mod text; mod text;
mod ts; mod ts;
mod utils; mod utils;
mod watch; mod watch;
use std::collections::HashSet;
use std::fs; use std::fs;
use std::process::Command; use std::process::Command;
use camino::{Utf8Path, Utf8PathBuf}; use camino::{Utf8Path, Utf8PathBuf};
use chrono::{DateTime, Datelike, Utc}; use chrono::{DateTime, Datelike, Utc};
use clap::{Parser, ValueEnum}; use clap::{Parser, ValueEnum};
use gray_matter::engine::YAML; use pipeline::{Asset, AssetKind, Content, FileItemKind, Output, PipelineItem, Sack};
use gray_matter::Matter;
use hypertext::{Raw, Renderable}; use hypertext::{Raw, Renderable};
use pipeline::{Asset, AssetKind, Content, FileItemKind, Output, PipelineItem}; use once_cell::sync::Lazy;
use serde::Deserialize; use serde::Deserialize;
use crate::build::build_styles;
use crate::pipeline::Virtual; use crate::pipeline::Virtual;
use crate::build::build_styles;
#[derive(Parser, Debug, Clone)] #[derive(Parser, Debug, Clone)]
struct Args { struct Args {
@ -35,14 +34,35 @@ enum Mode {
} }
#[derive(Debug)] #[derive(Debug)]
struct BuildContext { struct BuildInfo {
pub mode: Mode,
pub year: i32, pub year: i32,
pub date: String, pub date: String,
pub link: String, pub link: String,
pub hash: String, pub hash: String,
} }
static REPO: Lazy<BuildInfo> = Lazy::new(|| {
let time = chrono::Utc::now();
BuildInfo {
year: time.year(),
date: time.format("%Y/%m/%d %H:%M").to_string(),
link: "https://git.kamoshi.org/kamov/website".into(),
hash: String::from_utf8(
Command::new("git")
.args(["rev-parse", "--short", "HEAD"])
.output()
.unwrap()
.stdout
)
.unwrap()
.trim()
.into()
}
});
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct Link { pub struct Link {
pub path: Utf8PathBuf, pub path: Utf8PathBuf,
@ -62,54 +82,88 @@ pub enum Linkable {
Date(LinkDate), Date(LinkDate),
} }
fn main() {
let args = Args::parse();
let time = chrono::Utc::now();
let ctx = BuildContext { fn to_index<T>(item: PipelineItem) -> PipelineItem
mode: args.mode, where
year: time.year(), T: for<'de> Deserialize<'de> + Content + 'static,
date: time.format("%Y/%m/%d %H:%M").to_string(), {
link: "https://git.kamoshi.org/kamov/website".into(), let meta = match item {
hash: String::from_utf8( PipelineItem::Skip(meta) if matches!(meta.kind, FileItemKind::Index) => meta,
Command::new("git") _ => return item,
.args(["rev-parse", "--short", "HEAD"])
.output()
.expect("Couldn't load git revision")
.stdout,
)
.expect("Invalid UTF8")
.trim()
.into(),
}; };
match args.mode { let dir = meta.path.parent().unwrap().strip_prefix("content").unwrap();
Mode::Build => { let dir = match meta.path.file_stem().unwrap() {
build(&ctx); "index" => dir.to_owned(),
} name => dir.join(name),
Mode::Watch => { };
build(&ctx); let path = dir.join("index.html");
watch::watch().unwrap()
} match meta.path.extension() {
Some("md" | "mdx" | "lhs") => {
let data = fs::read_to_string(&meta.path).unwrap();
let (fm, md) = md::preflight::<T>(&data);
let link = T::as_link(&fm, Utf8Path::new("/").join(dir));
let call = move |sack: &Sack| {
let lib = sack.get_library();
let (outline, html, bib) = T::parse(&md, lib);
T::transform(&fm, Raw(html), outline, sack, bib).render().into()
};
Output {
kind: Asset {
kind: pipeline::AssetKind::Html(Box::new(call)),
meta,
}.into(),
path,
link,
}.into()
},
_ => meta.into(),
} }
} }
struct Source { fn to_bundle(item: PipelineItem) -> PipelineItem {
path: &'static str, let meta = match item {
exts: HashSet<&'static str>, PipelineItem::Skip(meta) if matches!(meta.kind, FileItemKind::Bundle) => meta,
func: fn(PipelineItem) -> PipelineItem, _ => return item,
} };
impl Source { let path = meta.path.strip_prefix("content").unwrap().to_owned();
fn get(&self) -> Vec<PipelineItem> {
pipeline::gather(self.path, &self.exts) match meta.path.extension() {
.into_iter() // any image
.map(self.func) Some("jpg" | "png" | "gif") => {
.collect() Output {
kind: Asset {
kind: AssetKind::Image,
meta,
}.into(),
path,
link: None,
}.into()
},
// bibliography
Some("bib") => {
let data = fs::read_to_string(&meta.path).unwrap();
let data = hayagriva::io::from_biblatex_str(&data).unwrap();
Output {
kind: Asset {
kind: AssetKind::Bibtex(data),
meta,
}.into(),
path,
link: None,
}.into()
},
_ => meta.into(),
} }
} }
fn build(ctx: &BuildContext) {
fn build() {
if fs::metadata("dist").is_ok() { if fs::metadata("dist").is_ok() {
println!("Cleaning dist"); println!("Cleaning dist");
fs::remove_dir_all("dist").unwrap(); fs::remove_dir_all("dist").unwrap();
@ -117,38 +171,28 @@ fn build(ctx: &BuildContext) {
fs::create_dir("dist").unwrap(); fs::create_dir("dist").unwrap();
let sources = vec![ let assets: Vec<Output> = [
Source { pipeline::gather("content/about.md", &["md"].into())
path: "content/about.md", .into_iter()
exts: ["md"].into(), .map(to_index::<crate::html::Post> as fn(PipelineItem) -> PipelineItem),
func: as_index::<crate::html::Post>, pipeline::gather("content/posts/**/*", &["md", "mdx"].into())
}, .into_iter()
Source { .map(to_index::<crate::html::Post>),
path: "content/posts/**/*", pipeline::gather("content/slides/**/*", &["md", "lhs"].into())
exts: ["md", "mdx"].into(), .into_iter()
func: as_index::<crate::html::Post>, .map(to_index::<crate::html::Slideshow>),
}, pipeline::gather("content/wiki/**/*", &["md"].into())
Source { .into_iter()
path: "content/slides/**/*", .map(to_index::<crate::html::Wiki>),
exts: ["md", "lhs"].into(), ]
func: as_index::<crate::html::Slideshow>, .into_iter()
}, .flatten()
Source {
path: "content/wiki/**/*",
exts: ["md"].into(),
func: as_index::<crate::html::Wiki>,
},
];
let assets: Vec<Output> = sources
.iter()
.flat_map(Source::get)
.map(to_bundle) .map(to_bundle)
.filter_map(|item| match item { .filter_map(|item| match item {
PipelineItem::Skip(skip) => { PipelineItem::Skip(skip) => {
println!("Skipping {}", skip.path); println!("Skipping {}", skip.path);
None None
} },
PipelineItem::Take(take) => Some(take), PipelineItem::Take(take) => Some(take),
}) })
.collect(); .collect();
@ -157,48 +201,37 @@ fn build(ctx: &BuildContext) {
assets, assets,
vec![ vec![
Output { Output {
kind: Virtual::new(|sack| crate::html::map(sack).render().to_owned().into()).into(), kind: Virtual::new(|_| crate::html::map().render().to_owned().into()).into(),
path: "map/index.html".into(), path: "map/index.html".into(),
link: None, link: None,
}, },
Output { Output {
kind: Virtual::new(|sack| crate::html::search(sack).render().to_owned().into()) kind: Virtual::new(|_| crate::html::search().render().to_owned().into()).into(),
.into(),
path: "search/index.html".into(), path: "search/index.html".into(),
link: None, link: None,
}, },
Output { Output {
kind: Asset { kind: Asset {
kind: pipeline::AssetKind::html(|sack| { kind: pipeline::AssetKind::Html(Box::new(|_| {
let data = std::fs::read_to_string("content/index.md").unwrap(); let data = std::fs::read_to_string("content/index.md").unwrap();
let (_, html, _) = text::md::parse(data, None); let (_, html, _) = text::md::parse(&data, None);
crate::html::home(sack, Raw(html)) crate::html::home(Raw(html)).render().to_owned().into()
.render() })),
.to_owned()
.into()
}),
meta: pipeline::FileItem { meta: pipeline::FileItem {
kind: pipeline::FileItemKind::Index, kind: pipeline::FileItemKind::Index,
path: "content/index.md".into(), path: "content/index.md".into()
},
} }
.into(), }.into(),
path: "index.html".into(), path: "index.html".into(),
link: None, link: None,
}, },
Output { Output {
kind: Virtual::new(|sack| { kind: Virtual::new(|sack| crate::html::to_list(sack.get_links("posts/**/*.html"))).into(),
crate::html::to_list(sack, sack.get_links("posts/**/*.html"), "Posts".into())
})
.into(),
path: "posts/index.html".into(), path: "posts/index.html".into(),
link: None, link: None,
}, },
Output { Output {
kind: Virtual::new(|sack| { kind: Virtual::new(|sack| crate::html::to_list(sack.get_links("slides/**/*.html"))).into(),
crate::html::to_list(sack, sack.get_links("slides/**/*.html"), "Slideshows".into())
})
.into(),
path: "slides/index.html".into(), path: "slides/index.html".into(),
link: None, link: None,
}, },
@ -210,7 +243,7 @@ fn build(ctx: &BuildContext) {
{ {
let now = std::time::Instant::now(); let now = std::time::Instant::now();
pipeline::render_all(ctx, &assets); pipeline::render_all(&assets);
println!("Elapsed: {:.2?}", now.elapsed()); println!("Elapsed: {:.2?}", now.elapsed());
} }
@ -240,100 +273,14 @@ fn build(ctx: &BuildContext) {
println!("{}", String::from_utf8(res.stderr).unwrap()); println!("{}", String::from_utf8(res.stderr).unwrap());
} }
pub fn parse_frontmatter<T>(raw: &str) -> (T, String) fn main() {
where let args = Args::parse();
T: for<'de> Deserialize<'de>,
{
let matter = Matter::<YAML>::new();
let result = matter.parse(raw);
( match args.mode {
// Just the front matter Mode::Build => build(),
result.data.unwrap().deserialize::<T>().unwrap(), Mode::Watch => {
// The rest of the content build();
result.content, watch::watch().unwrap()
) },
}
fn as_index<T>(item: PipelineItem) -> PipelineItem
where
T: for<'de> Deserialize<'de> + Content + Clone + 'static,
{
let meta = match item {
PipelineItem::Skip(e) if matches!(e.kind, FileItemKind::Index) => e,
_ => return item,
};
let dir = meta.path.parent().unwrap().strip_prefix("content").unwrap();
let dir = match meta.path.file_stem().unwrap() {
"index" => dir.to_owned(),
name => dir.join(name),
};
let path = dir.join("index.html");
match meta.path.extension() {
Some("md" | "mdx" | "lhs") => {
let data = fs::read_to_string(&meta.path).unwrap();
let (fm, md) = parse_frontmatter::<T>(&data);
let link = T::as_link(&fm, Utf8Path::new("/").join(dir));
Output {
kind: Asset {
kind: pipeline::AssetKind::html(move |sack| {
let lib = sack.get_library();
let (outline, parsed, bib) = T::parse(md.clone(), lib);
T::render(fm.clone(), sack, Raw(parsed), outline, bib)
.render()
.into()
}),
meta,
}
.into(),
path,
link,
}
.into()
}
_ => meta.into(),
}
}
fn to_bundle(item: PipelineItem) -> PipelineItem {
let meta = match item {
PipelineItem::Skip(meta) if matches!(meta.kind, FileItemKind::Bundle) => meta,
_ => return item,
};
let path = meta.path.strip_prefix("content").unwrap().to_owned();
match meta.path.extension() {
// any image
Some("jpg" | "png" | "gif") => Output {
kind: Asset {
kind: AssetKind::Image,
meta,
}
.into(),
path,
link: None,
}
.into(),
// bibliography
Some("bib") => {
let data = fs::read_to_string(&meta.path).unwrap();
let data = hayagriva::io::from_biblatex_str(&data).unwrap();
Output {
kind: Asset {
kind: AssetKind::Bibtex(data),
meta,
}
.into(),
path,
link: None,
}
.into()
}
_ => meta.into(),
} }
} }

43
src/md/matter.rs Normal file
View file

@ -0,0 +1,43 @@
use gray_matter::{engine::YAML, Matter};
use serde::Deserialize;
pub fn preflight<T>(raw: &str) -> (T, String)
where
T: for<'de> Deserialize<'de>,
{
let matter = Matter::<YAML>::new();
let result = matter.parse(raw);
(
// Just the front matter
result.data.unwrap().deserialize::<T>().unwrap(),
// The actual markdown content
result.content,
)
}
mod isodate {
use chrono::{DateTime, Utc};
use serde::{self, Deserialize, Deserializer};
// pub fn serialize<S>(
// date: &DateTime<Utc>,
// serializer: S,
// ) -> Result<S::Ok, S::Error>
// where
// S: Serializer,
// {
// let s = date.to_rfc3339();
// serializer.serialize_str(&s)
// }
pub fn deserialize<'de, D>(deserializer: D) -> Result<DateTime<Utc>, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
let dt = chrono::DateTime::parse_from_rfc3339(&s).map_err(serde::de::Error::custom)?;
Ok(dt.into())
}
}

3
src/md/mod.rs Normal file
View file

@ -0,0 +1,3 @@
mod matter;
pub use matter::preflight;

View file

@ -11,7 +11,7 @@ use hayagriva::Library;
use hypertext::Renderable; use hypertext::Renderable;
use crate::text::md::Outline; use crate::text::md::Outline;
use crate::{BuildContext, Link, LinkDate, Linkable}; use crate::{Link, LinkDate, Linkable};
/// Represents a piece of content that can be rendered as a page. This trait needs to be /// Represents a piece of content that can be rendered as a page. This trait needs to be
/// implemented for the front matter associated with some web page as that is what ultimately /// implemented for the front matter associated with some web page as that is what ultimately
@ -19,23 +19,21 @@ use crate::{BuildContext, Link, LinkDate, Linkable};
/// rendered page on the website. /// rendered page on the website.
pub(crate) trait Content { pub(crate) trait Content {
/// Parse the document. Pass an optional library for bibliography. /// Parse the document. Pass an optional library for bibliography.
/// This generates the initial HTML markup from content. fn parse(document: &str, library: Option<&Library>) -> (Outline, String, Option<Vec<String>>);
fn parse(document: String, library: Option<&Library>)
-> (Outline, String, Option<Vec<String>>);
/// Render the full page from parsed content. fn transform<'fm, 'md, 'sack, 'html, T>(
fn render<'s, 'p, 'html>( &'fm self,
self, content: T,
sack: &'s Sack,
parsed: impl Renderable + 'p,
outline: Outline, outline: Outline,
sack: &'sack Sack,
bib: Option<Vec<String>>, bib: Option<Vec<String>>,
) -> impl Renderable + 'html ) -> impl Renderable + 'html
where where
's: 'html, 'fm: 'html,
'p: 'html; 'md: 'html,
'sack: 'html,
T: Renderable + 'md;
/// Get link for this content
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable>; fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable>;
} }
@ -68,12 +66,6 @@ pub(crate) enum AssetKind {
Image, Image,
} }
impl AssetKind {
pub fn html(f: impl Fn(&Sack) -> String + 'static) -> Self {
Self::Html(Box::new(f))
}
}
/// Asset corresponding to a file on disk. /// Asset corresponding to a file on disk.
pub(crate) struct Asset { pub(crate) struct Asset {
/// The kind of a processed asset. /// The kind of a processed asset.
@ -146,7 +138,6 @@ impl From<Output> for PipelineItem {
/// This struct allows for querying the website hierarchy. It is passed to each rendered website /// This struct allows for querying the website hierarchy. It is passed to each rendered website
/// page, so that it can easily access the website metadata. /// page, so that it can easily access the website metadata.
pub(crate) struct Sack<'a> { pub(crate) struct Sack<'a> {
pub ctx: &'a BuildContext,
/// Literally all of the content /// Literally all of the content
hole: &'a [Output], hole: &'a [Output],
/// Current path for the page being rendered /// Current path for the page being rendered
@ -156,6 +147,10 @@ pub(crate) struct Sack<'a> {
} }
impl<'a> Sack<'a> { impl<'a> Sack<'a> {
pub fn new(hole: &'a [Output], path: &'a Utf8PathBuf, file: Option<&'a Utf8PathBuf>) -> Self {
Self { hole, path, file }
}
pub fn get_links(&self, path: &str) -> Vec<LinkDate> { pub fn get_links(&self, path: &str) -> Vec<LinkDate> {
let pattern = glob::Pattern::new(path).expect("Bad glob pattern"); let pattern = glob::Pattern::new(path).expect("Bad glob pattern");
self.hole self.hole
@ -265,25 +260,17 @@ fn to_source(path: Utf8PathBuf, exts: &HashSet<&'static str>) -> FileItem {
FileItem { kind, path } FileItem { kind, path }
} }
pub fn render_all(ctx: &BuildContext, items: &[Output]) { pub fn render_all(items: &[Output]) {
for item in items { for item in items {
let file = match &item.kind { let file = match &item.kind {
OutputKind::Asset(a) => Some(&a.meta.path), OutputKind::Asset(a) => Some(&a.meta.path),
OutputKind::Virtual(_) => None, OutputKind::Virtual(_) => None,
}; };
render( render(item, &Sack::new(items, &item.path, file));
item,
Sack {
ctx,
hole: items,
path: &item.path,
file,
},
);
} }
} }
fn render(item: &Output, sack: Sack) { fn render(item: &Output, sack: &Sack) {
let o = Utf8Path::new("dist").join(&item.path); let o = Utf8Path::new("dist").join(&item.path);
fs::create_dir_all(o.parent().unwrap()).unwrap(); fs::create_dir_all(o.parent().unwrap()).unwrap();
@ -294,7 +281,7 @@ fn render(item: &Output, sack: Sack) {
match &real.kind { match &real.kind {
AssetKind::Html(closure) => { AssetKind::Html(closure) => {
let mut file = File::create(&o).unwrap(); let mut file = File::create(&o).unwrap();
file.write_all(closure(&sack).as_bytes()).unwrap(); file.write_all(closure(sack).as_bytes()).unwrap();
println!("HTML: {} -> {}", i, o); println!("HTML: {} -> {}", i, o);
} }
AssetKind::Bibtex(_) => {} AssetKind::Bibtex(_) => {}
@ -307,7 +294,7 @@ fn render(item: &Output, sack: Sack) {
} }
OutputKind::Virtual(Virtual(ref closure)) => { OutputKind::Virtual(Virtual(ref closure)) => {
let mut file = File::create(&o).unwrap(); let mut file = File::create(&o).unwrap();
file.write_all(closure(&sack).as_bytes()).unwrap(); file.write_all(closure(sack).as_bytes()).unwrap();
println!("Virtual: -> {}", o); println!("Virtual: -> {}", o);
} }
} }

View file

@ -1,11 +1,6 @@
use std::collections::HashMap; use std::collections::HashMap;
use hayagriva::{ use hayagriva::{archive::ArchivedStyle, citationberg::{IndependentStyle, Locale, Style}, BibliographyDriver, BibliographyRequest, BufWriteFormat, CitationItem, CitationRequest, Library};
archive::ArchivedStyle,
citationberg::{IndependentStyle, Locale, Style},
BibliographyDriver, BibliographyRequest, BufWriteFormat, CitationItem, CitationRequest,
Library,
};
use hypertext::Renderable; use hypertext::Renderable;
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd, TextMergeStream}; use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag, TagEnd, TextMergeStream};
@ -13,50 +8,52 @@ use regex::Regex;
use crate::ts; use crate::ts;
static OPTS: Lazy<Options> = Lazy::new(|| {
static OPTS: Lazy<Options> = Lazy::new(||
Options::empty() Options::empty()
.union(Options::ENABLE_MATH) .union(Options::ENABLE_MATH)
.union(Options::ENABLE_TABLES) .union(Options::ENABLE_TABLES)
.union(Options::ENABLE_TASKLISTS) .union(Options::ENABLE_TASKLISTS)
.union(Options::ENABLE_STRIKETHROUGH) .union(Options::ENABLE_STRIKETHROUGH)
.union(Options::ENABLE_SMART_PUNCTUATION) .union(Options::ENABLE_SMART_PUNCTUATION)
}); );
static KATEX_I: Lazy<katex::Opts> = Lazy::new(|| { static KATEX_I: Lazy<katex::Opts> = Lazy::new(||
katex::opts::Opts::builder() katex::opts::Opts::builder()
.output_type(katex::OutputType::Mathml) .output_type(katex::OutputType::Mathml)
.build() .build()
.unwrap() .unwrap()
}); );
static KATEX_B: Lazy<katex::Opts> = Lazy::new(|| { static KATEX_B: Lazy<katex::Opts> = Lazy::new(||
katex::opts::Opts::builder() katex::opts::Opts::builder()
.output_type(katex::OutputType::Mathml) .output_type(katex::OutputType::Mathml)
.display_mode(true) .display_mode(true)
.build() .build()
.unwrap() .unwrap()
}); );
static LOCALE: Lazy<Vec<Locale>> = Lazy::new(hayagriva::archive::locales); static LOCALE: Lazy<Vec<Locale>> = Lazy::new(hayagriva::archive::locales);
static STYLE: Lazy<IndependentStyle> = static STYLE: Lazy<IndependentStyle> = Lazy::new(||
Lazy::new(|| match ArchivedStyle::InstituteOfPhysicsNumeric.get() { match ArchivedStyle::InstituteOfPhysicsNumeric.get() {
Style::Independent(style) => style, Style::Independent(style) => style,
Style::Dependent(_) => unreachable!(), Style::Dependent(_) => unreachable!(),
}); }
);
pub struct Outline(pub Vec<(String, String)>); pub struct Outline(pub Vec<(String, String)>);
pub fn parse(text: String, lib: Option<&Library>) -> (Outline, String, Option<Vec<String>>) {
pub fn parse(text: &str, lib: Option<&Library>) -> (Outline, String, Option<Vec<String>>) {
let (outline, stream) = { let (outline, stream) = {
let stream = Parser::new_ext(&text, *OPTS); let stream = Parser::new_ext(text, *OPTS);
let mut stream: Vec<_> = TextMergeStream::new(stream).collect(); let mut stream: Vec<_> = TextMergeStream::new(stream).collect();
let outline = set_heading_ids(&mut stream); let outline = set_heading_ids(&mut stream);
(outline, stream) (outline, stream)
}; };
let stream = stream let stream = stream.into_iter()
.into_iter()
.map(make_math) .map(make_math)
.map(make_emoji) .map(make_emoji)
.collect::<Vec<_>>(); .collect::<Vec<_>>();
@ -78,20 +75,13 @@ pub fn parse(text: String, lib: Option<&Library>) -> (Outline, String, Option<Ve
(outline, html, bib) (outline, html, bib)
} }
fn make_bib<'a, 'b>( fn make_bib<'a, 'b>(stream: Vec<Event<'a>>, lib: &'b Library) -> (Vec<Event<'a>>, Option<Vec<String>>) {
stream: Vec<Event<'a>>,
lib: &'b Library,
) -> (Vec<Event<'a>>, Option<Vec<String>>) {
let mut driver = BibliographyDriver::new(); let mut driver = BibliographyDriver::new();
for event in stream.iter() { for event in stream.iter() {
match event { match event {
Event::InlineMath(ref text) => match lib.get(text) { Event::InlineMath(ref text) => match lib.get(text) {
Some(entry) => driver.citation(CitationRequest::from_items( Some(entry) => driver.citation(CitationRequest::from_items(vec![CitationItem::with_entry(entry)], &STYLE, &LOCALE)),
vec![CitationItem::with_entry(entry)],
&STYLE,
&LOCALE,
)),
None => (), None => (),
}, },
_ => (), _ => (),
@ -99,51 +89,36 @@ fn make_bib<'a, 'b>(
} }
// add fake citation to make all entries show up // add fake citation to make all entries show up
driver.citation(CitationRequest::from_items( driver.citation(CitationRequest::from_items(lib.iter().map(CitationItem::with_entry).collect(), &STYLE, &LOCALE));
lib.iter().map(CitationItem::with_entry).collect(),
&STYLE,
&LOCALE,
));
let res = driver.finish(BibliographyRequest { let res = driver.finish(BibliographyRequest { style: &STYLE, locale: None, locale_files: &LOCALE });
style: &STYLE,
locale: None,
locale_files: &LOCALE,
});
let mut n = 0; let mut n = 0;
let stream = stream let stream = stream.into_iter()
.into_iter()
.map(|event| match event { .map(|event| match event {
Event::InlineMath(name) => { Event::InlineMath(name) => {
let mut buffer = String::from("<cite>"); let mut buffer = String::from("<cite>");
match res.citations.get(n) { match res.citations.get(n) {
Some(rf) => rf Some(rf) => rf.citation.write_buf(&mut buffer, BufWriteFormat::Html).unwrap(),
.citation
.write_buf(&mut buffer, BufWriteFormat::Html)
.unwrap(),
None => buffer.push_str(&name), None => buffer.push_str(&name),
}; };
buffer.push_str("</cite>"); buffer.push_str("</cite>");
n += 1; n += 1;
Event::InlineHtml(buffer.into()) Event::InlineHtml(buffer.into())
} },
_ => event, _ => event
}) })
.collect(); .collect();
let bib = res.bibliography.map(|bib| { let bib = res.bibliography.map(|bib|
bib.items bib.items.iter()
.iter()
.map(|x| { .map(|x| {
let mut buffer = String::new(); let mut buffer = String::new();
x.content x.content.write_buf(&mut buffer, BufWriteFormat::Html).unwrap();
.write_buf(&mut buffer, BufWriteFormat::Html)
.unwrap();
buffer buffer
}) })
.collect::<Vec<_>>() .collect::<Vec<_>>()
}); );
(stream, bib) (stream, bib)
} }
@ -181,13 +156,15 @@ fn annotate_(input: &str) -> Vec<Annotated_> {
fn make_cite(event: Event) -> Vec<Event> { fn make_cite(event: Event) -> Vec<Event> {
match event { match event {
Event::Text(ref text) => annotate_(text) Event::Text(ref text) => {
annotate_(text)
.into_iter() .into_iter()
.map(|e| match e { .map(|e| match e {
Annotated_::Text(text) => Event::Text(text.to_owned().into()), Annotated_::Text(text) => Event::Text(text.to_owned().into()),
Annotated_::Cite(cite) => Event::InlineMath(cite.to_owned().into()), Annotated_::Cite(cite) => Event::InlineMath(cite.to_owned().into()),
}) })
.collect(), .collect()
},
_ => vec![event], _ => vec![event],
} }
} }
@ -200,45 +177,38 @@ fn set_heading_ids(events: &mut [Event]) -> Outline {
for event in events { for event in events {
match event { match event {
Event::Start(ref mut tag @ Tag::Heading { .. }) => { Event::Start(ref mut tag @ Tag::Heading {..}) => {
ptr = Some(tag); ptr = Some(tag);
} },
Event::Text(ref text) if ptr.is_some() => buf.push_str(text), Event::Text(ref text) if ptr.is_some() => {
buf.push_str(text)
},
Event::End(TagEnd::Heading(..)) => { Event::End(TagEnd::Heading(..)) => {
let txt = std::mem::take(&mut buf); let txt = std::mem::take(&mut buf);
let url = txt.to_lowercase().replace(' ', "-"); let url = txt.to_lowercase().replace(' ', "-");
let url = match cnt.get_mut(&url) { let url = match cnt.get_mut(&url) {
Some(ptr) => { Some(ptr) => { *ptr += 1; format!("{url}-{ptr}") },
*ptr += 1; None => { cnt.insert(url.clone(), 0); url },
format!("{url}-{ptr}")
}
None => {
cnt.insert(url.clone(), 0);
url
}
}; };
match ptr.take().unwrap() { match ptr.take().unwrap() {
Tag::Heading { ref mut id, .. } => *id = Some(url.clone().into()), Tag::Heading { ref mut id, .. } => *id = Some(url.clone().into()),
_ => unreachable!(), _ => unreachable!(),
} }
out.push((txt, url)); out.push((txt, url));
} },
_ => (), _ => (),
} }
} };
Outline(out) Outline(out)
} }
fn make_math(event: Event) -> Event { fn make_math(event: Event) -> Event {
match event { match event {
Event::InlineMath(math) => { Event::InlineMath(math) => Event::InlineHtml(katex::render_with_opts(&math, &*KATEX_I).unwrap().into()),
Event::InlineHtml(katex::render_with_opts(&math, &*KATEX_I).unwrap().into()) Event::DisplayMath(math) => Event::Html(katex::render_with_opts(&math, &*KATEX_B).unwrap().into()),
} _ => event
Event::DisplayMath(math) => {
Event::Html(katex::render_with_opts(&math, &*KATEX_B).unwrap().into())
}
_ => event,
} }
} }
@ -258,19 +228,21 @@ fn make_code(es: Vec<Event>) -> Vec<Event> {
let html = ts::highlight(&lang, &code).render().as_str().to_owned(); let html = ts::highlight(&lang, &code).render().as_str().to_owned();
buff.push(Event::Html(html.into())); buff.push(Event::Html(html.into()));
code.clear(); code.clear();
} },
Event::Text(text) => match lang { Event::Text(text) => match lang {
None => buff.push(Event::Text(text)), None => buff.push(Event::Text(text)),
Some(_) => code.push_str(&text), Some(_) => code.push_str(&text),
}, },
_ => buff.push(event), _ => buff.push(event)
} }
} }
buff buff
} }
static RE_RUBY: Lazy<Regex> = Lazy::new(|| Regex::new(r"\[([^\]]+)\]\{([^}]+)\}").unwrap()); static RE_RUBY: Lazy<Regex> = Lazy::new(||
Regex::new(r"\[([^\]]+)\]\{([^}]+)\}").unwrap()
);
#[derive(Debug)] #[derive(Debug)]
enum Annotated<'a> { enum Annotated<'a> {
@ -278,6 +250,7 @@ enum Annotated<'a> {
Ruby(&'a str, &'a str), Ruby(&'a str, &'a str),
} }
fn annotate(input: &str) -> Vec<Annotated> { fn annotate(input: &str) -> Vec<Annotated> {
let mut parts: Vec<Annotated> = Vec::new(); let mut parts: Vec<Annotated> = Vec::new();
let mut last_index = 0; let mut last_index = 0;
@ -308,9 +281,7 @@ fn make_ruby(event: Event) -> Vec<Event> {
.into_iter() .into_iter()
.map(|el| match el { .map(|el| match el {
Annotated::Text(text) => Event::Text(text.to_owned().into()), Annotated::Text(text) => Event::Text(text.to_owned().into()),
Annotated::Ruby(t, f) => Event::InlineHtml( Annotated::Ruby(t, f) => Event::InlineHtml(format!("<ruby>{t}<rp>(</rp><rt>{f}</rt><rp>)</rp></ruby>").into()),
format!("<ruby>{t}<rp>(</rp><rt>{f}</rt><rp>)</rp></ruby>").into(),
),
}) })
.collect(), .collect(),
_ => vec![event], _ => vec![event],
@ -329,7 +300,7 @@ fn make_emoji(event: Event) -> Event {
if let Some(emoji) = emojis::get_by_shortcode(key) { if let Some(emoji) = emojis::get_by_shortcode(key) {
let buf = buf.get_or_insert_with(|| String::with_capacity(text.len())); let buf = buf.get_or_insert_with(|| String::with_capacity(text.len()));
buf.push_str(&text[top..old - 1]); buf.push_str(&text[top..old-1]);
buf.push_str(emoji.as_str()); buf.push_str(emoji.as_str());
top = idx + 1; top = idx + 1;
} }
@ -343,9 +314,9 @@ fn make_emoji(event: Event) -> Event {
match buf { match buf {
None => event, None => event,
Some(buf) => Event::Text(buf.into()), Some(buf) => Event::Text(buf.into())
}
} }
},
_ => event, _ => event,
} }
} }

View file

@ -1,10 +1,11 @@
use std::borrow::Cow;
use hypertext::{html_elements, maud_move, Raw, Renderable, GlobalAttributes};
use tree_sitter_highlight::{Highlighter, HighlightEvent};
mod captures; mod captures;
mod configs; mod configs;
use std::borrow::Cow;
use hypertext::{html_elements, maud_move, GlobalAttributes, Raw, Renderable};
use tree_sitter_highlight::{HighlightEvent, Highlighter};
pub enum Event { pub enum Event {
Write(String), Write(String),
@ -12,9 +13,13 @@ pub enum Event {
Close, Close,
} }
pub fn highlight<'data, 'html>(lang: &'data str, code: &'data str) -> impl Renderable + 'html
where pub fn highlight<'data, 'html>(
'data: 'html, lang: &'data str,
code: &'data str
) -> impl Renderable + 'html
where
'data: 'html
{ {
maud_move!( maud_move!(
figure .listing.kanagawa data-lang=(lang) { figure .listing.kanagawa data-lang=(lang) {
@ -32,13 +37,11 @@ fn to_html(lang: &str, code: &str) -> String {
.into_iter() .into_iter()
.map(|event| match event { .map(|event| match event {
Event::Write(text) => Cow::from( Event::Write(text) => Cow::from(
text.replace('&', "&amp;") text.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;")
.replace('<', "&lt;") ),
.replace('>', "&gt;"), Event::Enter(class) => Cow::from(
format!("<span class=\"{}\">", class.replace('.', "-"))
), ),
Event::Enter(class) => {
Cow::from(format!("<span class=\"{}\">", class.replace('.', "-")))
}
Event::Close => Cow::from("</span>"), Event::Close => Cow::from("</span>"),
}) })
.collect() .collect()
@ -47,15 +50,17 @@ fn to_html(lang: &str, code: &str) -> String {
fn get_events(lang: &str, src: &str) -> Vec<Event> { fn get_events(lang: &str, src: &str) -> Vec<Event> {
let config = match configs::get_config(lang) { let config = match configs::get_config(lang) {
Some(c) => c, Some(c) => c,
None => return vec![Event::Write(src.into())], None => return vec![Event::Write(src.into())]
}; };
let mut hl = Highlighter::new(); let mut hl = Highlighter::new();
let highlights = hl let highlights = hl.highlight(
.highlight(config, src.as_bytes(), None, |name| { config,
configs::get_config(name) src.as_bytes(),
}) None,
.unwrap(); |name| configs::get_config(name)
).unwrap();
let mut out = vec![]; let mut out = vec![];
for event in highlights { for event in highlights {
@ -68,7 +73,7 @@ fn get_events(lang: &str, src: &str) -> Vec<Event> {
fn map_event(event: HighlightEvent, src: &str) -> Event { fn map_event(event: HighlightEvent, src: &str) -> Event {
match event { match event {
HighlightEvent::Source { start, end } => Event::Write(src[start..end].into()), HighlightEvent::Source {start, end} => Event::Write(src[start..end].into()),
HighlightEvent::HighlightStart(s) => Event::Enter(captures::NAMES[s.0].into()), HighlightEvent::HighlightStart(s) => Event::Enter(captures::NAMES[s.0].into()),
HighlightEvent::HighlightEnd => Event::Close, HighlightEvent::HighlightEnd => Event::Close,
} }

View file

@ -45,14 +45,6 @@
font-family: var(--serif); font-family: var(--serif);
font-weight: 500; font-weight: 500;
} }
&__latest {
padding-left: 1em;
a {
text-decoration: none;
color: var(--c-primary);
}
}
} }
.home-card-image { .home-card-image {