From 19a087d08d6024a59ad943bfee3086a6710b9278 Mon Sep 17 00:00:00 2001 From: SavagePeanut Date: Tue, 15 Aug 2023 15:02:07 -0500 Subject: replace newlines --- src/lib.rs | 11 ++++++--- tests/test_style_parser.py | 58 +++++++++++++++++++++++----------------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index ceca3a1..89387b0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -42,7 +42,13 @@ fn format_body(body: String, new_tags: HashMap) -> PyR chars = [chars[..index].to_vec(), tag.chars().collect(), chars[end..].to_vec()].concat(); } - Ok(remove_non_escaped_backslashes(chars.into_iter().collect())) + let text: String = if new_tags.contains_key("\n") { + chars.into_iter().collect::().replace("\n", &new_tags.get(&"\n".to_string()).unwrap().0) + } else { + chars.into_iter().collect::() + }; + + Ok(remove_non_escaped_backslashes(text)) } fn remove_non_escaped_backslashes(text: String) -> String { @@ -100,9 +106,8 @@ fn parse_with_limits(chars: &Vec, start: usize, end: usize, depth: usize) }; styles.push((keyword, index, end_of_line + 1, to, remove_end)); styles.append(&mut parse_quotes_in_code_block(chars, index + 3, to, depth)); + index = to; } - index = to + 3; - continue; } None => () } diff --git a/tests/test_style_parser.py b/tests/test_style_parser.py index 673b63c..86a7eff 100644 --- a/tests/test_style_parser.py +++ b/tests/test_style_parser.py @@ -8,7 +8,8 @@ MATRIX_FORMATS = { "```": ("
", "
"), "```language": ("
", "
"), ">": ("
", "
"), - "||": ("", "") + "||": ("", ""), + "\n": ("
", "") } def test_basic(): @@ -36,12 +37,7 @@ def test_basic(): assert(format_body(test, MATRIX_FORMATS) == formatted_body) ``` """ - formatted_body = test = """ -
def test_basic():
-        test = "_underline_"
-        formatted_body = "underline"
-        assert(format_body(test, MATRIX_FORMATS) == (test, formatted_body))
- """ + formatted_body = test = """
def test_basic():
test = "_underline_"
formatted_body = "underline"
assert(format_body(test, MATRIX_FORMATS) == (test, formatted_body))

""" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = "```\ncode block\n```" @@ -62,7 +58,7 @@ def test_quotes(): assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">single\n>grouped" - formatted_body = "
single\ngrouped
" + formatted_body = "
single
grouped
" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">>double" @@ -70,24 +66,24 @@ def test_quotes(): assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">>double\n>>double" - formatted_body = "
double\ndouble
" + formatted_body = "
double
double
" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">>double\n&>not quote" - formatted_body = "
double
\n&>not quote" + formatted_body = "
double

&>not quote" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">>double\n>grouped single" - formatted_body = "
double
\ngrouped single
" + formatted_body = "
double

grouped single
" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">>>tripple\n>single\n>>double" - formatted_body = "
tripple
\nsingle\n
double
" + formatted_body = "
tripple

single
double
" assert(format_body(test, MATRIX_FORMATS) == formatted_body) def test_code_blocks(): test = "```\nhacker\ncode\n```" - formatted_body = "
hacker\ncode
" + formatted_body = "
hacker
code
" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = "```python\nhacker code\n```" @@ -99,7 +95,7 @@ def test_code_blocks(): assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">>```\n>>double quote code block\n>single quote not in code block\nnormal text" - formatted_body = "
double quote code block
\nsingle quote not in code block
\nnormal text" + formatted_body = "
double quote code block

single quote not in code block

normal text" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">```\n>please stop trying to break my parser ;-;" @@ -107,11 +103,11 @@ def test_code_blocks(): assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">>```\n>>>>double quote code block\n>single quote not in code block\nnormal text" - formatted_body = "
>>double quote code block
\nsingle quote not in code block
\nnormal text" + formatted_body = "
>>double quote code block

single quote not in code block

normal text" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = "_```_ignored\ninvalid code block\n```" - formatted_body = "```ignored\ninvalid code block\n```" + formatted_body = "```ignored
invalid code block
```" assert(format_body(test, MATRIX_FORMATS) == formatted_body) @@ -125,7 +121,7 @@ def test_escaped(): assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = ">>>tripple\n\\>none\n>>double" - formatted_body = "
tripple
\n>none\n
double
" + formatted_body = "
tripple

>none
double
" assert(format_body(test, MATRIX_FORMATS) == formatted_body) def test_nested(): @@ -183,28 +179,28 @@ def test_no_changes(): assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = "||\nalso\nnot\na\nspoiler||" - formatted_body = "||\nalso\nnot\na\nspoiler||" + formatted_body = "||
also
not
a
spoiler||" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = "`no code\nblock here`" - formatted_body = "`no code\nblock here`" + formatted_body = "`no code
block here`" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = "invalid ```\ncode block\n```" - formatted_body = "invalid ```\ncode block\n```" + formatted_body = "invalid ```
code block
```" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = "```\ncode block\ninvalid```" - formatted_body = "```\ncode block\ninvalid```" + formatted_body = "```
code block
invalid```" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = "```\ncode block\n```invalid" - formatted_body = "```\ncode block\n```invalid" + formatted_body = "```
code block
```invalid" assert(format_body(test, MATRIX_FORMATS) == formatted_body) def test_assorted(): test = "\n" - formatted_body = "\n" + formatted_body = "
" assert(format_body(test, MATRIX_FORMATS) == formatted_body) test = "at the ||end||" @@ -216,23 +212,27 @@ def test_assorted(): assert(format_body(test, MATRIX_FORMATS) == 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```
" + formatted_body = "underline bold strikethrough >not quote spoiler
quote

nothing
nothing
another quote with ```four```
" assert(format_body(test, MATRIX_FORMATS) == 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>>
" + formatted_body = "
do be do be dooo ba do be do be do ba
>>
" assert(format_body(test, MATRIX_FORMATS) == 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" + formatted_body = "

do be do be dooo ba do be do be do ba

a


aoeu
" assert(format_body(test, MATRIX_FORMATS) == 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
" + formatted_body = "
do be do be dooo ba do be do be do ba


aoeu
" + assert(format_body(test, MATRIX_FORMATS) == formatted_body) + + test = ">```\n>code block\n>```invalid end\n" + formatted_body = "
code block
```invalid end

" assert(format_body(test, MATRIX_FORMATS) == formatted_body) - test = ">```\n>code block\n>```invalid\n" - formatted_body = "
code block\n```invalid
\n" + test = "invalid ```\ncode block\n*bold*\n```" + formatted_body = "invalid ```
code block
bold
```" assert(format_body(test, MATRIX_FORMATS) == formatted_body) def test_weird_utf8(): -- cgit v1.2.3