Skip to content

Commit

Permalink
allow language specification in code fence
Browse files Browse the repository at this point in the history
  • Loading branch information
Canop committed Dec 22, 2019
1 parent 19d8f9f commit 32cfa1f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 10 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.1"
version = "0.6.2"
authors = ["dystroy <denys.seguret@gmail.com>"]
repository = "https://github.com/Canop/minimad"
description = "light Markdown parser"
Expand Down
14 changes: 13 additions & 1 deletion src/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum Line<'a> {
TableRow(TableRow<'a>), // a normal table row, with cells having content
TableRule(TableRule), // a separator/border in a table, optionally defining alignments
HorizontalRule, // an horizontal line dividing the screen
CodeFence,
CodeFence(Composite<'a>),
}

impl Line<'_> {
Expand All @@ -33,6 +33,18 @@ impl Line<'_> {
compounds,
})
}
pub fn empty_code_fence() -> Line<'static> {
Line::CodeFence(Composite {
style: CompositeStyle::Paragraph,
compounds: vec![],
})
}
pub fn new_code_fence(compounds: Vec<Compound<'_>>) -> Line<'_> {
Line::CodeFence(Composite {
style: CompositeStyle::Paragraph,
compounds,
})
}
pub fn new_code(compound: Compound<'_>) -> Line<'_> {
Line::Normal(Composite {
style: CompositeStyle::Code,
Expand Down
20 changes: 14 additions & 6 deletions src/line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,9 +219,10 @@ 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(self) -> Line<'s> {
if self.src == "```" {
Line::CodeFence
pub fn as_code(mut self) -> Line<'s> {
if self.src.starts_with("```") {
self.idx = 3;
Line::new_code_fence(self.parse_compounds(false))
} else {
Line::new_code(self.code_compound_from_idx(0))
}
Expand Down Expand Up @@ -250,8 +251,9 @@ impl<'s> LineParser<'s> {
self.idx = 2;
return Line::new_quote(self.parse_compounds(false));
}
if self.src == "```" {
return Line::CodeFence;
if self.src.starts_with("```") {
self.idx = 3;
return Line::new_code_fence(self.parse_compounds(false));
}
let header_level = header_level(self.src);
if header_level > 0 {
Expand Down Expand Up @@ -401,7 +403,13 @@ mod tests {
fn code_fence() {
assert_eq!(
Line::from("```"),
Line::CodeFence,
Line::new_code_fence(vec![]),
);
assert_eq!(
Line::from("```rust"),
Line::new_code_fence(vec![
Compound::raw_str("rust"),
]),
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'s> From<&'s str> for Text<'s> {
parser.line()
};
match line {
Line::CodeFence => {
Line::CodeFence(..) => {
between_fences = !between_fences;
}
_ => {
Expand Down
2 changes: 1 addition & 1 deletion src/text_template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ impl<'s> From<&'s str> for TextTemplate<'s> {
}
text.lines.push(line);
}
Line::CodeFence => {
Line::CodeFence(..) => {
between_fences = !between_fences;
}
_ => {
Expand Down

0 comments on commit 32cfa1f

Please sign in to comment.