Skip to content

Commit

Permalink
fix: newlines not being converted into whitespace properly in paragraphs
Browse files Browse the repository at this point in the history
  • Loading branch information
vhyrro committed Aug 12, 2024
1 parent 8c18777 commit 6137299
Show file tree
Hide file tree
Showing 7 changed files with 90 additions and 166 deletions.
1 change: 1 addition & 0 deletions proptest-regressions/lib.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
# everyone who runs the test benefits from these saved cases.
cc fb9b5df4fe46fe331cc3aa40bba6501c1c603084688fd02dda6d1c73106c1324 # shrinks to tag_name = "A", parameter = "A", multi_parameter = "\u{b}"
cc 28afae9872324ba0632a8023219e32939580363ce8b99752dc19fae0ac5b63d1 # shrinks to paragraph_content = " "
cc 5b31c9987c98fc0e4faa50b782e5952e0948d2c2a60dd29081c8f54c75b4b52c # shrinks to tag_name = "ø", parameter = "a", multi_parameter = " "
1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,6 @@ mod tests {
"*hello*, world!",
"*/hello/*, world!",
"*hi!* how are you?",

"this *is a test",
"this *is/ a test",
"this *is*/ a test",
Expand Down
67 changes: 67 additions & 0 deletions src/snapshots/rust_norg__tests__modifiers.snap
Original file line number Diff line number Diff line change
Expand Up @@ -89,3 +89,70 @@ expression: examples
- Token: Whitespace
- Token:
Text: you?
- - Paragraph:
- Token:
Text: this
- Token: Whitespace
- Token:
Special: "*"
- Token:
Text: is
- Token: Whitespace
- Token:
Text: a
- Token: Whitespace
- Token:
Text: test
- - Paragraph:
- Token:
Text: this
- Token: Whitespace
- Token:
Special: "*"
- Token:
Text: is
- Token:
Special: /
- Token: Whitespace
- Token:
Text: a
- Token: Whitespace
- Token:
Text: test
- - Paragraph:
- Token:
Text: this
- Token: Whitespace
- AttachedModifier:
modifier_type: "*"
content:
- Token:
Text: is
- Token:
Special: /
- Token: Whitespace
- Token:
Text: a
- Token: Whitespace
- Token:
Text: test
- - Paragraph:
- Token:
Text: this
- Token: Whitespace
- AttachedModifier:
modifier_type: "*"
content:
- AttachedModifier:
modifier_type: /
content:
- Token:
Text: is
- Token:
Special: /
- Token: Whitespace
- Token:
Text: a
- Token: Whitespace
- Token:
Text: test
159 changes: 0 additions & 159 deletions src/snapshots/rust_norg__tests__modifiers.snap.new

This file was deleted.

3 changes: 3 additions & 0 deletions src/snapshots/rust_norg__tests__paragraphs.snap
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ expression: examples
Text: hello
- Token:
Special: ","
- Token: Whitespace
- Token:
Text: world
- Token:
Expand All @@ -46,6 +47,7 @@ expression: examples
- - Paragraph:
- Token:
Text: paragraph
- Token: Whitespace
- Token:
Text: here
- Paragraph:
Expand All @@ -54,6 +56,7 @@ expression: examples
- Token: Whitespace
- Token:
Text: paragraph
- Token: Whitespace
- Token:
Text: here
- Token:
Expand Down
10 changes: 6 additions & 4 deletions src/stage_2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ fn tokens_to_paragraph_segment(tokens: Vec<NorgToken>) -> ParagraphTokenList {
.into_iter()
.peekable()
.batching(|it| match it.next() {
Some(NorgToken::Whitespace(_)) => Some(ParagraphSegmentToken::Whitespace),
Some(NorgToken::SingleNewline) | Some(NorgToken::Whitespace(_)) => {
Some(ParagraphSegmentToken::Whitespace)
}
Some(NorgToken::Special(c)) => Some(ParagraphSegmentToken::Special(c)),
Some(NorgToken::Escape(c)) => Some(ParagraphSegmentToken::Escape(c)),
Some(NorgToken::Regular(c)) => {
Expand Down Expand Up @@ -401,9 +403,9 @@ pub fn stage_2() -> impl Parser<NorgToken, Vec<NorgBlock>, Error = chumsky::erro
NorgToken::Newlines(_) => {
NorgBlock::ParagraphSegmentEnd(tokens_to_paragraph_segment(content))
}
NorgToken::SingleNewline => {
NorgBlock::ParagraphSegment(tokens_to_paragraph_segment(content))
}
NorgToken::SingleNewline => NorgBlock::ParagraphSegment(
tokens_to_paragraph_segment(content.into_iter().chain(trailing).collect()),
),
_ => unreachable!(),
})
.labelled("paragraph_segment"),
Expand Down
15 changes: 13 additions & 2 deletions src/stage_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,10 @@ fn paragraph_rollup_candidates(
content,
})
} else {
Err(Simple::custom(span, "differing opening and closing modifiers found"))
Err(Simple::custom(
span,
"differing opening and closing modifiers found",
))
}
})
});
Expand Down Expand Up @@ -453,7 +456,15 @@ pub fn stage_3(
.chain(paragraph_segment_end.or_not()),
paragraph_segment_end,
))
.map(|tokens| NorgASTFlat::Paragraph(parse_paragraph(tokens).unwrap()));
.map(|mut tokens| {
// Trim trailing whitespace (both user-induced but also induced by us when
// converting single newlines to whitespace).
if let Some(ParagraphSegmentToken::Whitespace) = tokens.last() {
tokens.pop();
}

NorgASTFlat::Paragraph(parse_paragraph(tokens).unwrap())
});

let nestable_detached_modifier = select! {
NorgBlock::NestableDetachedModifier { modifier_type: '-', level, extension_section } => (NestableDetachedModifier::UnorderedList, level, extension_section),
Expand Down

0 comments on commit 6137299

Please sign in to comment.