mirror of
https://framagit.org/veretcle/oolatoocs.git
synced 2025-07-20 20:41:17 +02:00
move strip to utils
This commit is contained in:
21
src/lib.rs
21
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("</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);
|
||||
}
|
||||
}
|
||||
|
15
src/utils.rs
15
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<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(())
|
||||
}
|
||||
|
Reference in New Issue
Block a user