diff options
| author | Xe Iaso <me@christine.website> | 2023-01-04 14:55:54 -0500 |
|---|---|---|
| committer | Xe Iaso <me@christine.website> | 2023-01-04 14:55:54 -0500 |
| commit | 665bc0b334a671587dcbc39f5e2b77b24a6df88f (patch) | |
| tree | 74f77831a70c5be49e01b477637af7e2ca1b878d /lib | |
| parent | 351069d9f91edab96425bcd221858529acb7e08a (diff) | |
| download | xesite-665bc0b334a671587dcbc39f5e2b77b24a6df88f.tar.xz xesite-665bc0b334a671587dcbc39f5e2b77b24a6df88f.zip | |
attempt to change webmention titles
Signed-off-by: Xe Iaso <me@christine.website>
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/mastodon2text/Cargo.toml | 9 | ||||
| -rw-r--r-- | lib/mastodon2text/src/lib.rs | 39 |
2 files changed, 48 insertions, 0 deletions
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()) +} |
