Skip to content

Commit

Permalink
Inline verbatim (#15)
Browse files Browse the repository at this point in the history
* add inline verbatim

* tests: add markup inside verbatim

* rename snapshot file
  • Loading branch information
awegsche authored Nov 9, 2024
1 parent 21f83af commit 055d113
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -706,4 +706,21 @@ mod tests {

assert_yaml_snapshot!(examples);
}

#[test]
fn inline_verbatim() {
let examples: Vec<_> = [
"some text `inline verbatim`",
"`verbatim at start`",
"{/ some_link.txt}[with `inline verbatim` in anchor]",
"`*markup* /inside/ /-verbatim-/`",
]
.into_iter()
.map(|example| example.to_string() + "\n")
.map(|str| parse(&str))
.try_collect()
.unwrap();

assert_yaml_snapshot!(examples);
}
}
57 changes: 57 additions & 0 deletions src/snapshots/rust_norg__tests__inline_verbatim.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
source: src/lib.rs
assertion_line: 588
expression: examples
---
- - Paragraph:
- Token:
Text: some
- Token: Whitespace
- Token:
Text: text
- Token: Whitespace
- InlineVerbatim:
- Text: inline
- Whitespace
- Text: verbatim
- - Paragraph:
- InlineVerbatim:
- Text: verbatim
- Whitespace
- Text: at
- Whitespace
- Text: start
- - Paragraph:
- Link:
filepath: ~
targets:
- Path: some_link.txt
description:
- Token:
Text: with
- Token: Whitespace
- InlineVerbatim:
- Text: inline
- Whitespace
- Text: verbatim
- Token: Whitespace
- Token:
Text: in
- Token: Whitespace
- Token:
Text: anchor
- - Paragraph:
- InlineVerbatim:
- Special: "*"
- Text: markup
- Special: "*"
- Whitespace
- Special: /
- Text: inside
- Special: /
- Whitespace
- Special: /
- Special: "-"
- Text: verbatim
- Special: "-"
- Special: /
2 changes: 1 addition & 1 deletion src/stage_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ impl From<NorgToken> for String {
}

/// A list of characters which are considered "special", i.e. for parsing of attached modifiers.
const SPECIAL_CHARS: &str = "*-~/_!%^,\"'$:@|=.#+<>()[]{}\\";
const SPECIAL_CHARS: &str = "*-~/_!%^,\"'`$:@|=.#+<>()[]{}\\";

/// Parses a `.norg` document and breaks it up into tokens.
pub fn stage_1() -> impl Parser<char, Vec<NorgToken>, Error = chumsky::error::Simple<char>> {
Expand Down
12 changes: 12 additions & 0 deletions src/stage_3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ fn paragraph_parser_opener_candidates_and_links() -> impl Parser<
ParagraphSegment::AttachedModifierOpener((None, modifiers, right))
});

let inline_verbatim = just(ParagraphSegmentToken::Special('`'))
.ignore_then(
just(ParagraphSegmentToken::Special('`'))
.not()
.repeated()
.at_least(1),
)
.then_ignore(just(ParagraphSegmentToken::Special('`')))
.map(|content| ParagraphSegment::InlineVerbatim(content));

let anchor = just(ParagraphSegmentToken::Special('['))
.ignore_then(
just(ParagraphSegmentToken::Special(']'))
Expand Down Expand Up @@ -230,6 +240,7 @@ fn paragraph_parser_opener_candidates_and_links() -> impl Parser<
content: parse_paragraph(content).unwrap(),
target: Box::new(link),
}),
inline_verbatim,
anchor
.clone()
.then(anchor.clone().or_not())
Expand Down Expand Up @@ -472,6 +483,7 @@ pub enum ParagraphSegment {
description: Option<Vec<ParagraphSegment>>,
},
InlineLinkTarget(Vec<ParagraphSegment>),
InlineVerbatim(Vec<ParagraphSegmentToken>),
}

fn parse_paragraph(
Expand Down

0 comments on commit 055d113

Please sign in to comment.