website/src/html/slideshow.rs

91 lines
1.9 KiB
Rust
Raw Normal View History

2024-07-22 23:08:11 +02:00
use camino::{Utf8Path, Utf8PathBuf};
2024-07-03 23:34:31 +02:00
use chrono::{DateTime, Utc};
2024-07-22 23:08:11 +02:00
use hauchiwa::{Bibliography, Content, Link, LinkDate, Linkable, Outline, Sack};
2024-07-03 23:34:31 +02:00
use hayagriva::Library;
2024-07-22 23:08:11 +02:00
use hypertext::{html_elements, maud, GlobalAttributes, Raw, Renderable};
2024-07-03 23:34:31 +02:00
use serde::Deserialize;
2024-07-05 13:59:07 +02:00
const CSS: &str = r#"
.slides img {
margin-left: auto;
margin-right: auto;
max-height: 60vh;
}
"#;
2024-07-03 23:34:31 +02:00
/// Represents a slideshow
#[derive(Deserialize, Debug, Clone)]
pub(crate) struct Slideshow {
2024-07-05 13:59:07 +02:00
pub title: String,
#[serde(with = "super::isodate")]
pub date: DateTime<Utc>,
pub desc: Option<String>,
2024-07-03 23:34:31 +02:00
}
impl Content for Slideshow {
2024-07-22 23:08:11 +02:00
fn parse_content(
content: &str,
sack: &Sack,
path: &Utf8Path,
library: Option<&Library>,
) -> (String, Outline, Bibliography) {
let parsed = content
2024-07-05 13:59:07 +02:00
.split("\n-----\n")
.map(|chunk| {
chunk
.split("\n---\n")
2024-07-22 23:08:11 +02:00
.map(|slide| crate::text::md::parse(&slide, sack, path, library).0)
2024-07-05 13:59:07 +02:00
.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>();
2024-07-22 23:08:11 +02:00
(parsed, Outline(vec![]), Bibliography(None))
2024-07-05 13:59:07 +02:00
}
2024-07-03 23:34:31 +02:00
2024-07-22 23:08:11 +02:00
fn as_html(&self, parsed: &str, sack: &Sack, _: Outline, _: Bibliography) -> String {
2024-07-05 13:59:07 +02:00
show(self, sack, parsed)
}
2024-07-03 23:34:31 +02:00
2024-07-05 13:59:07 +02:00
fn as_link(&self, path: Utf8PathBuf) -> Option<Linkable> {
Some(Linkable::Date(LinkDate {
link: Link {
path,
name: self.title.to_owned(),
desc: self.desc.to_owned(),
},
date: self.date.to_owned(),
}))
}
2024-07-03 23:34:31 +02:00
}
2024-07-22 23:08:11 +02:00
pub fn show(fm: &Slideshow, sack: &Sack, slides: &str) -> String {
2024-07-05 13:59:07 +02:00
crate::html::bare(
sack,
2024-07-22 23:08:11 +02:00
maud!(
2024-07-05 13:59:07 +02:00
div .reveal {
div .slides {
2024-07-22 23:08:11 +02:00
(Raw(slides))
2024-07-05 13:59:07 +02:00
}
}
2024-07-03 23:34:31 +02:00
2024-07-05 13:59:07 +02:00
script type="module" {
2024-07-22 23:08:11 +02:00
(Raw("import 'reveal'; import 'search';"))
2024-07-05 13:59:07 +02:00
}
2024-07-03 23:34:31 +02:00
2024-07-05 13:59:07 +02:00
style { (Raw(CSS)) }
),
fm.title.clone(),
)
2024-07-22 23:08:11 +02:00
.render()
.into()
2024-07-03 23:34:31 +02:00
}