Skip to content

Commit

Permalink
fix code fences not correctly interpreted in text templates
Browse files Browse the repository at this point in the history
Fix #1
  • Loading branch information
Canop committed Dec 21, 2019
1 parent 1214d2c commit 19d8f9f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minimad"
version = "0.6.0"
version = "0.6.1"
authors = ["dystroy <denys.seguret@gmail.com>"]
repository = "https://github.com/Canop/minimad"
description = "light Markdown parser"
Expand Down
2 changes: 1 addition & 1 deletion src/line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ impl<'s> LineParser<'s> {
}
/// should be called when the line must be interpreted as a code part,
/// for example between code fences
pub fn as_code(mut self) -> Line<'s> {
pub fn as_code(self) -> Line<'s> {
if self.src == "```" {
Line::CodeFence
} else {
Expand Down
21 changes: 18 additions & 3 deletions src/text_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ impl<'s> From<&'s str> for TextTemplate<'s> {
let mut compound_args = Vec::new();
let mut sub_templates = Vec::new();
let mut current_sub_template: Option<SubTemplate<'_>> = None;
let mut between_fences = false;
for md_line in clean::lines(md) {
match read_sub_template_token(md_line) {
SubTemplateToken::Start(name) => {
Expand All @@ -174,20 +175,31 @@ impl<'s> From<&'s str> for TextTemplate<'s> {
}
SubTemplateToken::None => { }
}
let mut line = LineParser::from(md_line).line();
let line_idx = text.lines.len();
let parser = LineParser::from(md_line);
let mut line = if between_fences {
parser.as_code()
} else {
parser.line()
};
match &mut line {
Line::Normal(ref mut composite) => {
find_args(composite, &mut compound_args, line_idx, 0);
text.lines.push(line);
}
Line::TableRow(ref mut table_row) => {
for (composite_idx, composite) in table_row.cells.iter_mut().enumerate() {
find_args(composite, &mut compound_args, line_idx, composite_idx);
}
text.lines.push(line);
}
Line::CodeFence => {
between_fences = !between_fences;
}
_ => {},
_ => {
text.lines.push(line);
},
};
text.lines.push(line);
}
TextTemplate{
text,
Expand Down Expand Up @@ -379,6 +391,8 @@ impl<'s, 'b> TextTemplateExpander<'s, 'b> {
self
}

/// replace a placeholder with several lines.
/// This is mostly useful when the placeholder is a repeatable line (code, list item)
pub fn set_lines(&mut self, name: &'b str, raw_lines: &'s str) -> &mut TextTemplateExpander<'s, 'b> {
for compound_arg in &self.template.compound_args {
if compound_arg.name == name {
Expand All @@ -398,6 +412,7 @@ impl<'s, 'b> TextTemplateExpander<'s, 'b> {
self
}

/// replace a placeholder with several lines interpreted as markdown
pub fn set_lines_md(&mut self, name: &'b str, md: &'s str) -> &mut TextTemplateExpander<'s, 'b> {
for compound_arg in &self.template.compound_args {
if compound_arg.name == name {
Expand Down

0 comments on commit 19d8f9f

Please sign in to comment.