From aec662ca10c5efd6d357fe5ab9f9d27af9fb6925 Mon Sep 17 00:00:00 2001 From: Christine Dodrill Date: Fri, 18 Sep 2020 16:28:31 -0400 Subject: [PATCH] start mara code --- Cargo.lock | 98 +++++++++++++++++++++++++++++++++++--- Cargo.toml | 3 +- blog/mara-test.markdown | 15 ++++++ shell.nix | 2 +- src/app/markdown.rs | 75 +++++++++++++++++++++++++++++ src/{app.rs => app/mod.rs} | 22 ++------- src/main.rs | 3 +- src/post/frontmatter.rs | 2 +- src/post/mod.rs | 15 +++--- templates/mara.rs.html | 14 ++++++ 10 files changed, 214 insertions(+), 35 deletions(-) create mode 100644 blog/mara-test.markdown create mode 100644 src/app/markdown.rs rename src/{app.rs => app/mod.rs} (88%) create mode 100644 templates/mara.rs.html diff --git a/Cargo.lock b/Cargo.lock index 0900e11..5979efa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -66,12 +66,6 @@ dependencies = [ "winapi 0.3.9", ] -[[package]] -name = "anyhow" -version = "1.0.32" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6b602bfe940d21c130f3895acd65221e8a61270debe89d628b9cb4e3ccb8569b" - [[package]] name = "arrayvec" version = "0.5.1" @@ -263,6 +257,32 @@ dependencies = [ "bitflags", ] +[[package]] +name = "color-eyre" +version = "0.5.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "22ba9b5e817f1bc1f2219b5a3474b69f838321b2a2ba8860d6a71c3bfe3d0fc1" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a99aa4aa18448eef4c7d3f86d2720d2d8cad5c860fe9ff9b279293efdc8f5be" +dependencies = [ + "ansi_term", + "tracing-core", + "tracing-error", +] + [[package]] name = "comrak" version = "0.8.2" @@ -433,6 +453,16 @@ dependencies = [ "version_check", ] +[[package]] +name = "eyre" +version = "0.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b0f9683839e579a53258d377fcc0073ca0bf2042ac5e6c60a598069e64403a6d" +dependencies = [ + "indenter", + "once_cell", +] + [[package]] name = "fake-simd" version = "0.1.2" @@ -758,6 +788,12 @@ dependencies = [ "unicode-normalization", ] +[[package]] +name = "indenter" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0bd112d44d9d870a6819eb505d04dd92b5e4d94bb8c304924a0872ae7016fb5" + [[package]] name = "indexmap" version = "1.5.0" @@ -1139,6 +1175,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "owo-colors" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7a1250cdd103eef6bd542b5ae82989f931fc00a41a27f60377338241594410f3" + [[package]] name = "parking_lot" version = "0.11.0" @@ -1810,6 +1852,15 @@ dependencies = [ "opaque-debug 0.3.0", ] +[[package]] +name = "sharded-slab" +version = "0.0.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06d5a3f5166fb5b42a5439f2eee8b9de149e235961e3eb21c5808fc3ea17ff3e" +dependencies = [ + "lazy_static", +] + [[package]] name = "shell-words" version = "1.0.0" @@ -2033,9 +2084,21 @@ checksum = "dbdf4ccd1652592b01286a5dbe1e2a77d78afaa34beadd9872a5f7396f92aaa9" dependencies = [ "cfg-if", "log", + "tracing-attributes", "tracing-core", ] +[[package]] +name = "tracing-attributes" +version = "0.1.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "80e0ccfc3378da0cce270c946b676a376943f5cd16aeba64568e7939806f4ada" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "tracing-core" version = "0.1.11" @@ -2045,6 +2108,16 @@ dependencies = [ "lazy_static", ] +[[package]] +name = "tracing-error" +version = "0.1.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b4d7c0b83d4a500748fa5879461652b361edf5c9d51ede2a2ac03875ca185e24" +dependencies = [ + "tracing", + "tracing-subscriber", +] + [[package]] name = "tracing-futures" version = "0.2.4" @@ -2055,6 +2128,16 @@ dependencies = [ "tracing", ] +[[package]] +name = "tracing-subscriber" +version = "0.2.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4f5dd7095c2481b7b3cbed71c8de53085fb3542bc3c2b4c73cba43e8f11c7ba" +dependencies = [ + "sharded-slab", + "tracing-core", +] + [[package]] name = "try-lock" version = "0.2.3" @@ -2414,8 +2497,8 @@ checksum = "d089681aa106a86fade1b0128fb5daf07d5867a509ab036d99988dec80429a57" name = "xesite" version = "2.0.1" dependencies = [ - "anyhow", "chrono", + "color-eyre", "comrak", "envy", "glob", @@ -2437,6 +2520,7 @@ dependencies = [ "sitemap", "thiserror", "tokio", + "url", "warp", "xml-rs", ] diff --git a/Cargo.toml b/Cargo.toml index a9c178b..f43230d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,7 +9,7 @@ repository = "https://github.com/Xe/site" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -anyhow = "1" +color-eyre = "0.5" chrono = "0.4" comrak = "0.8" envy = "0.4" @@ -30,6 +30,7 @@ thiserror = "1" tokio = { version = "0.2", features = ["macros"] } warp = "0.2" xml-rs = "0.8" +url = "2" # workspace dependencies go_vanity = { path = "./lib/go_vanity" } diff --git a/blog/mara-test.markdown b/blog/mara-test.markdown new file mode 100644 index 0000000..bd0298e --- /dev/null +++ b/blog/mara-test.markdown @@ -0,0 +1,15 @@ +--- +title: Mara Test +date: 2020-09-18 +--- + +# Mara Test + +[But if you do this thing, what about that other +thing?](conversation://Mara/hmm) + +[If you like using this, be sure to check out this other thing: +https://h.christine.website](conversation://Mara/hacker) + +[But, how does that make sense if you refrobnicated the bifrucated flopnax back +there?](conversation://Mara/wat) diff --git a/shell.nix b/shell.nix index b9c873a..2139d98 100644 --- a/shell.nix +++ b/shell.nix @@ -33,6 +33,6 @@ mkShell { SITE_PREFIX = "devel."; CLACK_SET = "Ashlynn,Terry Davis,Dennis Ritchie"; - RUST_LOG = "info"; + RUST_LOG = "debug"; GITHUB_SHA = "devel"; } diff --git a/src/app/markdown.rs b/src/app/markdown.rs new file mode 100644 index 0000000..996e764 --- /dev/null +++ b/src/app/markdown.rs @@ -0,0 +1,75 @@ +use color_eyre::eyre::{Result, WrapErr}; +use comrak::nodes::{Ast, AstNode, NodeValue}; +use comrak::{format_html, parse_document, Arena, ComrakOptions}; +use std::cell::RefCell; +use url::Url; + +pub fn render(inp: &str) -> Result { + 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); + + 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://christine.website/")?; + 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![]; + format_html(node.first_child().unwrap(), &options, &mut message)?; + let message = std::str::from_utf8(&message)?; + 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, message)?; + + 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(root, &options, &mut html).unwrap(); + + String::from_utf8(html).wrap_err("post is somehow invalid UTF-8") +} + +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("") +} diff --git a/src/app.rs b/src/app/mod.rs similarity index 88% rename from src/app.rs rename to src/app/mod.rs index 5ffca7c..cab7528 100644 --- a/src/app.rs +++ b/src/app/mod.rs @@ -1,9 +1,10 @@ use crate::{post::Post, signalboost::Person}; -use anyhow::Result; -use comrak::{markdown_to_html, ComrakOptions}; +use color_eyre::eyre::Result; use serde::Deserialize; use std::{fs, path::PathBuf}; +pub mod markdown; + #[derive(Clone, Deserialize)] pub struct Config { #[serde(rename = "clackSet")] @@ -14,21 +15,6 @@ pub struct Config { resume_fname: PathBuf, } -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) -} - async fn patrons() -> Result> { use patreon::*; let creds: Credentials = envy::prefixed("PATREON_").from_env().unwrap(); @@ -72,7 +58,7 @@ pub async fn init(cfg: PathBuf) -> Result { let cfg: Config = serde_dhall::from_file(cfg).parse()?; let sb = cfg.signalboost.clone(); let resume = fs::read_to_string(cfg.resume_fname.clone())?; - let resume: String = markdown(&resume); + let resume: String = markdown::render(&resume)?; let blog = crate::post::load("blog")?; let gallery = crate::post::load("gallery")?; let talks = crate::post::load("talks")?; diff --git a/src/main.rs b/src/main.rs index aa5400e..c1e9e1d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use anyhow::Result; +use color_eyre::eyre::Result; use hyper::{header::CONTENT_TYPE, Body, Response}; use prometheus::{Encoder, TextEncoder}; use std::sync::Arc; @@ -21,6 +21,7 @@ fn with_state( #[tokio::main] async fn main() -> Result<()> { + color_eyre::install()?; let _ = kankyo::init(); pretty_env_logger::init(); log::info!("starting up commit {}", env!("GITHUB_SHA")); diff --git a/src/post/frontmatter.rs b/src/post/frontmatter.rs index 1cc8032..615f2c5 100644 --- a/src/post/frontmatter.rs +++ b/src/post/frontmatter.rs @@ -1,6 +1,6 @@ /// This code was borrowed from @fasterthanlime. -use anyhow::{Result}; +use color_eyre::eyre::{Result}; use serde::{Serialize, Deserialize}; #[derive(Eq, PartialEq, Deserialize, Default, Debug, Serialize, Clone)] diff --git a/src/post/mod.rs b/src/post/mod.rs index a948017..c0062a4 100644 --- a/src/post/mod.rs +++ b/src/post/mod.rs @@ -1,5 +1,5 @@ -use anyhow::{anyhow, Result}; use chrono::prelude::*; +use color_eyre::eyre::{eyre, Result, WrapErr}; use glob::glob; use std::{cmp::Ordering, fs}; @@ -75,8 +75,10 @@ pub fn load(dir: &str) -> Result> { for path in glob(&format!("{}/*.markdown", dir))?.filter_map(Result::ok) { log::debug!("loading {:?}", path); - let body = fs::read_to_string(path.clone()).expect("things to work"); - let (fm, content_offset) = frontmatter::Data::parse(body.clone().as_str()).expect("stuff to work"); + let body = + fs::read_to_string(path.clone()).wrap_err_with(|| format!("can't read {:?}", path))?; + let (fm, content_offset) = frontmatter::Data::parse(body.clone().as_str()) + .wrap_err_with(|| format!("can't parse frontmatter of {:?}", path))?; let markup = &body[content_offset..]; let date = NaiveDate::parse_from_str(&fm.clone().date, "%Y-%m-%d")?; @@ -84,7 +86,8 @@ pub fn load(dir: &str) -> Result> { front_matter: fm, link: format!("{}/{}", dir, path.file_stem().unwrap().to_str().unwrap()), body: markup.to_string(), - body_html: crate::app::markdown(&markup), + body_html: crate::app::markdown::render(&markup) + .wrap_err_with(|| format!("can't parse markdown for {:?}", path))?, date: { DateTime::::from_utc( NaiveDateTime::new(date, NaiveTime::from_hms(0, 0, 0)), @@ -97,7 +100,7 @@ pub fn load(dir: &str) -> Result> { } if result.len() == 0 { - Err(anyhow!("no posts loaded")) + Err(eyre!("no posts loaded")) } else { result.sort(); result.reverse(); @@ -108,7 +111,7 @@ pub fn load(dir: &str) -> Result> { #[cfg(test)] mod tests { use super::*; - use anyhow::Result; + use color_eyre::eyre::Result; #[test] fn blog() { diff --git a/templates/mara.rs.html b/templates/mara.rs.html new file mode 100644 index 0000000..257cc10 --- /dev/null +++ b/templates/mara.rs.html @@ -0,0 +1,14 @@ +@(mood: &str, character: &str, message: &str) +
+
+
+ + + @message + +
+
+
+

@character

@message
+
+