diff options
| author | Xe Iaso <me@christine.website> | 2022-10-08 17:19:21 +0000 |
|---|---|---|
| committer | Xe Iaso <me@christine.website> | 2022-10-08 17:19:21 +0000 |
| commit | e2dd27beaa865e5bf9e348aeb98abb918b97925a (patch) | |
| tree | 3ee6d9f7e597f1bb31d30230b67fd041672b5325 /lib | |
| parent | c324bf0ef0ee9a14534943de2734e04f7c2eabf5 (diff) | |
| download | xesite-e2dd27beaa865e5bf9e348aeb98abb918b97925a.tar.xz xesite-e2dd27beaa865e5bf9e348aeb98abb918b97925a.zip | |
prepare for cryptocurrency/ownership article
Signed-off-by: Xe Iaso <me@christine.website>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/xesite_markdown/Cargo.toml | 1 | ||||
| -rw-r--r-- | lib/xesite_markdown/src/lib.rs | 123 | ||||
| -rw-r--r-- | lib/xesite_templates/src/lib.rs | 13 |
3 files changed, 88 insertions, 49 deletions
diff --git a/lib/xesite_markdown/Cargo.toml b/lib/xesite_markdown/Cargo.toml index b05ca75..62eb817 100644 --- a/lib/xesite_markdown/Cargo.toml +++ b/lib/xesite_markdown/Cargo.toml @@ -11,6 +11,7 @@ comrak = "0.14.0" lazy_static = "1.4" lol_html = "0.3" maud = "0.23.0" +thiserror = "1" tracing = "0.1" url = "2" diff --git a/lib/xesite_markdown/src/lib.rs b/lib/xesite_markdown/src/lib.rs index 882278b..b225c4a 100644 --- a/lib/xesite_markdown/src/lib.rs +++ b/lib/xesite_markdown/src/lib.rs @@ -15,6 +15,12 @@ lazy_static! { static ref SYNTECT_ADAPTER: SyntectAdapter<'static> = SyntectAdapter::new("base16-mocha.dark"); } +#[derive(thiserror::Error, Debug, Clone)] +pub enum Error { + #[error("missing element attribute {0}")] + MissingElementAttribute(String), +} + pub fn render(inp: &str) -> Result<String> { let mut options = ComrakOptions::default(); @@ -83,14 +89,21 @@ pub fn render(inp: &str) -> Result<String> { 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#" + let html = rewrite_str( + &html, + RewriteStrSettings { + element_content_handlers: vec![ + element!("xeblog-conv", |el| { + let name = el + .get_attribute("name") + .ok_or(Error::MissingElementAttribute("name".to_string()))?; + let name_lower = name.clone().to_lowercase(); + let mood = el + .get_attribute("mood") + .ok_or(Error::MissingElementAttribute("mood".to_string()))?; + let name = name.replace("_", " "); + + el.before(&format!(r#" <div class="conversation"> <div class="conversation-picture conversation-smol"> <picture> @@ -100,42 +113,64 @@ pub fn render(inp: &str) -> Result<String> { </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(&xesite_templates::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(&xesite_templates::hero(file, el.get_attribute("prompt"), el.get_attribute("ai")).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(&xesite_templates::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(&xesite_templates::slide(name, essential).0, ContentType::Html); - - Ok(()) - }), - element!("xeblog-talk-warning", |el| { - el.replace(&xesite_templates::talk_warning().0, ContentType::Html); - Ok(()) - }), - ], - ..RewriteStrSettings::default() - }).unwrap(); + 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(&xesite_templates::picture(path).0, ContentType::Html); + Ok(()) + }), + element!("xeblog-hero", |el| { + let file = el + .get_attribute("file") + .ok_or(Error::MissingElementAttribute("file".to_string()))?; + el.replace( + &xesite_templates::hero( + file, + el.get_attribute("prompt"), + el.get_attribute("ai"), + ) + .0, + ContentType::Html, + ); + Ok(()) + }), + element!("xeblog-sticker", |el| { + let name = el + .get_attribute("name") + .ok_or(Error::MissingElementAttribute("name".to_string()))?; + let mood = el + .get_attribute("mood") + .ok_or(Error::MissingElementAttribute("mood".to_string()))?; + el.replace(&xesite_templates::sticker(name, mood).0, ContentType::Html); + + Ok(()) + }), + element!("xeblog-slide", |el| { + let name = el + .get_attribute("name") + .ok_or(Error::MissingElementAttribute("name".to_string()))?; + let essential = el.get_attribute("essential").is_some(); + el.replace( + &xesite_templates::slide(name, essential).0, + ContentType::Html, + ); + + Ok(()) + }), + element!("xeblog-talk-warning", |el| { + el.replace(&xesite_templates::talk_warning().0, ContentType::Html); + Ok(()) + }), + ], + ..RewriteStrSettings::default() + }, + )?; Ok(html) } diff --git a/lib/xesite_templates/src/lib.rs b/lib/xesite_templates/src/lib.rs index 4b920b8..32ec07f 100644 --- a/lib/xesite_templates/src/lib.rs +++ b/lib/xesite_templates/src/lib.rs @@ -29,11 +29,13 @@ pub fn slide(name: String, essential: bool) -> Markup { pub fn picture(path: String) -> Markup { html! { a href={"https://cdn.xeiaso.net/file/christine-static/" (path) ".jpg"} target="_blank" { - 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"}; - } + center { + 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"}; + } + } } } } @@ -59,6 +61,7 @@ pub fn hero(file: String, prompt: Option<String>, ai: Option<String>) -> Markup pub fn conv(name: String, mood: String, body: Markup) -> Markup { let name_lower = name.clone().to_lowercase(); + let name = name.replace("_", " "); html! { .conversation { |
