aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorXe Iaso <me@christine.website>2023-01-04 14:55:54 -0500
committerXe Iaso <me@christine.website>2023-01-04 14:55:54 -0500
commit665bc0b334a671587dcbc39f5e2b77b24a6df88f (patch)
tree74f77831a70c5be49e01b477637af7e2ca1b878d
parent351069d9f91edab96425bcd221858529acb7e08a (diff)
downloadxesite-665bc0b334a671587dcbc39f5e2b77b24a6df88f.tar.xz
xesite-665bc0b334a671587dcbc39f5e2b77b24a6df88f.zip
attempt to change webmention titles
Signed-off-by: Xe Iaso <me@christine.website>
-rw-r--r--Cargo.lock8
-rw-r--r--Cargo.toml3
-rw-r--r--lib/mastodon2text/Cargo.toml9
-rw-r--r--lib/mastodon2text/src/lib.rs39
-rw-r--r--src/main.rs6
-rw-r--r--src/post/mod.rs10
6 files changed, 69 insertions, 6 deletions
diff --git a/Cargo.lock b/Cargo.lock
index e6c3671..94d69d1 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -1475,6 +1475,13 @@ dependencies = [
]
[[package]]
+name = "mastodon2text"
+version = "0.1.0"
+dependencies = [
+ "lol_html",
+]
+
+[[package]]
name = "matches"
version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -3503,6 +3510,7 @@ dependencies = [
"kankyo",
"lazy_static",
"log",
+ "mastodon2text",
"maud",
"mi",
"mime",
diff --git a/Cargo.toml b/Cargo.toml
index fa6c393..3ff333f 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -52,9 +52,10 @@ uuid = { version = "0.8", features = ["serde", "v4"] }
xesite_types = { path = "./lib/xesite_types" }
# workspace dependencies
-xe_jsonfeed = { path = "./lib/jsonfeed" }
+mastodon2text = { path = "./lib/mastodon2text" }
mi = { path = "./lib/mi" }
patreon = { path = "./lib/patreon" }
+xe_jsonfeed = { path = "./lib/jsonfeed" }
xesite_markdown = { path = "./lib/xesite_markdown" }
xesite_templates = { path = "./lib/xesite_templates" }
diff --git a/lib/mastodon2text/Cargo.toml b/lib/mastodon2text/Cargo.toml
new file mode 100644
index 0000000..25bcedf
--- /dev/null
+++ b/lib/mastodon2text/Cargo.toml
@@ -0,0 +1,9 @@
+[package]
+name = "mastodon2text"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+lol_html = "0.3"
diff --git a/lib/mastodon2text/src/lib.rs b/lib/mastodon2text/src/lib.rs
new file mode 100644
index 0000000..0359c8a
--- /dev/null
+++ b/lib/mastodon2text/src/lib.rs
@@ -0,0 +1,39 @@
+use lol_html::{element, html_content::ContentType, HtmlRewriter, Settings};
+use std::error::Error;
+
+pub fn convert(input: String) -> Result<String, Box<dyn Error>> {
+ let mut output = Vec::new();
+
+ let mut rewriter = HtmlRewriter::new(
+ Settings {
+ element_content_handlers: vec![
+ element!("span", |el| {
+ el.remove_and_keep_content();
+ Ok(())
+ }),
+ element!("p", |el| {
+ el.append(" ", ContentType::Html);
+ el.remove_and_keep_content();
+ Ok(())
+ }),
+ element!("br", |el| {
+ el.append(" ", ContentType::Html);
+ el.remove_and_keep_content();
+ Ok(())
+ }),
+ element!("a[href]", |el| {
+ el.remove_and_keep_content();
+
+ Ok(())
+ }),
+ ],
+ ..Settings::default()
+ },
+ |c: &[u8]| output.extend_from_slice(c),
+ );
+
+ rewriter.write(input.as_bytes())?;
+ rewriter.end()?;
+
+ Ok(String::from_utf8_lossy(&output).to_string())
+}
diff --git a/src/main.rs b/src/main.rs
index 37c0a4d..aaf4ea4 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -4,7 +4,6 @@ extern crate tracing;
use axum::{
body,
extract::Extension,
- handler::Handler,
http::header::{self, HeaderValue, CONTENT_TYPE},
response::Response,
routing::{get, get_service},
@@ -22,10 +21,7 @@ use std::{
};
use tokio::net::UnixListener;
use tower_http::{
- cors::CorsLayer,
- services::{ServeDir, ServeFile},
- set_header::SetResponseHeaderLayer,
- trace::TraceLayer,
+ cors::CorsLayer, services::ServeFile, set_header::SetResponseHeaderLayer, trace::TraceLayer,
};
pub mod app;
diff --git a/src/post/mod.rs b/src/post/mod.rs
index 65c859e..be3bf04 100644
--- a/src/post/mod.rs
+++ b/src/post/mod.rs
@@ -130,6 +130,16 @@ async fn read_post(dir: &str, fname: PathBuf, cli: &Option<mi::Client>) -> Resul
.filter(|wm| {
wm.title.as_ref().unwrap_or(&"".to_string()) != &"Bridgy Response".to_string()
})
+ .map(|wm| {
+ let mut wm = wm.clone();
+ wm.title = Some(
+ mastodon2text::convert(
+ wm.title.as_ref().unwrap_or(&"".to_string()).to_string(),
+ )
+ .unwrap(),
+ );
+ wm
+ })
.collect(),
None => vec![],
};