diff options
| author | SavagePeanut <sourcehut@lazytapir.com> | 2023-09-03 15:43:06 -0500 |
|---|---|---|
| committer | SavagePeanut <sourcehut@lazytapir.com> | 2023-09-03 15:43:06 -0500 |
| commit | 2028c5a6cff6f5b860b6419cbc1b27a1260dcd00 (patch) | |
| tree | 6a0301485682e836516ba07cbeb712648b88ab27 | |
| parent | 651ab7c90f436de60035a138e91561b1848b5725 (diff) | |
count \s removed for telegram
| -rw-r--r-- | src/general.rs | 12 | ||||
| -rw-r--r-- | src/parser.rs | 20 | ||||
| -rw-r--r-- | src/telegram.rs | 5 |
3 files changed, 9 insertions, 28 deletions
diff --git a/src/general.rs b/src/general.rs index 517bf92..110a1c7 100644 --- a/src/general.rs +++ b/src/general.rs @@ -4,8 +4,6 @@ use pyo3::prelude::*; use crate::parser::parse_with_limits; -const PLACEHOLDER: &str = "\u{200B}\u{200B}\u{200B}\u{200B}\u{200B}\u{200B}\u{200B}\u{200B}\u{200B}\u{200B}"; - #[pyfunction] pub fn format_body(body: String, new_tags: HashMap<String, (String, String)>) -> PyResult<String> { let mut chars: Vec<char> = body.chars().collect(); @@ -28,7 +26,7 @@ pub fn format_body(body: String, new_tags: HashMap<String, (String, String)>) -> }; tags.push((start, opening_tag, remove_start)); tags.push((end, new_tags.get(&keyword).unwrap().1.clone(), remove_end)); - } else if (keyword == ">>" && parse_quotes) || keyword == "```>" { + } else if (keyword == ">>" && parse_quotes) || keyword == "```>" || keyword == "\\" { tags.push((start, "".to_string(), start+1)); } } @@ -45,11 +43,5 @@ pub fn format_body(body: String, new_tags: HashMap<String, (String, String)>) -> chars.into_iter().collect::<String>() }; - Ok(remove_non_escaped_backslashes(text)) -} - -fn remove_non_escaped_backslashes(text: String) -> String { - let tmp_string = text.replace("\\\\", PLACEHOLDER); - let tmp_string = tmp_string.replace("\\", ""); - tmp_string.replace(PLACEHOLDER, "\\") + Ok(text) } diff --git a/src/parser.rs b/src/parser.rs index 8e96a69..53c38f6 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -8,12 +8,13 @@ pub fn parse_with_limits(chars: &Vec<char>, start: usize, end: usize, depth: usi let end = end.min(chars.len() - 1); while index <= end { - if preceeded_by_backslash(chars, index, start) { - index += 1; + let c = chars[index]; + if c == '\\' { + styles.push(("\\".to_string(), index, index + 1, index + 1, index + 1)); + index += 2; continue; } - let c = chars[index]; if QUOTE_KEYWORDS.contains(&c) { if is_quote_start(chars, index, depth) { let to = seek_end_of_quote(chars, index, end, depth); @@ -168,7 +169,6 @@ fn seek_end(chars: &Vec<char>, keyword: char, start: usize, repetitions: usize, } if c == keyword && !chars[i - 1].is_whitespace() - && !preceeded_by_backslash(chars, i, start) && is_char_repeating(chars, keyword, repetitions, i + 1, end) { match seek_higher_order_end(chars, c, i + 1, repetitions, end) { @@ -200,7 +200,6 @@ fn seek_higher_order_end(chars: &Vec<char>, keyword: char, start: usize, repetit if c == keyword && !chars[i - 1].is_whitespace() && followed_by_whitespace(chars, i, end) - && !preceeded_by_backslash(chars, i, start) && is_char_repeating(chars, keyword, repetitions, i + 1, end) { return Some(i); @@ -261,14 +260,3 @@ fn seek_end_block(chars: &Vec<char>, keyword: char, start: usize, end: usize, de fn is_quote_start(chars: &Vec<char>, index: usize, depth: usize) -> bool { index - depth == 0 || chars[index - 1 - depth] == '\n' } - -fn preceeded_by_backslash(chars: &Vec<char>, index: usize, start: usize) -> bool { - if index == start { - return false; - } - let mut num_backslashes = 0; - while index > num_backslashes && chars[index - 1 - num_backslashes] == '\\' { - num_backslashes += 1; - } - num_backslashes % 2 == 1 -} diff --git a/src/telegram.rs b/src/telegram.rs index ac6eeb3..74473c6 100644 --- a/src/telegram.rs +++ b/src/telegram.rs @@ -10,6 +10,7 @@ const TELEGRAM_STYLES: &[(&'static str, &'static str)] = &[ ("~", "strikethrough"), ("||", "spoiler"), ("`", "code"), + ("```language", "pre"), ("```", "pre") ]; @@ -26,7 +27,7 @@ pub fn parse_for_telegram(body: String) -> PyResult<(String, Vec<(String, usize, if TELEGRAM_STYLES.iter().any(|&(k, _)| k == keyword) { remove_tags.push((*start, *remove_start)); remove_tags.push((*end, *remove_end)); - } else if keyword == "```>" { + } else if keyword == "```>" || keyword == "\\" { remove_tags.push((*start, *remove_start)); } } @@ -52,7 +53,7 @@ pub fn parse_for_telegram(body: String) -> PyResult<(String, Vec<(String, usize, let last_index = all_indexes.len() - 1; message_entities.push((true, last_index, TELEGRAM_STYLES.iter().find(|&&(k, _)| k == keyword).unwrap().1.to_string(), *start, language)); message_entities.push((false, last_index, "".to_string(), *end, "".to_string())); - } else if keyword == "```>" { + } else if keyword == "```>" || keyword == "\\" { all_indexes.push(vec![0, 0, *start, 1]); message_entities.push((false, all_indexes.len() - 1, "".to_string(), *start, "".to_string())); } |
