aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorXe Iaso <me@christine.website>2022-10-30 18:24:09 -0400
committerXe Iaso <me@christine.website>2022-10-30 18:24:09 -0400
commit23c64aebf57b3a9f9db4c08af001f01af25d732d (patch)
tree6a3e6aafe2eef06aaea2faa5b8c7711be328c86e /lib
parent5d2935617a0e4bf615a8ffc0f7c1f96d9479cbd8 (diff)
downloadxesite-23c64aebf57b3a9f9db4c08af001f01af25d732d.tar.xz
xesite-23c64aebf57b3a9f9db4c08af001f01af25d732d.zip
various site updates
Signed-off-by: Xe Iaso <me@christine.website>
Diffstat (limited to 'lib')
-rw-r--r--lib/xesite_markdown/Cargo.toml6
-rw-r--r--lib/xesite_markdown/src/lib.rs38
-rw-r--r--lib/xesite_templates/src/lib.rs55
-rw-r--r--lib/xesite_types/src/mastodon.rs3
4 files changed, 84 insertions, 18 deletions
diff --git a/lib/xesite_markdown/Cargo.toml b/lib/xesite_markdown/Cargo.toml
index 62eb817..08e196f 100644
--- a/lib/xesite_markdown/Cargo.toml
+++ b/lib/xesite_markdown/Cargo.toml
@@ -8,12 +8,16 @@ edition = "2021"
[dependencies]
color-eyre = "0.6"
comrak = "0.14.0"
+hex = "0.4"
lazy_static = "1.4"
lol_html = "0.3"
maud = "0.23.0"
+sha2 = "0.10"
+serde_json = "1"
thiserror = "1"
tracing = "0.1"
url = "2"
# local deps
-xesite_templates = { path = "../xesite_templates" } \ No newline at end of file
+xesite_templates = { path = "../xesite_templates" }
+xesite_types = { path = "../xesite_types" } \ No newline at end of file
diff --git a/lib/xesite_markdown/src/lib.rs b/lib/xesite_markdown/src/lib.rs
index fad2b3a..5bd6dfa 100644
--- a/lib/xesite_markdown/src/lib.rs
+++ b/lib/xesite_markdown/src/lib.rs
@@ -8,8 +8,16 @@ use comrak::{
use lazy_static::lazy_static;
use lol_html::{element, html_content::ContentType, rewrite_str, RewriteStrSettings};
use maud::PreEscaped;
+use sha2::{Digest, Sha256};
use std::{cell::RefCell, io::Write};
use url::Url;
+use xesite_types::mastodon::{Toot, User};
+
+pub fn hash_string(inp: String) -> String {
+ let mut h = Sha256::new();
+ h.update(&inp.as_bytes());
+ hex::encode(h.finalize())
+}
lazy_static! {
static ref SYNTECT_ADAPTER: SyntectAdapter<'static> = SyntectAdapter::new("base16-mocha.dark");
@@ -175,6 +183,36 @@ pub fn render(inp: &str) -> Result<String> {
el.replace(&xesite_templates::video(path).0, ContentType::Html);
Ok(())
}),
+ #[cfg(not(target_arch = "wasm32"))]
+ element!("xeblog-toot", |el| {
+ use serde_json::from_reader;
+ use std::fs;
+
+ let mut toot_url = el
+ .get_attribute("url")
+ .ok_or(Error::MissingElementAttribute("url".to_string()))?;
+
+ if !toot_url.ends_with(".json") {
+ toot_url = format!("{toot_url}.json");
+ }
+
+ let toot_fname = format!("./data/toots/{}.json", hash_string(toot_url));
+ tracing::debug!("opening {toot_fname}");
+ let mut fin = fs::File::open(&toot_fname)?;
+ let t: Toot = from_reader(&mut fin)?;
+
+ let user_fname = format!(
+ "./data/users/{}.json",
+ hash_string(format!("{}.json", t.attributed_to.clone()))
+ );
+ tracing::debug!("opening {user_fname}");
+ let mut fin = fs::File::open(&user_fname)?;
+
+ let u: User = from_reader(&mut fin)?;
+
+ el.replace(&xesite_templates::toot_embed(u, t).0, ContentType::Html);
+ Ok(())
+ }),
],
..RewriteStrSettings::default()
},
diff --git a/lib/xesite_templates/src/lib.rs b/lib/xesite_templates/src/lib.rs
index b3298d9..333c847 100644
--- a/lib/xesite_templates/src/lib.rs
+++ b/lib/xesite_templates/src/lib.rs
@@ -168,33 +168,54 @@ pub fn advertiser_nag() -> Markup {
}
pub fn toot_embed(u: User, t: Toot) -> Markup {
+ let content = html! {
+ (PreEscaped::<String>(t.content))
+
+ @for att in &t.attachment {
+ @if att.media_type.starts_with("image/") {
+ a href=(att.url) {
+ img height=(if att.height > att.width {"480"} else {"100%"}) src=(att.url) alt=(att.name.clone().unwrap_or("no description provided".into()));
+ }
+ }
+
+ @if att.media_type.starts_with("video/") {
+ video width=({att.width / 2}) height=({att.height / 2}) controls {
+ source src=(att.url) type=(att.media_type);
+ "Your browser does not support the video tag, see this URL: "
+ a href=(att.url) {(att.url)}
+ }
+ }
+ }
+ @if t.attachment.len() != 0 {
+ br;
+ }
+
+ a href=(t.url) { "Link" }
+ };
html! {
.media {
.media-left {
.avatarholder {
- img src=(u.icon.url);
+ img src=(u.icon.url) alt={"the profile picture for " (u.preferred_username)};
}
}
.media-body {
- .media-heading { (u.name) " @" (u.preferred_username) }
+ .media-heading {
+ (u.name)
+ " "
+ a href=(u.url) {"@" (u.preferred_username)}
+ br;
+ (t.published.format("M%m %d %Y %H:%M (UTC)").to_string())
+ }
.media-content {
- (PreEscaped::<String>(t.content))
-
- @for att in &t.attachment {
- @if att.media_type.starts_with("image/") {
- img src=(att.url) alt=(att.name.clone().unwrap_or("no description provided".into()));
- }
-
- @if att.media_type.starts_with("video/") {
- video width=(att.width) height=(att.height) controls {
- source src=(att.url) type=(att.media_type);
- "Your browser does not support the video tag, see this URL: "
- a href=(att.url) {(att.url)}
- }
+ @if let Some(warning) = t.summary {
+ details {
+ summary { "Content warning: " (warning) }
+ (content)
}
+ } @else {
+ (content)
}
-
- a href=(t.url) { "Link" }
}
}
}
diff --git a/lib/xesite_types/src/mastodon.rs b/lib/xesite_types/src/mastodon.rs
index 4cf1e96..56567e6 100644
--- a/lib/xesite_types/src/mastodon.rs
+++ b/lib/xesite_types/src/mastodon.rs
@@ -108,6 +108,9 @@ pub struct Toot {
#[serde(rename = "conversation")]
pub conversation: String,
+ #[serde(rename = "summary")]
+ pub summary: Option<String>,
+
#[serde(rename = "content")]
pub content: String,