use html_escape::decode_html_entities; use megalodon::entities::status::Tag; use regex::Regex; use std::error::Error; pub fn strip_everything(content: &str, tags: &Vec) -> Result> { let mut res = strip_html_tags(&content.replace("

", "\n\n").replace("
", "\n")); strip_mastodon_tags(&mut res, tags).unwrap(); res = res.trim_end_matches('\n').trim_end_matches(' ').to_string(); res = decode_html_entities(&res).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(()) } fn strip_html_tags(input: &str) -> String { let mut data = String::new(); let mut inside = false; for c in input.chars() { if c == '<' { inside = true; continue; } if c == '>' { inside = false; continue; } if !inside { data.push(c); } } data } #[cfg(test)] mod tests { use super::*; #[test] fn test_strip_mastodon_tags() { let tags = vec![ Tag { name: "putaclic".to_string(), url: "https://m.nintendojo.fr/tags/putaclic".to_string(), }, Tag { name: "tamerelol".to_string(), url: "https://m.nintendojo.fr/tags/tamerelol".to_string(), }, Tag { name: "JeFaisNawakEnCamelCase".to_string(), url: "https://m.nintendojo.fr/tags/jefaisnawakencamelcase".to_string(), }, ]; let mut content = "C’est super ça ! #putaclic #TAMERELOL #JeFaisNawakEnCamelCase".to_string(); let sample = "C’est super ça ! ".to_string(); strip_mastodon_tags(&mut content, &tags).unwrap(); assert_eq!(content, sample); } #[test] fn test_strip_everything() { let content = "

Ce soir à 21h, c'est le Dojobar ! Au programme ce soir, une rétrospective sur la série Mario & Luigi.
Comme d'hab, le Twitch sera ici : https://twitch.tv/nintendojofr
Ou juste l'audio là : https://nintendojo.fr/dojobar
A toute !

"; let expected_result = "Ce soir à 21h, c'est le Dojobar ! Au programme ce soir, une rétrospective sur la série Mario & Luigi.\nComme d'hab, le Twitch sera ici : https://twitch.tv/nintendojofr\nOu juste l'audio là : https://nintendojo.fr/dojobar\nA toute !".to_string(); let result = strip_everything(content, &vec![]).unwrap(); assert_eq!(result, expected_result); } }