Skip to content

Commit

Permalink
Merge pull request #7 from dylanowen/blockquotes
Browse files Browse the repository at this point in the history
Better support for blockquotes (#6)
  • Loading branch information
Sebastian Thiel committed Dec 2, 2019
2 parents 6f2382e + 58e5137 commit fee9004
Show file tree
Hide file tree
Showing 8 changed files with 220 additions and 34 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,7 +1,7 @@
[package]
name = "pulldown-cmark-to-cmark"
version = "1.2.3"
authors = ["Sebastian Thiel <byronimo@gmail.com>"]
authors = ["Sebastian Thiel <byronimo@gmail.com>", "Dylan Owen <dyltotheo@gmail.com>"]

description = "Convert pulldown-cmark Events back to the string they were parsed from"
license = "Apache-2.0"
Expand Down
26 changes: 20 additions & 6 deletions src/fmt.rs
Expand Up @@ -63,6 +63,7 @@ pub struct Options {
pub newlines_after_html: usize,
pub newlines_after_rule: usize,
pub newlines_after_list: usize,
pub newlines_after_blockquote: usize,
pub newlines_after_rest: usize,
}

Expand All @@ -76,6 +77,7 @@ impl Default for Options {
newlines_after_html: 1,
newlines_after_rule: 2,
newlines_after_list: 2,
newlines_after_blockquote: 2,
newlines_after_rest: 1,
}
}
Expand Down Expand Up @@ -172,7 +174,6 @@ where
.and_then(|_| formatter.write_char('`')),
Start(ref tag) => {
match *tag {
BlockQuote => state.padding.push(" > ".into()),
List(ref list_type) => {
state.list_stack.push(list_type.clone());
if state.list_stack.len() > 1 {
Expand All @@ -183,7 +184,7 @@ where
}
_ => {}
}
let left_on_padded_newlines = state.newlines_before_start != 0;
let consumed_newlines = state.newlines_before_start != 0;
consume_newlines(&mut formatter, &mut state)?;
match *tag {
Item => match state.list_stack.last() {
Expand Down Expand Up @@ -220,10 +221,18 @@ where
formatter.write_char(' ')
}
BlockQuote => {
if !left_on_padded_newlines {
padding(&mut formatter, &state.padding)
} else {
Ok(())
state.padding.push(" > ".into());
state.newlines_before_start = 1;

// if we consumed some newlines, we know that we can just write out the next
// level in our blockquote. This should work regardless if we have other
// padding or if we're in a list
if consumed_newlines {
formatter.write_str(" > ")
}
else {
formatter.write_char('\n')
.and(padding(&mut formatter, &state.padding))
}
}
CodeBlock(ref info) => formatter
Expand Down Expand Up @@ -344,6 +353,11 @@ where
}
BlockQuote => {
state.padding.pop();

if state.newlines_before_start < options.newlines_after_blockquote {
state.newlines_before_start = options.newlines_after_blockquote;
}

Ok(())
}
FootnoteDefinition(_) => Ok(()),
Expand Down
2 changes: 1 addition & 1 deletion tests/display.rs
Expand Up @@ -44,7 +44,7 @@ mod start {
}
#[test]
fn blockquote() {
assert_eq!(s(Start(BlockQuote)), " > ")
assert_eq!(s(Start(BlockQuote)), "\n > ")
}
#[test]
fn codeblock() {
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/snapshots/stupicat-ordered-output
Expand Up @@ -12,6 +12,7 @@ Ordered lists:
1. With

Paragraphs and nested blocks:

>
> A quote

Expand Down
2 changes: 2 additions & 0 deletions tests/fixtures/snapshots/stupicat-output
Expand Up @@ -48,6 +48,7 @@ Unordered lists:
* With

Paragraphs and nested blocks:

>
> A quote

Expand Down Expand Up @@ -93,6 +94,7 @@ And a mix of both:
## Block level elements

Block quotes

>
> Lorem ipsum dolor sit amet, *consetetur sadipscing elitr*, sed diam nonumy
> eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/snapshots/stupicat-table-output
Expand Up @@ -9,6 +9,7 @@ Colons can be used to align columns.
There must be at least 3 dashes separating each header cell.
The outer pipes (|) are optional, and you don't need to make the
raw Markdown line up prettily. You can also use inline Markdown.

>
> |Markdown|Less|Pretty|
> |--------|----|------|
Expand Down
1 change: 1 addition & 0 deletions tests/fixtures/snapshots/stupicat-unordered-output
Expand Up @@ -10,6 +10,7 @@ Unordered lists:
* With

Paragraphs and nested blocks:

>
> A quote

Expand Down

0 comments on commit fee9004

Please sign in to comment.