markdown/
lib.rs

1//! A crate for parsing Markdown in Rust
2#![crate_name = "markdown"]
3#![deny(missing_docs)]
4// #![deny(warnings)]
5
6#![cfg_attr(feature="clippy", feature(plugin))]
7#![cfg_attr(feature="clippy", plugin(clippy))]
8
9extern crate regex;
10
11#[macro_use]
12extern crate pipeline;
13
14#[macro_use]
15extern crate lazy_static;
16
17use std::fs::File;
18use std::path::Path;
19use std::io::{self, Read};
20
21mod parser;
22mod markdown_generator;
23mod html;
24
25pub use parser::{Block, ListItem, Span};
26
27/// Converts a Markdown string to HTML
28pub fn to_html(text: &str) -> String {
29    let result = parser::parse(text);
30    html::to_html(&result)
31}
32
33/// Converts a Markdown string to a tokenset of Markdown items
34pub fn tokenize(text: &str) -> Vec<Block> {
35    parser::parse(text)
36}
37
38/// Convert tokenset of Markdown items back to String
39pub fn generate_markdown(x: Vec<Block>) -> String {
40    markdown_generator::generate(x)
41}
42
43/// Opens a file and converts its contents to HTML
44pub fn file_to_html(path: &Path) -> io::Result<String> {
45    let mut file = File::open(path)?;
46
47    let mut text = String::new();
48    file.read_to_string(&mut text)?;
49
50    let result = parser::parse(&text);
51    Ok(html::to_html(&result))
52}