diff options
| author | Xe Iaso <me@christine.website> | 2022-09-20 21:05:44 -0400 |
|---|---|---|
| committer | Xe Iaso <me@christine.website> | 2022-09-20 21:05:44 -0400 |
| commit | 67005bc59dd918eadcbd9c7c9285aed0c3422292 (patch) | |
| tree | de54bbad6aa651d4e56517af56a6773f02766fcf /src | |
| parent | 839c44e535d03f8ae747139acd27109e897c76e4 (diff) | |
| download | xesite-67005bc59dd918eadcbd9c7c9285aed0c3422292.tar.xz xesite-67005bc59dd918eadcbd9c7c9285aed0c3422292.zip | |
move markdown and templates into a dedicated crate
This does not move the ructe templates around, only the newer Maud ones.
The only template I can't move easily is the salary history one, but I
should get rid of that anyways.
Diffstat (limited to 'src')
| -rw-r--r-- | src/app/markdown.rs | 169 | ||||
| -rw-r--r-- | src/app/mod.rs | 3 | ||||
| -rw-r--r-- | src/main.rs | 3 | ||||
| -rw-r--r-- | src/post/mod.rs | 2 | ||||
| -rw-r--r-- | src/tmpl/mod.rs | 92 | ||||
| -rw-r--r-- | src/tmpl/nag.rs | 2 |
6 files changed, 6 insertions, 265 deletions
diff --git a/src/app/markdown.rs b/src/app/markdown.rs deleted file mode 100644 index 9c3266e..0000000 --- a/src/app/markdown.rs +++ /dev/null @@ -1,169 +0,0 @@ -use crate::app::Config; -use crate::templates::Html; -use color_eyre::eyre::{Result, WrapErr}; -use comrak::nodes::{Ast, AstNode, NodeValue}; -use comrak::plugins::syntect::SyntectAdapter; -use comrak::{ - format_html_with_plugins, markdown_to_html_with_plugins, parse_document, Arena, ComrakOptions, - ComrakPlugins, -}; -use lazy_static::lazy_static; -use lol_html::{element, html_content::ContentType, rewrite_str, RewriteStrSettings}; -use std::cell::RefCell; -use std::sync::Arc; -use url::Url; - -lazy_static! { - static ref SYNTECT_ADAPTER: SyntectAdapter<'static> = SyntectAdapter::new("base16-mocha.dark"); -} - -pub fn render(cfg: Arc<Config>, inp: &str) -> Result<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; - - let arena = Arena::new(); - let root = parse_document(&arena, inp, &options); - - let mut plugins = ComrakPlugins::default(); - plugins.render.codefence_syntax_highlighter = Some(&*SYNTECT_ADAPTER); - - iter_nodes(root, &|node| { - let mut data = node.data.borrow_mut(); - match &mut data.value { - &mut NodeValue::Link(ref mut link) => { - let base = Url::parse("https://xeiaso.net/")?; - let u = base.join(std::str::from_utf8(&link.url.clone())?)?; - if u.scheme() != "conversation" { - return Ok(()); - } - let parent = node.parent().unwrap(); - node.detach(); - let mut message = vec![]; - for child in node.children() { - format_html_with_plugins(child, &options, &mut message, &plugins)?; - } - let message = std::str::from_utf8(&message)?; - let mut message = markdown_to_html_with_plugins(message, &options, &plugins); - crop_letters(&mut message, 3); - message.drain((message.len() - 5)..); - let mood = without_first(u.path()); - let name = u.host_str().unwrap_or("Mara"); - - let mut html = vec![]; - crate::templates::mara(&mut html, mood, name, Html(message.trim().into()))?; - - let new_node = arena.alloc(AstNode::new(RefCell::new(Ast::new( - NodeValue::HtmlInline(html), - )))); - parent.append(new_node); - - Ok(()) - } - _ => Ok(()), - } - })?; - - let mut html = vec![]; - format_html_with_plugins(root, &options, &mut html, &plugins).unwrap(); - - let html = String::from_utf8(html).wrap_err("post is somehow invalid UTF-8")?; - - let html = rewrite_str(&html, RewriteStrSettings{ - element_content_handlers: vec![ - element!("xeblog-conv", |el| { - let name = el.get_attribute("name").expect("wanted xeblog-conv to contain name"); - let name_lower = name.clone().to_lowercase(); - let mood = el.get_attribute("mood").expect("wanted xeblog-conv to contain mood"); - - el.before(&format!(r#" -<div class="conversation"> - <div class="conversation-picture conversation-smol"> - <picture> - <source srcset="https://cdn.xeiaso.net/file/christine-static/stickers/{name_lower}/{mood}.avif" type="image/avif"> - <source srcset="https://cdn.xeiaso.net/file/christine-static/stickers/{name_lower}/{mood}.webp" type="image/webp"> - <img src="https://cdn.xeiaso.net/file/christine-static/stickers/{name_lower}/{mood}.png" alt="{name} is {mood}"> - </picture> - </div> - <div class="conversation-chat"><<b>{name}</b>> "#), ContentType::Html); - el.after("</div></div>", ContentType::Html); - - el.remove_and_keep_content(); - Ok(()) - }), - element!("xeblog-picture", |el| { - let path = el.get_attribute("path").expect("wanted xeblog-picture to contain path"); - el.replace(&crate::tmpl::xeblog_picture(path).0, ContentType::Html); - Ok(()) - }), - element!("xeblog-hero", |el| { - let file = el.get_attribute("file").expect("wanted xeblog-hero to contain file"); - el.replace(&crate::tmpl::xeblog_hero(file, el.get_attribute("prompt"), el.get_attribute("ai")).0, ContentType::Html); - Ok(()) - }), - element!("xeblog-salary-history", |el| { - el.replace(&crate::tmpl::xeblog_salary_history(cfg.clone()).0, ContentType::Html); - - Ok(()) - }), - element!("xeblog-sticker", |el| { - let name = el.get_attribute("name").expect("wanted xeblog-sticker to contain name"); - let mood = el.get_attribute("mood").expect("wanted xeblog-sticker to contain mood"); - el.replace(&crate::tmpl::xeblog_sticker(name, mood).0, ContentType::Html); - - Ok(()) - }), - element!("xeblog-slide", |el| { - let name = el.get_attribute("name").expect("wanted xeblog-slide to contain name"); - let essential = el.get_attribute("essential").is_some(); - el.replace(&crate::tmpl::xeblog_slide(name, essential).0, ContentType::Html); - - Ok(()) - }), - element!("xeblog-talk-warning", |el| { - el.replace(&crate::tmpl::xeblog_talk_warning().0, ContentType::Html); - Ok(()) - }), - ], - ..RewriteStrSettings::default() - }).unwrap(); - - Ok(html) -} - -fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F) -> Result<()> -where - F: Fn(&'a AstNode<'a>) -> Result<()>, -{ - f(node)?; - for c in node.children() { - iter_nodes(c, f)?; - } - Ok(()) -} - -fn without_first(string: &str) -> &str { - string - .char_indices() - .nth(1) - .and_then(|(i, _)| string.get(i..)) - .unwrap_or("") -} - -fn crop_letters(s: &mut String, pos: usize) { - match s.char_indices().nth(pos) { - Some((pos, _)) => { - s.drain(..pos); - } - None => { - s.clear(); - } - } -} diff --git a/src/app/mod.rs b/src/app/mod.rs index 5b8e719..9a331a4 100644 --- a/src/app/mod.rs +++ b/src/app/mod.rs @@ -5,7 +5,6 @@ use std::{fs, path::PathBuf, sync::Arc}; use tracing::{error, instrument}; pub mod config; -pub mod markdown; pub mod poke; pub use config::*; @@ -64,7 +63,7 @@ pub async fn init(cfg: PathBuf) -> Result<State> { let cfg: Arc<Config> = Arc::new(serde_dhall::from_file(cfg).parse()?); let sb = cfg.signalboost.clone(); let resume = fs::read_to_string(cfg.clone().resume_fname.clone())?; - let resume: String = markdown::render(cfg.clone(), &resume)?; + let resume: String = xesite_markdown::render(&resume)?; let mi = mi::Client::new( cfg.clone().mi_token.clone(), crate::APPLICATION_NAME.to_string(), diff --git a/src/main.rs b/src/main.rs index cdcae68..1df4a02 100644 --- a/src/main.rs +++ b/src/main.rs @@ -12,7 +12,6 @@ use axum::{ use color_eyre::eyre::Result; use hyper::StatusCode; use prometheus::{Encoder, TextEncoder}; -use sdnotify::SdNotify; use std::{ env, io, net::{IpAddr, SocketAddr}, @@ -214,6 +213,8 @@ async fn main() -> Result<()> { #[cfg(target_os = "linux")] { + use sdnotify::SdNotify; + match SdNotify::from_env() { Ok(ref mut n) => { // shitty heuristic for detecting if we're running in prod diff --git a/src/post/mod.rs b/src/post/mod.rs index 5c2ed2d..263471d 100644 --- a/src/post/mod.rs +++ b/src/post/mod.rs @@ -103,7 +103,7 @@ async fn read_post( let date = NaiveDate::parse_from_str(&front_matter.clone().date, "%Y-%m-%d") .map_err(|why| eyre!("error parsing date in {:?}: {}", fname, why))?; let link = format!("{}/{}", dir, fname.file_stem().unwrap().to_str().unwrap()); - let body_html = crate::app::markdown::render(cfg.clone(), &body) + let body_html = xesite_markdown::render(&body) .wrap_err_with(|| format!("can't parse markdown for {:?}", fname))?; let date: DateTime<FixedOffset> = DateTime::<Utc>::from_utc(NaiveDateTime::new(date, NaiveTime::from_hms(0, 0, 0)), Utc) diff --git a/src/tmpl/mod.rs b/src/tmpl/mod.rs index ec7ccd5..b2ffd0d 100644 --- a/src/tmpl/mod.rs +++ b/src/tmpl/mod.rs @@ -4,7 +4,7 @@ use std::sync::Arc; pub mod nag; -pub fn xeblog_salary_history(cfg: Arc<Config>) -> Markup { +pub fn salary_history(cfg: Arc<Config>) -> Markup { html! { table.salary_history { tr { @@ -21,93 +21,3 @@ pub fn xeblog_salary_history(cfg: Arc<Config>) -> Markup { } } } - -pub fn xeblog_talk_warning() -> Markup { - html! { - div.warning { - (xeblog_conv("Cadey".to_string(), "coffee".to_string(), html!{ - "So you are aware: you are reading the written version of a conference talk. This is written in a different style that is more lighthearted, conversational and different than the content normally on this blog. The words being said are the verbatim words that were spoken at the conference. The slides are the literal slides for each spoken utterance. If you want to hide the non-essential slides, please install this userstyle: " - a href="/css/no-fun-allowed.user.css" {code {"No fun allowed"}} - ". If this isn't enough, please edit it to also hide this CSS class: " - code { "xeblog-slides-essential" } - ". Doing this may make the presentation page harder to understand." - })) - } - } -} - -pub fn xeblog_slide(name: String, essential: bool) -> Markup { - html! { - div.hero.{@if essential {("xeblog-slides-essential")} @else {("xeblog-slides-fluff")}} { - picture style="margin:0" { - source type="image/avif" srcset={"https://cdn.xeiaso.net/file/christine-static/talks/" (name) ".avif"}; - source type="image/webp" srcset={"https://cdn.xeiaso.net/file/christine-static/talks/" (name) ".webp"}; - img style="padding:0" loading="lazy" src={"https://cdn.xeiaso.net/file/christine-static/talks/" (name) "-smol.png"}; - } - } - } -} - -pub fn xeblog_picture(path: String) -> Markup { - html! { - picture style="margin:0" { - source type="image/avif" srcset={"https://cdn.xeiaso.net/file/christine-static/" (path) ".avif"}; - source type="image/webp" srcset={"https://cdn.xeiaso.net/file/christine-static/" (path) ".webp"}; - img style="padding:0" loading="lazy" alt={"hero image " (path)} src={"https://cdn.xeiaso.net/file/christine-static/" (path) "-smol.png"}; - } - } -} - -pub fn xeblog_hero(file: String, prompt: Option<String>, ai: Option<String>) -> Markup { - let ai = ai.unwrap_or("MidJourney".to_string()); - html! { - meta property="og:image" content={"https://cdn.xeiaso.net/file/christine-static/hero/" (file) "-smol.png"} - figure.hero style="margin:0" { - picture style="margin:0" { - source type="image/avif" srcset={"https://cdn.xeiaso.net/file/christine-static/hero/" (file) ".avif"}; - source type="image/webp" srcset={"https://cdn.xeiaso.net/file/christine-static/hero/" (file) ".webp"}; - img style="padding:0" loading="lazy" alt={"hero image " (file)} src={"https://cdn.xeiaso.net/file/christine-static/hero/" (file) "-smol.png"}; - } - figcaption { - "Image generated by " - (ai) - @if let Some(prompt) = prompt { " -- " (prompt) } - } - } - } -} - -pub fn xeblog_conv(name: String, mood: String, body: Markup) -> Markup { - let name_lower = name.clone().to_lowercase(); - - html! { - .conversation { - ."conversation-picture"."conversation-smol" { - picture { - source type="image/avif" srcset={"https://cdn.xeiaso.net/file/christine-static/stickers/" (name_lower) "/" (mood) ".avif"}; - source type="image/webp" srcset={"https://cdn.xeiaso.net/file/christine-static/stickers/" (name_lower) "/" (mood) ".webp"}; - img alt={(name) " is " (mood)} loading="lazy" src={"https://cdn.xeiaso.net/file/christine-static/stickers/" (name_lower) "/" (mood) ".png"}; - } - } - ."conversation-chat" { - "<" - b { (name) } - "> " - (body) - } - } - } -} - -pub fn xeblog_sticker(name: String, mood: String) -> Markup { - let name_lower = name.to_lowercase(); - html! { - center { - picture { - source type="image/avif" srcset={"https://cdn.xeiaso.net/file/christine-static/stickers/" (name_lower) "/" (mood) ".avif"}; - source type="image/webp" srcset={"https://cdn.xeiaso.net/file/christine-static/stickers/" (name_lower) "/" (mood) ".webp"}; - img alt={(name) " is " (mood)} src={"https://cdn.xeiaso.net/file/christine-static/stickers/" (name_lower) "/" (mood) ".png"}; - } - } - } -} diff --git a/src/tmpl/nag.rs b/src/tmpl/nag.rs index a41447b..4ba165e 100644 --- a/src/tmpl/nag.rs +++ b/src/tmpl/nag.rs @@ -1,9 +1,9 @@ -use super::xeblog_conv; use crate::post::Post; use chrono::prelude::*; use lazy_static::lazy_static; use maud::{html, Markup}; use regex::Regex; +use xesite_templates::conv as xeblog_conv; lazy_static! { static ref HN: Regex = Regex::new(r#"^https?://news.ycombinator.com"#).unwrap(); |
