website/src/html/post.rs

98 lines
2.1 KiB
Rust
Raw Normal View History

2024-07-07 13:09:14 +02:00
use std::collections::HashMap;
2024-07-03 23:34:31 +02:00
use camino::Utf8PathBuf;
use chrono::{DateTime, Utc};
2024-07-20 17:08:11 +02:00
use hauchiwa::{Content, Link, LinkDate, Linkable, Outline, Sack};
2024-07-03 23:34:31 +02:00
use hayagriva::Library;
2024-04-13 15:26:52 +02:00
use hypertext::{html_elements, maud_move, GlobalAttributes, Renderable};
2024-07-03 23:34:31 +02:00
use serde::Deserialize;
2024-04-28 00:26:19 +02:00
2024-07-03 23:34:31 +02:00
/// Represents a simple post.
#[derive(Deserialize, Debug, Clone)]
pub(crate) struct Post {
2024-07-05 13:59:07 +02:00
pub(crate) title: String,
#[serde(with = "super::isodate")]
pub(crate) date: DateTime<Utc>,
pub(crate) desc: Option<String>,
2024-07-03 23:34:31 +02:00
}
impl Content for Post {
2024-07-07 13:09:14 +02:00
fn parse(
data: String,
lib: Option<&Library>,
dir: Utf8PathBuf,
hash: HashMap<Utf8PathBuf, Utf8PathBuf>,
) -> (Outline, String, Option<Vec<String>>) {
crate::text::md::parse(data, lib, dir, hash)
2024-07-05 13:59:07 +02:00
}
2024-07-03 23:34:31 +02:00
2024-07-05 13:59:07 +02:00
fn render<'s, 'p, 'html>(
self,
sack: &'s Sack,
parsed: impl Renderable + 'p,
outline: Outline,
bib: Option<Vec<String>>,
) -> impl Renderable + 'html
where
's: 'html,
'p: 'html,
{
post(self, sack, parsed, outline, bib)
}
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 {
2024-07-20 17:08:11 +02:00
link: Link {
2024-07-05 13:59:07 +02:00
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-04-13 15:26:52 +02:00
2024-07-05 13:59:07 +02:00
pub fn post<'s, 'p, 'html>(
fm: Post,
sack: &'s Sack,
content: impl Renderable + 'p,
outline: Outline,
bib: Option<Vec<String>>,
2024-05-02 15:26:08 +02:00
) -> impl Renderable + 'html
2024-07-05 13:59:07 +02:00
where
's: 'html,
'p: 'html,
2024-04-13 15:26:52 +02:00
{
2024-07-05 13:59:07 +02:00
let heading = fm.title.clone();
let main = maud_move!(
main .wiki-main {
2024-04-13 15:26:52 +02:00
2024-07-05 13:59:07 +02:00
// Slide in/out for mobile
input #wiki-aside-shown type="checkbox" hidden;
2024-04-13 15:26:52 +02:00
2024-07-05 13:59:07 +02:00
aside .wiki-aside {
// Slide button
label .wiki-aside__slider for="wiki-aside-shown" {
img .wiki-icon src="/static/svg/double-arrow.svg" width="24" height="24";
}
(crate::html::misc::show_outline(outline))
}
2024-04-13 15:26:52 +02:00
2024-07-05 13:59:07 +02:00
article .wiki-article /*class:list={classlist)*/ {
header class="markdown" {
h1 #top { (heading) }
}
section .wiki-article__markdown.markdown {
(content)
}
2024-04-28 00:26:19 +02:00
2024-07-05 13:59:07 +02:00
@if let Some(bib) = bib {
(crate::html::misc::show_bibliography(bib))
}
}
}
);
2024-04-13 15:26:52 +02:00
2024-07-05 13:59:07 +02:00
crate::html::page(sack, main, fm.title.clone())
2024-04-13 15:26:52 +02:00
}