1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
//! A 100% [CommonMark](http://commonmark.org/) and [GFM](https://github.github.com/gfm/)
//! compatible Markdown parser. Source repository is at <https://github.com/kivikakk/comrak>.
//!
//! The design is based on [cmark](https://github.com/github/cmark), so familiarity with that will
//! help.
//!
//! You can use `comrak::markdown_to_html` directly:
//!
//! ```
//! use comrak::{markdown_to_html, ComrakOptions};
//! assert_eq!(markdown_to_html("Hello, **世界**!", &ComrakOptions::default()),
//! "<p>Hello, <strong>世界</strong>!</p>\n");
//! ```
//!
//! Or you can parse the input into an AST yourself, manipulate it, and then use your desired
//! formatter:
//!
//! ```
//! extern crate comrak;
//! use comrak::{Arena, parse_document, format_html, ComrakOptions};
//! use comrak::nodes::{AstNode, NodeValue};
//!
//! # fn main() {
//! // The returned nodes are created in the supplied Arena, and are bound by its lifetime.
//! let arena = Arena::new();
//!
//! let root = parse_document(
//! &arena,
//! "This is my input.\n\n1. Also my input.\n2. Certainly my input.\n",
//! &ComrakOptions::default());
//!
//! fn iter_nodes<'a, F>(node: &'a AstNode<'a>, f: &F)
//! where F : Fn(&'a AstNode<'a>) {
//! f(node);
//! for c in node.children() {
//! iter_nodes(c, f);
//! }
//! }
//!
//! iter_nodes(root, &|node| {
//! match &mut node.data.borrow_mut().value {
//! &mut NodeValue::Text(ref mut text) => {
//! let orig = std::mem::replace(text, vec![]);
//! *text = String::from_utf8(orig).unwrap().replace("my", "your").as_bytes().to_vec();
//! }
//! _ => (),
//! }
//! });
//!
//! let mut html = vec![];
//! format_html(root, &ComrakOptions::default(), &mut html).unwrap();
//!
//! assert_eq!(
//! String::from_utf8(html).unwrap(),
//! "<p>This is your input.</p>\n\
//! <ol>\n\
//! <li>Also your input.</li>\n\
//! <li>Certainly your input.</li>\n\
//! </ol>\n");
//! # }
//! ```
#![deny(
missing_docs,
missing_debug_implementations,
missing_copy_implementations,
trivial_casts,
trivial_numeric_casts,
unstable_features,
unused_import_braces
)]
#![allow(unknown_lints, doc_markdown, cyclomatic_complexity)]
#![cfg_attr(feature = "benchmarks", allow(unstable_features))]
#![cfg_attr(feature = "benchmarks", feature(test))]
extern crate entities;
#[macro_use]
extern crate lazy_static;
extern crate pest;
#[macro_use]
extern crate pest_derive;
extern crate regex;
#[cfg(feature = "benchmarks")]
extern crate test;
#[cfg(test)]
extern crate timebomb;
extern crate twoway;
extern crate typed_arena;
extern crate unicode_categories;
mod arena_tree;
mod cm;
mod ctype;
mod entity;
mod html;
pub mod nodes;
mod parser;
mod scanners;
mod strings;
#[cfg(test)]
mod tests;
pub use cm::format_document as format_commonmark;
pub use html::format_document as format_html;
pub use parser::{parse_document, ComrakOptions};
pub use typed_arena::Arena;
/// Render Markdown to HTML.
///
/// See the documentation of the crate root for an example.
pub fn markdown_to_html(md: &str, options: &ComrakOptions) -> String {
let arena = Arena::new();
let root = parse_document(&arena, md, options);
let mut s = Vec::new();
format_html(root, options, &mut s).unwrap();
String::from_utf8(s).unwrap()
}