diff options
| author | SavagePeanut <sourcehut@lazytapir.com> | 2024-07-29 13:40:13 -0500 |
|---|---|---|
| committer | SavagePeanut <sourcehut@lazytapir.com> | 2024-07-29 13:40:13 -0500 |
| commit | c88b701e961bdfb5aa4b8673c9d67475353f494d (patch) | |
| tree | a74ee38b8603b327c0744972b2cddaef74554979 /src/matrix.rs | |
| parent | c59f71487d0f479a3fd90b7c02ba1178ec70da10 (diff) | |
escape angle brackets <> for matrix
Diffstat (limited to 'src/matrix.rs')
| -rw-r--r-- | src/matrix.rs | 22 |
1 files changed, 15 insertions, 7 deletions
diff --git a/src/matrix.rs b/src/matrix.rs index 0c359d1..981d906 100644 --- a/src/matrix.rs +++ b/src/matrix.rs @@ -2,7 +2,7 @@ use pyo3::prelude::*; use crate::parser::parse_with_limits; -const MATRIX_FORMATS: &[(&'static str, (&'static str, &'static str))] = &[ +const DUAL_TAGS: &[(&'static str, (&'static str, &'static str))] = &[ ("_", ("<em>", "</em>")), ("*", ("<strong>", "</strong>")), ("~", ("<strike>", "</strike>")), @@ -13,6 +13,14 @@ const MATRIX_FORMATS: &[(&'static str, (&'static str, &'static str))] = &[ ("||", ("<span data-mx-spoiler>", "</span>")), ]; +const SINGLE_TAGS: &[(&'static str, &'static str)] = &[ + (">>", ""), + ("```>", ""), + ("\\", ""), + ("<", "<"), + (">", ">"), +]; + #[pyfunction] pub fn format_for_matrix(body: String, mentions: Option<Vec<(String, usize, usize)>>) -> PyResult<String> { let mut chars: Vec<char> = body.chars().collect(); @@ -24,19 +32,19 @@ pub fn format_for_matrix(body: String, mentions: Option<Vec<(String, usize, usiz let styles: Vec<(String, usize, usize, usize, usize)> = parse_with_limits(&chars, 0, chars.len() - 1, 0); let mut tags: Vec<(usize, String, usize)> = Vec::with_capacity(styles.len() * 2); for (keyword, start, remove_start, end, remove_end) in styles { - if MATRIX_FORMATS.iter().any(|&(k, _)| k == keyword) { + if DUAL_TAGS.iter().any(|&(k, _)| k == keyword) { let opening_tag = if keyword == "```language" { - MATRIX_FORMATS.iter().find(|&&(k, _)| k == keyword).unwrap().1.0.clone() + DUAL_TAGS.iter().find(|&&(k, _)| k == keyword).unwrap().1.0.clone() .replace("{}", &chars[start+3..remove_start-1] .into_iter() .collect::<String>()) } else { - MATRIX_FORMATS.iter().find(|&&(k, _)| k == keyword).unwrap().1.0.clone().to_owned() + DUAL_TAGS.iter().find(|&&(k, _)| k == keyword).unwrap().1.0.clone().to_owned() }; tags.push((start, opening_tag, remove_start)); - tags.push((end, MATRIX_FORMATS.iter().find(|&&(k, _)| k == keyword).unwrap().1.1.clone().to_owned(), remove_end)); - } else if keyword == ">>" || keyword == "```>" || keyword == "\\" { - tags.push((start, String::new(), start+1)); + tags.push((end, DUAL_TAGS.iter().find(|&&(k, _)| k == keyword).unwrap().1.1.clone().to_owned(), remove_end)); + } else if SINGLE_TAGS.iter().any(|&(k, _)| k == keyword) { + tags.push((start, SINGLE_TAGS.iter().find(|&&(k, _)| k == keyword).unwrap().1.to_owned(), start+1)); } } for (mxid, start, end) in mentions { |
