Skip to content

Commit

Permalink
Merge pull request #3 from integer32llc/pulldown-cmark-0.4
Browse files Browse the repository at this point in the history
Pulldown cmark 0.4
  • Loading branch information
Byron authored Jul 3, 2019
2 parents 108c355 + 90f134e commit a92f729
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 48 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ documentation = "https://docs.rs/crate/pulldown-cmark-to-cmark"
readme = "README.md"

[dependencies]
pulldown-cmark = {version = "0.2.0", default-features = false}
pulldown-cmark = {version = "0.4.1", default-features = false}

[dev-dependencies]
indoc = "0.3.1"
33 changes: 20 additions & 13 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ where
TableHead => Ok(()),
TableRow => Ok(()),
TableCell => formatter.write_char('|'),
Link(_, _) => formatter.write_char('['),
Image(_, _) => formatter.write_str("!["),
Link(..) => formatter.write_char('['),
Image(..) => formatter.write_str("!["),
Emphasis => formatter.write_char('*'),
Strong => formatter.write_str("**"),
Code => formatter.write_char('`'),
Expand All @@ -220,10 +220,12 @@ where
.and(formatter.write_char('\n'))
.and(padding(&mut formatter, &state.padding)),
List(_) => Ok(()),
HtmlBlock => Ok(()),
Strikethrough => formatter.write_str("~~"),
}
}
End(ref tag) => match *tag {
Image(ref uri, ref title) | Link(ref uri, ref title) => {
Image(_, ref uri, ref title) | Link(_, ref uri, ref title) => {
if title.is_empty() {
write!(formatter, "]({})", uri)
} else {
Expand Down Expand Up @@ -322,6 +324,15 @@ where
Ok(())
}
FootnoteDefinition(_) => Ok(()),
HtmlBlock => {
consume_newlines(&mut formatter, &mut state)?;

if state.newlines_before_start < options.newlines_after_html {
state.newlines_before_start = options.newlines_after_html;
}
Ok(())
},
Strikethrough => formatter.write_str("~~"),
},
HardBreak => formatter
.write_str(" \n")
Expand All @@ -331,22 +342,18 @@ where
.and(padding(&mut formatter, &state.padding)),
Text(ref text) => {
if state.table_alignments.len() != state.table_headers.len() {
state.table_headers.push(text.clone().into());
state.table_headers.push(text.to_owned().into_string());
}
consume_newlines(&mut formatter, &mut state)?;
print_text(text, &mut formatter, &state.padding)
}
Html(ref text) => {
consume_newlines(&mut formatter, &mut state)?;
print_text(text, &mut formatter, &state.padding)?;

if state.newlines_before_start < options.newlines_after_html {
state.newlines_before_start = options.newlines_after_html;
}
Ok(())
}
Html(ref text) => print_text(text, &mut formatter, &state.padding),
InlineHtml(ref name) => formatter.write_str(name),
FootnoteReference(ref name) => write!(formatter, "[^{}]", name),
TaskListMarker(checked) => {
let check = if checked { "x" } else { " " };
write!(formatter, "[{}] ", check)
}
}?
}
Ok(state)
Expand Down
18 changes: 10 additions & 8 deletions tests/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn s(e: Event) -> String {

mod start {
use pulldown_cmark::Event::*;
use pulldown_cmark::LinkType::*;
use pulldown_cmark::Tag::*;
use pulldown_cmark::Alignment::{self, Center, Left, Right};
use super::s;
Expand Down Expand Up @@ -70,19 +71,19 @@ mod start {
}
#[test]
fn link() {
assert_eq!(s(Start(Link("uri".into(), "title".into()))), "[")
assert_eq!(s(Start(Link(Inline, "uri".into(), "title".into()))), "[")
}
#[test]
fn link_without_title() {
assert_eq!(s(Start(Link("uri".into(), "".into()))), "[")
assert_eq!(s(Start(Link(Inline, "uri".into(), "".into()))), "[")
}
#[test]
fn image() {
assert_eq!(s(Start(Image("uri".into(), "title".into()))), "![")
assert_eq!(s(Start(Image(Inline, "uri".into(), "title".into()))), "![")
}
#[test]
fn image_without_title() {
assert_eq!(s(Start(Image("uri".into(), "".into()))), "![")
assert_eq!(s(Start(Image(Inline, "uri".into(), "".into()))), "![")
}
#[test]
fn table() {
Expand All @@ -107,6 +108,7 @@ mod start {

mod end {
use pulldown_cmark::Event::*;
use pulldown_cmark::LinkType::*;
use pulldown_cmark::Tag::*;
use pulldown_cmark::Alignment::{self, Center, Left, Right};
use super::s;
Expand Down Expand Up @@ -162,24 +164,24 @@ mod end {
#[test]
fn link() {
assert_eq!(
s(End(Link("/uri".into(), "title".into()))),
s(End(Link(Inline, "/uri".into(), "title".into()))),
"](/uri \"title\")"
)
}
#[test]
fn link_without_title() {
assert_eq!(s(End(Link("/uri".into(), "".into()))), "](/uri)")
assert_eq!(s(End(Link(Inline, "/uri".into(), "".into()))), "](/uri)")
}
#[test]
fn image() {
assert_eq!(
s(End(Image("/uri".into(), "title".into()))),
s(End(Image(Inline, "/uri".into(), "title".into()))),
"](/uri \"title\")"
)
}
#[test]
fn image_without_title() {
assert_eq!(s(End(Image("/uri".into(), "".into()))), "](/uri)")
assert_eq!(s(End(Image(Inline, "/uri".into(), "".into()))), "](/uri)")
}
#[test]
fn table() {
Expand Down
36 changes: 18 additions & 18 deletions tests/fixtures/snapshots/stupicat-table-output
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
Colons can be used to align columns.

| Tables | Are | Cool | yo |
|---------------|:-------------:|------:|:---|
| col 3 is | right-aligned | $1600 | x |
| col 2 is | centered | $12 | y |
| zebra stripes | are neat | $1 | z |
|Tables |Are |Cool |yo |
|--------------|:------------:|-----:|:--|
|col 3 is |right-aligned |$1600 |x |
|col 2 is |centered |$12 |y |
|zebra stripes |are neat |$1 |z |

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|
> |---------|------|-------|
> |*Still* | `renders` | **nicely**|
> |1 | 2 | 3|
> |Markdown |Less |Pretty|
> |---------|-----|------|
> |*Still* |`renders` |**nicely**|
> |1 |2 |3|

| Target | std |rustc|cargo| notes |
|-------------------------------|-----|-----|-----|----------------------------|
| `x86_64-unknown-linux-musl` | ✓ | | | 64-bit Linux with MUSL |
| `arm-linux-androideabi` | ✓ | | | ARM Android |
| `arm-unknown-linux-gnueabi` | ✓ | ✓ | | ARM Linux (2.6.18+) |
| `arm-unknown-linux-gnueabihf` | ✓ | ✓ | | ARM Linux (2.6.18+) |
| `aarch64-unknown-linux-gnu` | ✓ | | | ARM64 Linux (2.6.18+) |
| `mips-unknown-linux-gnu` | ✓ | | | MIPS Linux (2.6.18+) |
| `mipsel-unknown-linux-gnu` | ✓ | | | MIPS (LE) Linux (2.6.18+) |
|Target |std |rustc|cargo|notes |
|-----------------------------|----|-----|-----|---------------------------|
|`x86_64-unknown-linux-musl` |✓ |||64-bit Linux with MUSL |
|`arm-linux-androideabi` |✓ |||ARM Android |
|`arm-unknown-linux-gnueabi` |✓ |✓ ||ARM Linux (2.6.18+) |
|`arm-unknown-linux-gnueabihf` |✓ |✓ ||ARM Linux (2.6.18+) |
|`aarch64-unknown-linux-gnu` |✓ |||ARM64 Linux (2.6.18+) |
|`mips-unknown-linux-gnu` |✓ |||MIPS Linux (2.6.18+) |
|`mipsel-unknown-linux-gnu` |✓ |||MIPS (LE) Linux (2.6.18+) |
6 changes: 3 additions & 3 deletions tests/fixtures/table.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ Colons can be used to align columns.
|---------------|:-------------:|------:|:---|
| col 3 is | right-aligned | $1600 | x |
| col 2 is | centered | $12 | y |
| zebra stripes | are neat | $1 | z |
| zebra stripes | are neat | $1 | z |

There must be at least 3 dashes separating each header cell.
The outer pipes (|) are optional, and you don't need to make the
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
> --- | --- | ---
> *Still* | `renders` | **nicely**
> 1 | 2 | 3
| Target | std |rustc|cargo| notes |
|-------------------------------|-----|-----|-----|----------------------------|
| `x86_64-unknown-linux-musl` || | | 64-bit Linux with MUSL |
Expand Down
65 changes: 60 additions & 5 deletions tests/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ extern crate pulldown_cmark;
extern crate pulldown_cmark_to_cmark;

use pulldown_cmark_to_cmark::fmt::{cmark, State};
use pulldown_cmark::{Alignment, Event, Options, Parser, Tag};
use pulldown_cmark::{Alignment, Event, LinkType, Options, Parser, Tag};

fn fmts(s: &str) -> (String, State<'static>) {
let mut buf = String::new();
Expand All @@ -26,7 +26,7 @@ fn fmte(e: &[Event]) -> (String, State<'static>) {

mod lazy_newlines {
use super::{fmte, fmts};
use super::{Event, State, Tag};
use super::{Event, LinkType, State, Tag};

#[test]
fn after_emphasis_there_is_no_newline() {
Expand All @@ -35,8 +35,8 @@ mod lazy_newlines {
Tag::Strong,
Tag::Code,
Tag::BlockQuote,
Tag::Link("".into(), "".into()),
Tag::Image("".into(), "".into()),
Tag::Link(LinkType::Inline, "".into(), "".into()),
Tag::Image(LinkType::Inline, "".into(), "".into()),
Tag::FootnoteDefinition("".into()),
] {
assert_eq!(
Expand Down Expand Up @@ -107,7 +107,11 @@ fn it_applies_newlines_before_start_before_text() {
fn it_applies_newlines_before_start_before_html_and_enforces_newline_after() {
assert_eq!(
fmtes(
&[Event::Html("<e>".into())],
&[
Event::Start(Tag::HtmlBlock),
Event::Html("<e>".into()),
Event::End(Tag::HtmlBlock),
],
State {
newlines_before_start: 2,
..Default::default()
Expand Down Expand Up @@ -230,6 +234,14 @@ mod inline_elements {
)
)
}

#[test]
fn strikethrough() {
assert_eq!(
fmts("~~strikethrough~~").0,
"~~strikethrough~~",
);
}
}

mod blockquote {
Expand Down Expand Up @@ -412,6 +424,35 @@ mod table {
}
)
}
#[test]
fn it_generates_equivalent_table_markdown() {
use pulldown_cmark::{Options, Parser};

let original_table_markdown = indoc!(
"
| Tables | Are | Cool | yo |
|---------------|:-------------:|------:|:---|
| col 3 is | right-aligned | $1600 | x |
| col 2 is | centered | $12 | y |
| zebra stripes | are neat | $1 | z |"
);
let p = Parser::new_ext(original_table_markdown, Options::all());
let original_events: Vec<_> = p.into_iter().collect();

let (generated_markdown, _) = fmte(&original_events);

assert_eq!(generated_markdown, indoc!("
|Tables |Are |Cool |yo |
|--------------|:------------:|-----:|:--|
|col 3 is |right-aligned |$1600 |x |
|col 2 is |centered |$12 |y |
|zebra stripes |are neat |$1 |z |"));

let p = Parser::new_ext(&generated_markdown, Options::all());
let generated_events: Vec<_> = p.into_iter().collect();

assert_eq!(original_events, generated_events);
}
}

mod list {
Expand Down Expand Up @@ -508,4 +549,18 @@ mod list {
)
)
}

#[test]
fn checkboxes() {
assert_eq!(
fmts(indoc!(
"
* [ ] foo
* [x] bar
"
)).0,
"* [ ] foo\n* [x] bar",
);

}
}

0 comments on commit a92f729

Please sign in to comment.