diff --git a/src/lib.rs b/src/lib.rs index c2aa1da..69f9d31 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,9 +10,8 @@ use mastodon::get_mastodon_timeline_since; pub use mastodon::register; mod utils; -use utils::strip_mastodon_tags; +use utils::strip_everything; -use dissolve::strip_html_tags; use rusqlite::Connection; #[tokio::main] @@ -29,21 +28,9 @@ pub async fn run(config: &Config) { .unwrap_or_else(|e| panic!("Cannot get instance: {}", e)); for toot in timeline { - let mut tweet_content = strip_html_tags( - &toot - .content - .replace("

", "\n\n") - .replace("
", "\n"), - ) - .join(""); - - strip_mastodon_tags(&mut tweet_content, &toot.tags).unwrap(); - - tweet_content = tweet_content - .trim_end_matches('\n') - .trim_end_matches(' ') - .to_string(); - + let Ok(tweet_content) = strip_everything(&toot.content, &toot.tags) else { + continue; + }; println!("{:?}", tweet_content); } } diff --git a/src/utils.rs b/src/utils.rs index 59406af..8cd4004 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,11 +1,24 @@ +use dissolve::strip_html_tags; use megalodon::entities::status::Tag; use regex::Regex; use std::error::Error; -pub fn strip_mastodon_tags(content: &mut String, tags: &Vec) -> Result<(), Box> { +pub fn strip_everything(content: &str, tags: &Vec) -> Result> { + let mut res = + strip_html_tags(&content.replace("

", "\n\n").replace("
", "\n")).join(""); + + strip_mastodon_tags(&mut res, tags).unwrap(); + + res = res.trim_end_matches('\n').trim_end_matches(' ').to_string(); + + Ok(res) +} + +fn strip_mastodon_tags(content: &mut String, tags: &Vec) -> Result<(), Box> { for tag in tags { let re = Regex::new(&format!("(?i)(#{} ?)", &tag.name))?; *content = re.replace(content, "").to_string(); } + Ok(()) }