diff options
| author | Konsta Hölttä | 2020-09-07 01:30:34 +0300 |
|---|---|---|
| committer | GitHub | 2020-09-07 00:30:34 +0200 |
| commit | 4c487a44208a96e3a23ab8974d224cae489688be (patch) | |
| tree | 225496197ba8d5c096497809f53bf9e31ba8231b /parser | |
| parent | c80468a8f917079189c8cd111556f9752085e3e4 (diff) | |
| download | rust-rst-4c487a44208a96e3a23ab8974d224cae489688be.tar.bz2 | |
Add support for raw directives (#17)
All content under a raw directive is rendered as-is if the output format
matches the writer. Otherwise none of it is shown.
Diffstat (limited to 'parser')
| -rw-r--r-- | parser/src/conversion/block.rs | 15 | ||||
| -rw-r--r-- | parser/src/rst.pest | 12 | ||||
| -rw-r--r-- | parser/src/tests.rs | 39 |
3 files changed, 66 insertions, 0 deletions
diff --git a/parser/src/conversion/block.rs b/parser/src/conversion/block.rs index 64aa33e..9498b32 100644 --- a/parser/src/conversion/block.rs +++ b/parser/src/conversion/block.rs @@ -54,6 +54,7 @@ fn convert_body_elem(pair: Pair<Rule>) -> Result<c::BodyElement, Error> { Rule::image => convert_image::<e::Image>(pair)?.into(), Rule::bullet_list => convert_bullet_list(pair)?.into(), Rule::code_directive => convert_code_directive(pair)?.into(), + Rule::raw_directive => convert_raw_directive(pair)?.into(), rule => unimplemented!("unhandled rule {:?}", rule), }) } @@ -219,3 +220,17 @@ fn convert_code_directive(pair: Pair<Rule>) -> Result<e::LiteralBlock, Error> { }; Ok(code_block) } + +fn convert_raw_directive(pair: Pair<Rule>) -> Result<e::Raw, Error> { + let mut iter = pair.into_inner(); + let format = iter.next().unwrap(); + let block = iter.next().unwrap(); + let children = block.into_inner().map(|l| match l.as_rule() { + Rule::raw_line => l.as_str(), + Rule::raw_line_blank => "\n", + _ => unreachable!(), + }.into()).collect(); + let mut raw_block = e::Raw::with_children(children); + raw_block.extra_mut().format.push(at::NameToken(format.as_str().to_owned())); + Ok(raw_block) +} diff --git a/parser/src/rst.pest b/parser/src/rst.pest index 322ad08..8885109 100644 --- a/parser/src/rst.pest +++ b/parser/src/rst.pest @@ -16,6 +16,7 @@ hanging_block = _{ substitution_def | image_directive | code_directive + | raw_directive | admonition | admonition_gen | target @@ -91,6 +92,17 @@ code_block = { code_line ~ (code_line_blank* ~ PEEK[..] ~ code_line)* } code_line_blank = { " "* ~ NEWLINE } code_line = { (!NEWLINE ~ ANY)+ ~ NEWLINE } +// Raw block. A directive + +raw_directive = { + ".." ~ PUSH(" "+) ~ "raw::" ~ " "+ ~ raw_output_format ~ NEWLINE ~ + blank_line+ ~ PEEK[..-1] ~ PUSH(" " ~ POP) ~ raw_block ~ DROP +} +raw_output_format = { (!NEWLINE ~ ANY)+ } +raw_block = { raw_line ~ (raw_line_blank* ~ PEEK[..] ~ raw_line)* } +raw_line_blank = { " "* ~ NEWLINE } +raw_line = { (!NEWLINE ~ ANY)+ ~ NEWLINE } + // Admonition. A directive. The generic one has a title admonition = { ".." ~ PUSH(" "+) ~ ^"admonition::" ~ line ~ blank_line* ~ admonition_content? ~ DROP } diff --git a/parser/src/tests.rs b/parser/src/tests.rs index 504d13b..93e20ec 100644 --- a/parser/src/tests.rs +++ b/parser/src/tests.rs @@ -186,6 +186,45 @@ The end #[allow(clippy::cognitive_complexity)] #[test] +fn raw() { + parses_to! { + parser: RstParser, + input: "\ +.. raw:: html + + hello <span>world</span> + +.. raw:: html + + hello <pre>world + + parse</pre> this + +The end +", + rule: Rule::document, + tokens: [ + raw_directive(0, 43, [ + raw_output_format(9, 13), + raw_block(18, 43, [ + raw_line(18, 43), + ]), + ]), + raw_directive(44, 100, [ + raw_output_format(53, 57), + raw_block(62, 100, [ + raw_line(62, 79), + raw_line_blank(79, 80), + raw_line(83, 100), + ]), + ]), + paragraph(101, 108, [ str(101, 108) ]), + ] + }; +} + +#[allow(clippy::cognitive_complexity)] +#[test] fn substitutions() { parses_to! { parser: RstParser, |
