From 665bc0b334a671587dcbc39f5e2b77b24a6df88f Mon Sep 17 00:00:00 2001 From: Xe Iaso Date: Wed, 4 Jan 2023 14:55:54 -0500 Subject: attempt to change webmention titles Signed-off-by: Xe Iaso --- Cargo.lock | 8 ++++++++ Cargo.toml | 3 ++- lib/mastodon2text/Cargo.toml | 9 +++++++++ lib/mastodon2text/src/lib.rs | 39 +++++++++++++++++++++++++++++++++++++++ src/main.rs | 6 +----- src/post/mod.rs | 10 ++++++++++ 6 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 lib/mastodon2text/Cargo.toml create mode 100644 lib/mastodon2text/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index e6c3671..94d69d1 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1474,6 +1474,13 @@ dependencies = [ "tendril", ] +[[package]] +name = "mastodon2text" +version = "0.1.0" +dependencies = [ + "lol_html", +] + [[package]] name = "matches" version = "0.1.9" @@ -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> { + 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) -> 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![], }; -- cgit v1.2.3