aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPhilipp A2019-12-08 16:55:21 +0100
committerPhilipp A2019-12-08 17:13:15 +0100
commit13ecb9436b5987237062a79c21b1630689d4715d (patch)
treec410ed45edfff10c5d09e9c38e1fd90f1ef9f7e9
parent588f2474c4f13417c9dae18defc657fcb6c0f07e (diff)
downloadrust-rst-13ecb9436b5987237062a79c21b1630689d4715d.tar.bz2
Render emph, strong, and literal
-rw-r--r--src/parser/conversion/inline.rs8
-rw-r--r--src/parser/tests.rs14
-rw-r--r--src/renderer/html_tests.rs4
-rw-r--r--src/rst.pest15
4 files changed, 31 insertions, 10 deletions
diff --git a/src/parser/conversion/inline.rs b/src/parser/conversion/inline.rs
index ed118c3..297680a 100644
--- a/src/parser/conversion/inline.rs
+++ b/src/parser/conversion/inline.rs
@@ -5,7 +5,8 @@ use crate::document_tree::{
elements as e,
element_categories as c,
extra_attributes as a,
- attribute_types as at
+ attribute_types as at,
+ element_categories::HasChildren,
};
use crate::parser::{
@@ -19,9 +20,12 @@ use super::whitespace_normalize_name;
pub fn convert_inline(pair: Pair<Rule>) -> Result<c::TextOrInlineElement, Error> {
Ok(match pair.as_rule() {
- Rule::str => pair.as_str().into(),
+ Rule::str | Rule::str_nested => pair.as_str().into(),
Rule::reference => convert_reference(pair)?,
Rule::substitution_ref => convert_substitution_ref(pair)?.into(),
+ Rule::emph => e::Emphasis::with_children(convert_inlines(pair)?).into(),
+ Rule::strong => e::Strong::with_children(convert_inlines(pair)?).into(),
+ Rule::literal => e::Literal::with_children(convert_inlines(pair)?).into(),
rule => unimplemented!("unknown rule {:?}", rule),
})
}
diff --git a/src/parser/tests.rs b/src/parser/tests.rs
index bf9fe22..e161108 100644
--- a/src/parser/tests.rs
+++ b/src/parser/tests.rs
@@ -17,6 +17,18 @@ fn plain() {
}
#[test]
+fn emph_only() {
+ parses_to! {
+ parser: RstParser,
+ input: "*emphasis*",
+ rule: Rule::emph_outer,
+ tokens: [
+ emph(1, 9, [str_nested(1, 9)])
+ ]
+ };
+}
+
+#[test]
fn emph() {
parses_to! {
parser: RstParser,
@@ -25,7 +37,7 @@ fn emph() {
tokens: [
paragraph(0, 18, [
str(0, 5),
- emph(6, 17),
+ emph(6, 17, [str_nested(6, 17)]),
])
]
};
diff --git a/src/renderer/html_tests.rs b/src/renderer/html_tests.rs
index c12890a..32871dc 100644
--- a/src/renderer/html_tests.rs
+++ b/src/renderer/html_tests.rs
@@ -23,8 +23,8 @@ fn test_simple_string() {
#[test]
fn test_simple_string_with_markup() {
check_renders_to(
- "Simple String with *markup*",
- "<p>Simple String with <em>markup</em></p>",
+ "Simple String with *emph* and **strong**",
+ "<p>Simple String with <em>emph</em> and <strong>strong</strong></p>",
);
}
diff --git a/src/rst.pest b/src/rst.pest
index 34e7bd2..1034d03 100644
--- a/src/rst.pest
+++ b/src/rst.pest
@@ -110,7 +110,7 @@ inline_special = _{
| substitution_ref
| emph_outer
| strong_outer
- | literal_outer
+ | literal_outer
// | ul_or_star_line
// | space
// | note_reference
@@ -126,13 +126,18 @@ inline_special = _{
str = { (!(NEWLINE | inline_special) ~ ANY)+ }
-// simple formatting. TODO: similar to substitution_name, make function or so?
+// simple formatting
+inline_nested = _{ inline_special | str_nested }
+str_nested = { word_nested ~ ( " "+ ~ word_nested)* }
+// TODO: allow ` in emph and * in literal
+word_nested = _{ (!(NEWLINE | " " | inline_special | "*" | "`") ~ ANY)+ }
+
emph_outer = _{ "*" ~ emph ~ "*" }
-emph = { (!("*"|" ") ~ ANY)+ ~ (" "+ ~ (!("*"|" ") ~ ANY)+)* }
+emph = { (!("*"|" ") ~ inline_nested)+ ~ (" "+ ~ (!("*"|" ") ~ inline_nested)+)* }
strong_outer = _{ "**" ~ strong ~ "**" }
-strong = { (!("*"|" ") ~ ANY)+ ~ (" "+ ~ (!("*"|" ") ~ ANY)+)* }
+strong = { (!("*"|" ") ~ inline_nested)+ ~ (" "+ ~ (!("*"|" ") ~ inline_nested)+)* }
literal_outer = _{ "``" ~ literal ~ "``" }
-literal = { (!("`"|" ") ~ ANY)+ ~ (" "+ ~ (!("`"|" ") ~ ANY)+)* }
+literal = { (!("`"|" ") ~ inline_nested)+ ~ (" "+ ~ (!("`"|" ") ~ inline_nested)+)* }
// inline links
reference = { reference_target | reference_explicit | reference_auto }