xesite/src/app.rs

107 lines
2.9 KiB
Rust
Raw Normal View History

2020-07-13 02:04:38 +00:00
use crate::{post::Post, signalboost::Person};
2020-07-12 18:42:09 +00:00
use anyhow::Result;
2020-07-13 00:11:26 +00:00
use comrak::{markdown_to_html, ComrakOptions};
2020-07-12 18:42:09 +00:00
use serde::Deserialize;
2020-07-12 23:26:53 +00:00
use std::{fs, path::PathBuf};
2020-07-12 18:42:09 +00:00
#[derive(Clone, Deserialize)]
pub struct Config {
2020-07-13 00:11:26 +00:00
#[serde(rename = "clackSet")]
2020-07-12 18:42:09 +00:00
clack_set: Vec<String>,
2020-07-13 00:11:26 +00:00
signalboost: Vec<Person>,
2020-07-12 18:42:09 +00:00
port: u16,
2020-07-13 00:11:26 +00:00
#[serde(rename = "resumeFname")]
2020-07-12 23:26:53 +00:00
resume_fname: PathBuf,
2020-07-12 18:42:09 +00:00
}
2020-07-12 23:30:01 +00:00
pub fn markdown(inp: &str) -> String {
let mut options = ComrakOptions::default();
options.extension.autolink = true;
options.extension.table = true;
options.extension.description_lists = true;
options.extension.superscript = true;
options.extension.strikethrough = true;
options.extension.footnotes = true;
options.render.unsafe_ = true;
markdown_to_html(inp, &options)
}
2020-07-14 17:27:52 +00:00
pub const ICON: &'static str = "https://christine.website/static/img/avatar.png";
2020-07-12 18:42:09 +00:00
pub struct State {
pub cfg: Config,
pub signalboost: Vec<Person>,
2020-07-12 23:26:53 +00:00
pub resume: String,
2020-07-13 02:04:38 +00:00
pub blog: Vec<Post>,
pub gallery: Vec<Post>,
pub talks: Vec<Post>,
pub everything: Vec<Post>,
2020-07-14 17:27:52 +00:00
pub jf: jsonfeed::Feed,
2020-07-12 18:42:09 +00:00
}
2020-07-13 00:11:26 +00:00
pub fn init(cfg: PathBuf) -> Result<State> {
let cfg: Config = serde_dhall::from_file(cfg).parse()?;
let sb = cfg.signalboost.clone();
2020-07-12 23:26:53 +00:00
let resume = fs::read_to_string(cfg.resume_fname.clone())?;
2020-07-12 23:30:01 +00:00
let resume: String = markdown(&resume);
2020-07-13 02:04:38 +00:00
let blog = crate::post::load("blog")?;
let gallery = crate::post::load("gallery")?;
let talks = crate::post::load("talks")?;
let mut everything: Vec<Post> = vec![];
{
let blog = blog.clone();
let gallery = gallery.clone();
let talks = talks.clone();
everything.extend(blog.iter().cloned());
everything.extend(gallery.iter().cloned());
everything.extend(talks.iter().cloned());
};
everything.sort();
everything.reverse();
2020-07-12 18:42:09 +00:00
2020-07-14 17:27:52 +00:00
let mut jfb = jsonfeed::Feed::builder()
.title("Christine Dodrill's Blog")
.description("My blog posts and rants about various technology things.")
.author(
jsonfeed::Author::new()
.name("Christine Dodrill")
.url("https://christine.website")
.avatar(ICON),
)
.feed_url("https://christine.website/blog.json")
.user_comment("This is a JSON feed of my blogposts. For more information read: https://jsonfeed.org/version/1")
.icon(ICON)
.favicon(ICON);
for post in &everything {
let post = post.clone();
jfb = jfb.item(post.into());
}
2020-07-12 18:42:09 +00:00
Ok(State {
cfg: cfg,
signalboost: sb,
2020-07-12 23:26:53 +00:00
resume: resume,
2020-07-13 02:04:38 +00:00
blog: blog,
gallery: gallery,
talks: talks,
everything: everything,
2020-07-14 17:27:52 +00:00
jf: jfb.build(),
2020-07-12 18:42:09 +00:00
})
}
2020-07-13 02:04:38 +00:00
#[cfg(test)]
mod tests {
use anyhow::Result;
#[test]
fn init() -> Result<()> {
super::init("./config.dhall".into())?;
Ok(())
}
}