diff options
| -rw-r--r-- | src/lib.rs | 8 | ||||
| -rw-r--r-- | src/matrix.rs | 68 | ||||
| -rw-r--r-- | src/telegram.rs | 2 | ||||
| -rw-r--r-- | tests/test_matrix.py | 147 | ||||
| -rw-r--r-- | tests/test_telegram.py | 130 |
5 files changed, 208 insertions, 147 deletions
@@ -3,7 +3,10 @@ use pyo3::prelude::*; mod parser; mod telegram; -use telegram::parse_for_telegram; +use telegram::format_for_telegram; + +mod matrix; +use matrix::format_for_matrix; mod general; use general::format_body; @@ -11,6 +14,7 @@ use general::format_body; #[pymodule] fn slidge_style_parser(_py: Python, m: &PyModule) -> PyResult<()> { m.add_function(wrap_pyfunction!(format_body, m)?)?; - m.add_function(wrap_pyfunction!(parse_for_telegram, m)?)?; + m.add_function(wrap_pyfunction!(format_for_matrix, m)?)?; + m.add_function(wrap_pyfunction!(format_for_telegram, m)?)?; Ok(()) } diff --git a/src/matrix.rs b/src/matrix.rs new file mode 100644 index 0000000..92d2eb8 --- /dev/null +++ b/src/matrix.rs @@ -0,0 +1,68 @@ +use pyo3::prelude::*; + +use crate::parser::parse_with_limits; + +const MATRIX_FORMATS: &[(&'static str, (&'static str, &'static str))] = &[ + ("_", ("<em>", "</em>")), + ("*", ("<strong>", "</strong>")), + ("~", ("<strike>", "</strike>")), + ("`", ("<code>", "</code>")), + ("```", ("<pre><code>", "</code></pre>")), + ("```language", ("<pre><code class=\"language-{}\">", "</code></pre>")), + (">", ("<blockquote>", "</blockquote>")), + ("||", ("<span data-mx-spoiler>", "</span>")), +]; + +#[pyfunction] +pub fn format_for_matrix(body: String) -> PyResult<String> { + let mut chars: Vec<char> = body.chars().collect(); + if chars.len() < 1 { + return Ok(body); + } + 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) { + let opening_tag = if keyword == "```language" { + MATRIX_FORMATS.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_string() + }; + tags.push((start, opening_tag, remove_start)); + tags.push((end, MATRIX_FORMATS.iter().find(|&&(k, _)| k == keyword).unwrap().1.1.clone().to_string(), remove_end)); + } else if keyword == ">>" || keyword == "```>" || keyword == "\\" { + tags.push((start, "".to_string(), start+1)); + } + } + + tags.sort_by(|a, b| b.0.cmp(&a.0)); + + let mut replace_newlines_to = chars.len(); + for (index, tag, end) in tags { + if tag == "</code></pre>" { + // index is at \n, add 1 to skip that one + let substring = chars[index + 1..replace_newlines_to].into_iter().collect::<String>(); + chars = [&chars[..index + 1], &substring.replace('\n', "<br>").chars().collect::<Vec<char>>()[..]].concat(); + } else if tag == "<pre><code>" { + replace_newlines_to = index; + } + + let tag: Vec<char> = tag.chars().collect(); + chars = [chars[..index].to_vec(), tag.clone(), chars[end..].to_vec()].concat(); + + let offset: isize = index as isize - end as isize + tag.len() as isize; + replace_newlines_to = if offset > 0 { + replace_newlines_to + offset as usize + } else { + replace_newlines_to - offset.abs() as usize + }; + } + let substring = chars[..replace_newlines_to].into_iter().collect::<String>(); + let text = [substring.replace('\n', "<br>"), chars[replace_newlines_to..].into_iter().collect::<String>()].concat(); + + Ok(text) +} diff --git a/src/telegram.rs b/src/telegram.rs index 0774143..b215885 100644 --- a/src/telegram.rs +++ b/src/telegram.rs @@ -15,7 +15,7 @@ const TELEGRAM_STYLES: &[(&'static str, &'static str)] = &[ ]; #[pyfunction] -pub fn parse_for_telegram(body: String) -> PyResult<(String, Vec<(String, usize, usize, String)>)> { +pub fn format_for_telegram(body: String) -> PyResult<(String, Vec<(String, usize, usize, String)>)> { let mut chars: Vec<char> = body.chars().collect(); if chars.len() < 1 { return Ok((body, Vec::with_capacity(0))); diff --git a/tests/test_matrix.py b/tests/test_matrix.py index 0bc6f1a..be33973 100644 --- a/tests/test_matrix.py +++ b/tests/test_matrix.py @@ -1,4 +1,4 @@ -from slidge_style_parser import format_body +from slidge_style_parser import format_for_matrix MATRIX_FORMATS = { "_": ("<em>", "</em>"), @@ -15,270 +15,259 @@ MATRIX_FORMATS = { def test_basic(): test = "_underline_" formatted_body = "<em>underline</em>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "*bold*" formatted_body = "<strong>bold</strong>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "~strikethrough~" formatted_body = "<strike>strikethrough</strike>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "`code span`" formatted_body = "<code>code span</code>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = """ ```python def test_basic(): test = "_underline_" formatted_body = "<em>underline</em>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) ``` """ - formatted_body = '<br><pre><code class="language-python"> def test_basic():<br> test = "_underline_"<br> formatted_body = "<em>underline</em>"<br> assert(format_body(test, MATRIX_FORMATS) == formatted_body)</code></pre><br>' - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + formatted_body = '<br><pre><code class="language-python"> def test_basic():<br> test = "_underline_"<br> formatted_body = "<em>underline</em>"<br> assert(format_for_matrix(test) == formatted_body)</code></pre><br>' + assert(format_for_matrix(test) == formatted_body) test = "```\ncode block\n```" formatted_body = "<pre><code>code block</code></pre>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "||this message contains a spoiler||" formatted_body = "<span data-mx-spoiler>this message contains a spoiler</span>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) def test_quotes(): test = ">single" formatted_body = "<blockquote>single</blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">single arrow ->" formatted_body = "<blockquote>single arrow -></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">single\n>grouped" formatted_body = "<blockquote>single<br>grouped</blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">>double" formatted_body = "<blockquote><blockquote>double</blockquote></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">>double\n>>double" formatted_body = "<blockquote><blockquote>double<br>double</blockquote></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">>double\n&>not quote" formatted_body = "<blockquote><blockquote>double</blockquote></blockquote><br>&>not quote" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">>double\n>grouped single" formatted_body = "<blockquote><blockquote>double</blockquote><br>grouped single</blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">>>tripple\n>single\n>>double" formatted_body = "<blockquote><blockquote><blockquote>tripple</blockquote></blockquote><br>single<br><blockquote>double</blockquote></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) def test_code_blocks(): test = "```\nhacker\ncode\n```" - formatted_body = "<pre><code>hacker<br>code</code></pre>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + formatted_body = "<pre><code>hacker\ncode</code></pre>" + assert(format_for_matrix(test) == formatted_body) test = "```python\nhacker code\n```" formatted_body = "<pre><code class=\"language-python\">hacker code</code></pre>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "```pythonaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nhacker code\n```" formatted_body = "<pre><code class=\"language-pythonaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\">hacker code</code></pre>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "```python\nhacker code\n```\nnormal text" formatted_body = "<pre><code class=\"language-python\">hacker code</code></pre><br>normal text" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "```python\nhacker code\n```\nnormal text\n```java\npublic static void main(String [])\n```" formatted_body = "<pre><code class=\"language-python\">hacker code</code></pre><br>normal text<br><pre><code class=\"language-java\">public static void main(String [])</code></pre>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">```java\n>why are you quoting a code block\n>```" formatted_body = "<blockquote><pre><code class=\"language-java\">why are you quoting a code block</code></pre></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">>```\n>>double quote code block\n>single quote not in code block\nnormal text" formatted_body = "<blockquote><blockquote><pre><code>double quote code block</code></pre></blockquote><br>single quote not in code block</blockquote><br>normal text" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">```\n>please stop trying to break my parser ;-;" formatted_body = "<blockquote><pre><code>please stop trying to break my parser ;-;</code></pre></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">>```\n>>>>double quote code block\n>single quote not in code block\nnormal text" formatted_body = "<blockquote><blockquote><pre><code>>>double quote code block</code></pre></blockquote><br>single quote not in code block</blockquote><br>normal text" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "_```_ignored\ninvalid code block\n```" formatted_body = "<em>```</em>ignored<br>invalid code block<br>```" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) def test_escaped(): test = "\\_no underline_" formatted_body = "_no underline_" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "\\\\_no underline_" formatted_body = "\\_no underline_" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">>>tripple\n\\>none\n>>double" formatted_body = "<blockquote><blockquote><blockquote>tripple</blockquote></blockquote></blockquote><br>>none<br><blockquote><blockquote>double</blockquote></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) def test_nested(): test = "`*~_code span_~*`" formatted_body = "<code>*~_code span_~*</code>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "*_~`code span`~_*" formatted_body = "<strong><em><strike><code>code span</code></strike></em></strong>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">*_~`code span`~_*" formatted_body = "<blockquote><strong><em><strike><code>code span</code></strike></em></strong></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "*bold star >*< star bold*" formatted_body = "<strong>bold star >*< star bold</strong>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "*_bold*_" formatted_body = "<strong>_bold</strong>_" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "__underlined__" formatted_body = "<em><em>underlined</em></em>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) def test_no_changes(): test = "" formatted_body = "" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "~~ empty `````` styles **" formatted_body = "~~ empty `````` styles **" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "this is not an empty string" formatted_body = "this is not an empty string" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "arrow ->" formatted_body = "arrow ->" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = " > no quote" formatted_body = " > no quote" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "_not underlined" formatted_body = "_not underlined" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "|not a spoiler|" formatted_body = "|not a spoiler|" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "||\nalso\nnot\na\nspoiler||" formatted_body = "||<br>also<br>not<br>a<br>spoiler||" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "`no code\nblock here`" formatted_body = "`no code<br>block here`" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "invalid ```\ncode block\n```" formatted_body = "invalid ```<br>code block<br>```" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "```\ncode block\ninvalid```" formatted_body = "```<br>code block<br>invalid```" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "```\ncode block\n```invalid" formatted_body = "```<br>code block<br>```invalid" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) def test_assorted(): test = "\n" formatted_body = "<br>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "at the ||end||" formatted_body = "at the <span data-mx-spoiler>end</span>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "in the ~middle~ here" formatted_body = "in the <strike>middle</strike> here" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||" formatted_body = "<em>underline</em> <strong>bold</strong> <strike>strikethrough</strike> >not quote <span data-mx-spoiler>spoiler</span><br><blockquote>quote</blockquote><br>nothing<br>nothing<br><blockquote><blockquote><blockquote><blockquote>another quote with <span data-mx-spoiler><strike><em><strong>```four```</strong></em></strike></span></blockquote></blockquote></blockquote></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">```\n>do be do be dooo ba do be do be do ba\n>>>" - formatted_body = "<blockquote><pre><code>do be do be dooo ba do be do be do ba<br>>></code></pre></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + formatted_body = "<blockquote><pre><code>do be do be dooo ba do be do be do ba\n>></code></pre></blockquote>" + assert(format_for_matrix(test) == formatted_body) test = "\n\n>```\n>do be do be dooo ba do be do be do ba\na\n\n\naoeu\n" formatted_body = "<br><br><blockquote><pre><code>do be do be dooo ba do be do be do ba</code></pre></blockquote><br>a<br><br><br>aoeu<br>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">```\n>do be do be dooo ba do be do be do ba\n>\n>\n>aoeu" - formatted_body = "<blockquote><pre><code>do be do be dooo ba do be do be do ba<br><br><br>aoeu</code></pre></blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + formatted_body = "<blockquote><pre><code>do be do be dooo ba do be do be do ba\n\n\naoeu</code></pre></blockquote>" + assert(format_for_matrix(test) == formatted_body) test = ">```\n>code block\n>```invalid end\n" - formatted_body = "<blockquote><pre><code>code block<br>```invalid end</code></pre></blockquote><br>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + formatted_body = "<blockquote><pre><code>code block\n```invalid end</code></pre></blockquote><br>" + assert(format_for_matrix(test) == formatted_body) test = "invalid ```\ncode block\n*bold*\n```" formatted_body = "invalid ```<br>code block<br><strong>bold</strong><br>```" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) def test_weird_utf8(): test = "β€οΈππππ ||πππππ€|| πππβ£οΈ" formatted_body = "β€οΈππππ <span data-mx-spoiler>πππππ€</span> πππβ£οΈ" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "π¨βπ©βπ§βπ§ _underline_π©βπ©βπ¦βπ§" formatted_body = "π¨βπ©βπ§βπ§ <em>underline</em>π©βπ©βπ¦βπ§" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "\u202eRight to left" formatted_body = "\u202eRight to left" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = ">\u202eRight to left quote?" formatted_body = "<blockquote>\u202eRight to left quote?</blockquote>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "_Invisible\u200bseparator_" formatted_body = "<em>Invisible\u200bseparator</em>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) test = "~\u200b~" formatted_body = "<strike>\u200b</strike>" - assert(format_body(test, MATRIX_FORMATS) == formatted_body) - -LIMITED_FORMATS = { - "_": ("<em>", "</em>"), - "~": ("<strike>", "</strike>"), - "`": ("<code>", "</code>") -} - -def test_limited(): - test = "_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||" - formatted_body = "<em>underline</em> *bold* <strike>strikethrough</strike> >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||<strike><em>*```four```*</em></strike>||" - assert(format_body(test, LIMITED_FORMATS) == formatted_body) + assert(format_for_matrix(test) == formatted_body) diff --git a/tests/test_telegram.py b/tests/test_telegram.py index 164a697..1fbbb8d 100644 --- a/tests/test_telegram.py +++ b/tests/test_telegram.py @@ -1,268 +1,268 @@ -from slidge_style_parser import parse_for_telegram +from slidge_style_parser import format_for_telegram def test_basic(): test = "_underline_" formatted_body = "underline" styles = [('italics', 1, 8, '')] - assert(parse_for_telegram(test) == (formatted_body, styles)) + assert(format_for_telegram(test) == (formatted_body, styles)) test = "*bold*" formatted_body = "bold" styles = [('bold', 1, 3, '')] - assert(parse_for_telegram(test) == (formatted_body, styles)) + assert(format_for_telegram(test) == (formatted_body, styles)) test = "~strikethrough~" formatted_body = "strikethrough" styles = [('strikethrough', 1, 12, '')] - assert(parse_for_telegram(test) == (formatted_body, styles)) + assert(format_for_telegram(test) == (formatted_body, styles)) test = "`code span`" formatted_body = "code span" styles = [('code', 1, 8, '')] - assert(parse_for_telegram(test) == (formatted_body, styles)) + assert(format_for_telegram(test) == (formatted_body, styles)) test = """ ```python def test_basic(): test = "_underline_" formatted_body = "underline" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) ``` """ - formatted_body = '\n def test_basic():\n test = "_underline_"\n formatted_body = "underline"\n assert(parse_for_telegram(test)[0] == formatted_body)\n' - styles = [('pre', 2, 148, 'python')] - assert(parse_for_telegram(test) == (formatted_body, styles)) + formatted_body = '\n def test_basic():\n test = "_underline_"\n formatted_body = "underline"\n assert(format_for_telegram(test)[0] == formatted_body)\n' + styles = [('pre', 2, 149, 'python')] + assert(format_for_telegram(test) == (formatted_body, styles)) test = "```\ncode block\n```" formatted_body = "code block" styles = [('pre', 1, 9, '')] - assert(parse_for_telegram(test) == (formatted_body, styles)) + assert(format_for_telegram(test) == (formatted_body, styles)) test = "||this message contains a spoiler||" formatted_body = "this message contains a spoiler" styles = [('spoiler', 1, 30, '')] - assert(parse_for_telegram(test) == (formatted_body, styles)) + assert(format_for_telegram(test) == (formatted_body, styles)) def test_quotes(): test = ">single" formatted_body = ">single" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">single arrow ->" formatted_body = ">single arrow ->" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">single\n>grouped" formatted_body = ">single\n>grouped" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">>double" formatted_body = ">>double" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">>double\n>>double" formatted_body = ">>double\n>>double" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">>double\n&>not quote" formatted_body = ">>double\n&>not quote" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">>double\n>grouped single" formatted_body = ">>double\n>grouped single" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">>>tripple\n>single\n>>double" formatted_body = ">>>tripple\n>single\n>>double" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) def test_code_blocks(): test = "```\nhacker\ncode\n```" formatted_body = "hacker\ncode" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "```python\nhacker code\n```" formatted_body = "hacker code" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "```pythonaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\nhacker code\n```" formatted_body = "hacker code" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "```python\nhacker code\n```\nnormal text" formatted_body = "hacker code\nnormal text" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "```python\nhacker code\n```\nnormal text\n```java\npublic static void main(String [])\n```" formatted_body = "hacker code\nnormal text\npublic static void main(String [])" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">```java\n>why are you quoting a code block\n>```" formatted_body = ">why are you quoting a code block" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">>```\n>>double quote code block\n>single quote not in code block\nnormal text" formatted_body = ">>double quote code block\n>single quote not in code block\nnormal text" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">```\n>please stop trying to break my parser ;-;" formatted_body = ">please stop trying to break my parser ;-;" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">>```\n>>>>double quote code block\n>single quote not in code block\nnormal text" formatted_body = ">>>>double quote code block\n>single quote not in code block\nnormal text" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "_```_ignored\ninvalid code block\n```" formatted_body = "```ignored\ninvalid code block\n```" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) def test_escaped(): test = "\\_no underline_" formatted_body = "_no underline_" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "\\\\_no underline_" formatted_body = "\\_no underline_" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">>>tripple\n\\>none\n>>double" formatted_body = ">>>tripple\n>none\n>>double" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) def test_nested(): test = "`*~_code span_~*`" formatted_body = "*~_code span_~*" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "*_~`code span`~_*" formatted_body = "code span" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">*_~`code span`~_*" formatted_body = ">code span" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "*bold star >*< star bold*" formatted_body = "bold star >*< star bold" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "*_bold*_" formatted_body = "_bold_" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "__underlined__" formatted_body = "underlined" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) def test_no_changes(): test = "" formatted_body = "" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "~~ empty `````` styles **" formatted_body = "~~ empty `````` styles **" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "this is not an empty string" formatted_body = "this is not an empty string" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "arrow ->" formatted_body = "arrow ->" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = " > no quote" formatted_body = " > no quote" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "_not underlined" formatted_body = "_not underlined" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "|not a spoiler|" formatted_body = "|not a spoiler|" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "||\nalso\nnot\na\nspoiler||" formatted_body = "||\nalso\nnot\na\nspoiler||" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "`no code\nblock here`" formatted_body = "`no code\nblock here`" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "invalid ```\ncode block\n```" formatted_body = "invalid ```\ncode block\n```" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "```\ncode block\ninvalid```" formatted_body = "```\ncode block\ninvalid```" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "```\ncode block\n```invalid" formatted_body = "```\ncode block\n```invalid" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) def test_assorted(): test = "\n" formatted_body = "\n" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "at the ||end||" formatted_body = "at the end" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "in the ~middle~ here" formatted_body = "in the middle here" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "_underline_ *bold* ~strikethrough~ >not quote ||spoiler||\n>quote\nnothing\nnothing\n>>>>another quote with ||~_*```four```*_~||" formatted_body = "underline bold strikethrough >not quote spoiler\n>quote\nnothing\nnothing\n>>>>another quote with ```four```" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">```\n>do be do be dooo ba do be do be do ba\n>>>" formatted_body = ">do be do be dooo ba do be do be do ba\n>>" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "\n\n>```\n>do be do be dooo ba do be do be do ba\na\n\n\naoeu\n" formatted_body = "\n\n>do be do be dooo ba do be do be do ba\na\n\n\naoeu\n" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">```\n>do be do be dooo ba do be do be do ba\n>\n>\n>aoeu" formatted_body = ">do be do be dooo ba do be do be do ba\n\n\naoeu" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">```\n>code block\n>```invalid end\n" formatted_body = ">code block\n```invalid end\n" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "invalid ```\ncode block\n*bold*\n```" formatted_body = "invalid ```\ncode block\nbold\n```" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) def test_weird_utf8(): test = "β€οΈππππ ||πππππ€|| πππβ£οΈ" formatted_body = "β€οΈππππ πππππ€ πππβ£οΈ" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "π¨βπ©βπ§βπ§ _underline_π©βπ©βπ¦βπ§" formatted_body = "π¨βπ©βπ§βπ§ underlineπ©βπ©βπ¦βπ§" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "\u202eRight to left" formatted_body = "\u202eRight to left" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = ">\u202eRight to left quote?" formatted_body = ">\u202eRight to left quote?" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "_Invisible\u200bseparator_" formatted_body = "Invisible\u200bseparator" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) test = "~\u200b~" formatted_body = "\u200b" - assert(parse_for_telegram(test)[0] == formatted_body) + assert(format_for_telegram(test)[0] == formatted_body) |
