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
//! # Flatiron
//!
//! `flatiron` is a parser and HTML renderer for the [Textile markup language](https://web.archive.org/web/20021226034931/http://textism.com/article/648/)

use nom::combinator::complete;

mod parse;
mod render;
mod structs;

/// Convert a textile String to HTML.
///
/// ## Example
///
/// ```
/// use flatiron::convert;
///
/// let textile = String::from("h1. Is this thing on?");
/// let html = convert(textile);
/// assert_eq!(html, String::from("<h1>Is this thing on?</h1>\n"));
/// ```
pub fn convert(input: String) -> String {
    let (_, textile) = complete(parse::textile)(&input).unwrap();
    escape_unicode(format!("{}", textile))
}

fn escape_unicode(input: String) -> String {
    input
        .chars()
        .into_iter()
        .map(|c| {
            if !c.is_ascii() {
                format!("&#{};", u32::from(c))
            } else {
                String::from(c)
            }
        })
        .collect()
}