/src/lib.rs

https://gitlab.com/alx741/markdown.rs · Rust · 43 lines · 27 code · 11 blank · 5 comment · 0 complexity · 9208c6f7301500a7d334f8c345f75336 MD5 · raw file

  1. //! A crate for parsing Markdown in Rust
  2. #![crate_name = "markdown"]
  3. #![deny(missing_docs)]
  4. // #![deny(warnings)]
  5. #![cfg_attr(feature="clippy", feature(plugin))]
  6. #![cfg_attr(feature="clippy", plugin(clippy))]
  7. extern crate regex;
  8. #[macro_use]
  9. extern crate pipeline;
  10. use std::fs::File;
  11. use std::path::Path;
  12. use std::io::{self, Read};
  13. mod parser;
  14. mod html;
  15. use parser::Block;
  16. /// Converts a Markdown string to HTML
  17. pub fn to_html(text: &str) -> String {
  18. let result = parser::parse(text);
  19. html::to_html(&result)
  20. }
  21. /// Converts a Markdown string to a tokenset of Markdown items
  22. pub fn tokenize(text: &str) -> Vec<Block> {
  23. parser::parse(text)
  24. }
  25. /// Opens a file and converts its contents to HTML
  26. pub fn file_to_html(path: &Path) -> io::Result<String> {
  27. let mut file = try!(File::open(path));
  28. let mut text = String::new();
  29. try!(file.read_to_string(&mut text));
  30. let result = parser::parse(&text);
  31. Ok(html::to_html(&result))
  32. }