aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp A2019-11-09 16:43:59 +0100
committerPhilipp A2019-11-09 16:43:59 +0100
commitaedf9c79991482becbdb4b005fac9ca50cb53388 (patch)
treea11f9504a42deeb8a8e330e8d60f2aaf7264666a
parent89d1ed48c5f37d4c4e0e81bd85d0658093299b34 (diff)
downloadrust-rst-aedf9c79991482becbdb4b005fac9ca50cb53388.tar.bz2
Implement replace
-rw-r--r--src/parser/conversion/block.rs13
-rw-r--r--src/parser/tests.rs27
-rw-r--r--src/rst.pest8
3 files changed, 42 insertions, 6 deletions
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<Rule>) -> Result<e::SubstitutionDefinitio
let mut pairs = pair.into_inner();
let name = whitespace_normalize_name(pairs.next().unwrap().as_str()); // Rule::substitution_name
let inner_pair = pairs.next().unwrap();
- let inner: c::TextOrInlineElement = match inner_pair.as_rule() {
- Rule::image => convert_image::<e::ImageInline>(inner_pair)?.into(),
+ let inner: Vec<c::TextOrInlineElement> = match inner_pair.as_rule() {
+ Rule::replace => convert_replace(inner_pair)?,
+ Rule::image => vec![convert_image::<e::ImageInline>(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<Rule>) -> Result<Vec<c::TextOrInlineElement>, Error> {
+ let mut pairs = pair.into_inner();
+ let line = pairs.next().unwrap();
+ line.into_inner().map(convert_inline).collect()
+}
+
fn convert_image<I>(pair: Pair<Rule>) -> Result<I, Error> where I: Element + ExtraAttributes<a::Image> {
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)]
diff --git a/src/rst.pest b/src/rst.pest
index 1856a78..96652a2 100644
--- a/src/rst.pest
+++ b/src/rst.pest
@@ -41,7 +41,7 @@ hanging_block = _{
// Substitution definition. A block type
substitution_def = { ".." ~ PUSH(" "+) ~ "|" ~ substitution_name ~ "|" ~ " "+ ~ inline_dirblock ~ DROP }
substitution_name = { !" " ~ (!(" "|"|") ~ ANY)+ ~ (" "+ ~ (!(" "|"|") ~ ANY)+)* }
-inline_dirblock = _{ image } // TODO: implement “replace” and so on
+inline_dirblock = _{ replace | image } // TODO: implement others
// Target. A block type
target = { target_qu | target_uq }
@@ -74,7 +74,11 @@ paragraph = { inlines }
// Directives with options can have these or specific ones:
common_option = { "class" | "name" }
-// Image. A directive
+// Replace. A directive only usable in substitutions.
+
+replace = { ^"replace::" ~ line } // TODO multiline
+
+// Image. A directive.
image_directive = _{ ".." ~ PUSH(" "+) ~ image ~ DROP }
image = { ^"image::" ~ line ~ image_opt_block? }