From aedf9c79991482becbdb4b005fac9ca50cb53388 Mon Sep 17 00:00:00 2001 From: Philipp A Date: Sat, 9 Nov 2019 16:43:59 +0100 Subject: Implement replace --- src/parser/conversion/block.rs | 13 ++++++++++--- src/parser/tests.rs | 27 ++++++++++++++++++++++++++- 2 files changed, 36 insertions(+), 4 deletions(-) (limited to 'src/parser') diff --git a/src/parser/conversion/block.rs b/src/parser/conversion/block.rs index 0de86b5..0ea99f4 100644 --- a/src/parser/conversion/block.rs +++ b/src/parser/conversion/block.rs @@ -94,15 +94,22 @@ fn convert_substitution_def(pair: Pair) -> Result convert_image::(inner_pair)?.into(), + let inner: Vec = match inner_pair.as_rule() { + Rule::replace => convert_replace(inner_pair)?, + Rule::image => vec![convert_image::(inner_pair)?.into()], rule => panic!("Unknown substitution rule {:?}", rule), }; - let mut subst_def = e::SubstitutionDefinition::with_children(vec![inner]); + let mut subst_def = e::SubstitutionDefinition::with_children(inner); subst_def.names_mut().push(at::NameToken(name)); Ok(subst_def) } +fn convert_replace(pair: Pair) -> Result, Error> { + let mut pairs = pair.into_inner(); + let line = pairs.next().unwrap(); + line.into_inner().map(convert_inline).collect() +} + fn convert_image(pair: Pair) -> Result where I: Element + ExtraAttributes { let mut pairs = pair.into_inner(); let mut image = I::with_extra(a::Image::new( diff --git a/src/parser/tests.rs b/src/parser/tests.rs index c498159..ee33c8a 100644 --- a/src/parser/tests.rs +++ b/src/parser/tests.rs @@ -108,7 +108,32 @@ fn admonitions() { }; } -// TODO: test substitutions + +#[allow(clippy::cognitive_complexity)] +#[test] +fn substitutions() { + parses_to! { + parser: RstParser, + input: "\ +A |subst| in-line + +.. |subst| replace:: substitution +", + rule: Rule::document, + tokens: [ + paragraph(0, 17, [ + str(0, 2), + substitution_ref(2, 9, [ substitution_name(3, 8) ]), + str(9, 17), + ]), + substitution_def(19, 53, [ + substitution_name(23, 28), + replace(30, 53, [ line(39, 53, [str(39, 52)]) ]) + ]), + ] + }; +} + // TODO: test images #[allow(clippy::cognitive_complexity)] -- cgit v1.2.3