move strip to utils

This commit is contained in:
VC
2023-11-07 18:43:15 +01:00
parent 5059abe685
commit 2804a8ab38
2 changed files with 18 additions and 18 deletions

View File

@@ -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("</p><p>", "\n\n")
.replace("<br />", "\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);
}
}

View File

@@ -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<Tag>) -> Result<(), Box<dyn Error>> {
pub fn strip_everything(content: &str, tags: &Vec<Tag>) -> Result<String, Box<dyn Error>> {
let mut res =
strip_html_tags(&content.replace("</p><p>", "\n\n").replace("<br />", "\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<Tag>) -> Result<(), Box<dyn Error>> {
for tag in tags {
let re = Regex::new(&format!("(?i)(#{} ?)", &tag.name))?;
*content = re.replace(content, "").to_string();
}
Ok(())
}