extern crate pulldown_cmark; extern crate cmark_hamlet; use std::fmt::Write; use pulldown_cmark::Parser; use pulldown_cmark::Options; use cmark_hamlet::Adapter; fn to_html(md: &str) -> String { let ada = Adapter::new(Parser::new_ext(md, Options::all()), false); let mut res = String::from(""); for token in ada { write!(res, "{}", token).unwrap(); } res } #[test] fn simple() { assert_eq!(to_html("foo bar"), "

foo bar

"); assert_eq!(to_html("foo\nbar"), "

foo\nbar

"); assert_eq!(to_html("foo\n\nbar"), "

foo

bar

"); assert_eq!(to_html("foo\n\nbar"), "

foo

bar

"); assert_eq!(to_html("foo\n- - -\nbar"), "

foo


bar

"); assert_eq!(to_html("`foo bar`"), "

foo bar

"); assert_eq!(to_html("**foo bar**"), "

foo bar

"); assert_eq!(to_html("_foo bar_"), "

foo bar

"); assert_eq!(to_html("> foo\n>\n> bar"), "

foo

bar

"); } #[test] fn code_block() { assert_eq!( to_html("```\n\ code\n\n\ block\n\ ```"), "
code\n\nblock\n
" ); assert_eq!( to_html("```sh\n\ code\n\n\ block\n\ ```"), "
code\n\nblock\n
" ); } #[test] fn image() { assert_eq!( to_html("![](/link/to/image)"), "

" ); assert_eq!( to_html("![foo](/link/to/image)"), "

\"foo\"

" ); assert_eq!( to_html("![](/link/to/image \"bar\")"), "

" ); assert_eq!( to_html("![foo](/link/to/image \"bar\")"), "

\"foo\"

" ); assert_eq!( to_html("![foo] bar\n\n[foo]: /link/to/image \"bar\""), "

\"foo\" bar

" ); } #[test] fn link() { assert_eq!( to_html(""), // absolute URI "

file:/link/to/x

" ); assert_eq!( to_html("[](/link/to/x)"), "

" ); assert_eq!( to_html("[foo](/link/to/x)"), "

foo

" ); assert_eq!( to_html("[](/link/to/x \"bar\")"), "

" ); assert_eq!( to_html("[foo](/link/to/x \"bar\")"), "

foo

" ); assert_eq!( to_html("[foo] bar\n\n[foo]: /link/to/x \"bar\""), "

foo bar

" ); } #[test] fn list() { assert_eq!( to_html("- hello\n\ - world"), "" ); assert_eq!( to_html("1. hello\n\ 2. world"), "
  1. hello
  2. world
" ); assert_eq!( to_html("3. hello\n\ 4. world"), "
  1. hello
  2. world
" ); assert_eq!( to_html("- hello\ \n ```\ \n let x;\ \n ```\ \n- world"), "" ); assert_eq!( to_html("- hello\n\ \n goo\ \n- world"), "" // NB: different from above!! ); } #[test] fn table() { assert_eq!( to_html("head1 | head2\n\ ----- | -----\n\ foo | bar "), "\
head1 head2
foo bar
" ); }