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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
use into_string::*;
use parser::*;
use std::iter;

/// Options for rendering Textile markup language.
pub struct RenderOptions {
    pub compress: bool,
    pub indent: u8,
}

impl Default for RenderOptions {
    fn default() -> RenderOptions {
        RenderOptions {
            compress: false,
            indent: 2,
        }
    }
}

/// Renders Textile string into HTML string with default options.
/// Accepts `&str`, `String` or `Path` data type.
///
/// # Example
///
/// ```rust
/// let html = textile::render("h2. *Heading of level 2*");
/// assert_eq!(html, "<h2><strong>Heading of level 2</strong></h2>".to_string());
/// ```
pub fn render<S: IntoString>(text: S) -> String {
    render_blocks(&parse(text.into_string()), &RenderOptions::default())
}

/// Renders Textile string into HTML string with specified options.
/// Accepts `&str`, `String` or `Path` data type.
///
/// # Example
///
/// ```rust
/// let html = textile::render_with("h2. *Heading of level 2*", textile::RenderOptions::default());
/// assert_eq!(html, "<h2><strong>Heading of level 2</strong></h2>".to_string());
/// ```
pub fn render_with<S: IntoString>(text: S, options: RenderOptions) -> String {
    render_blocks(&parse(text.into_string()), &options)
}

fn render_attributes(attributes: &[Attribute], options: &RenderOptions) -> String {
    if !attributes.is_empty() {
        let mut res = Vec::new();

        for attribute in attributes {
            let attr = match *attribute {
                Attribute::Align(ref align) => format!("align=\"{}\"", align),
                Attribute::Class(ref list) => format!("class=\"{}\"", list.join(" ")),
                Attribute::Id(ref id) => format!("id=\"{}\"", id),
                Attribute::Language(ref lang) => format!("lang=\"{}\"", lang),
                Attribute::Style(ref props) => {
                    let mut res = Vec::new();

                    for (key, value) in props {
                        if !options.compress {
                            res.push(format!("{}: {}", key, value))
                        } else {
                            res.push(format!("{}:{}", key, value))
                        }
                    }

                    if !options.compress {
                        format!("style=\"{}\"", res.join("; "))
                    } else {
                        format!("style=\"{}\"", res.join(";"))
                    }
                }
            };
            res.push(attr);
        }
        format!(" {}", res.join(" "))
    } else {
        String::default()
    }
}

fn render_blocks(elements: &[Block], options: &RenderOptions) -> String {
    let mut res = String::new();

    for (idx, element) in elements.iter().enumerate() {
        if idx > 0 && idx < elements.len() && !options.compress {
            res.push_str("\n");
        }
        res.push_str(&*render_block(element, options));
    }
    res
}

fn render_block(element: &Block, options: &RenderOptions) -> String {
    match *element {
        Block::BlockQuotation { ref attributes, ref cite, ref elements } => {
            let cite_attr = if !cite.is_empty() {
                format!(" cite=\"{}\"", cite)
            } else {
                "".to_string()
            };

            if !options.compress {
                let mut res = String::new();
                let spaces: String = iter::repeat(" ").take(options.indent as usize).collect();

                for element in elements {
                    res.push_str(&*format!("\n{}{}", spaces, render_block(element, options)));
                }
                format!("<blockquote{}{}>{}\n</blockquote>",
                        render_attributes(attributes, options),
                        cite_attr,
                        res)
            } else {
                format!("<blockquote{}{}>{}</blockquote>",
                        render_attributes(attributes, options),
                        cite_attr,
                        render_blocks(elements, options))
            }
        }
        Block::CodeBlock { ref attributes, ref code } => {
            format!("<pre{}><code>{}</code></pre>",
                    render_attributes(attributes, options),
                    code)
        }
        Block::Heading { ref attributes, level, ref elements } => {
            format!("<h{0}{1}>{2}</h{0}>",
                    level,
                    render_attributes(attributes, options),
                    render_inline_elements(elements, options))
        }
        Block::NoTextileBlock(ref strings) => strings.join("\n"),
        Block::Paragraph { ref attributes, ref elements, .. } => {
            format!("<p{}>{}</p>",
                    render_attributes(attributes, options),
                    render_inline_elements(elements, options))
        },
        Block::Pre {ref attributes, ref lines} => {
            format!("<pre{}>{}</pre>",
                    render_attributes(attributes, options),
                    lines.join("\n"))
        },
        _ => "".to_string(),
    }
}

fn render_inline_elements(elements: &[Inline], options: &RenderOptions) -> String {
    let mut res = String::new();

    for element in elements {
        let html = match *element {
            Inline::Abbreviation { ref abbr, ref transcript } => {
                format!("<acronym title=\"{}\"><span>{}</span></acronym>",
                        transcript,
                        abbr)
            }
            Inline::Bold { ref attributes, ref elements, ref tag_type } => {
                let tag = match *tag_type {
                    BoldTagType::Strong => "strong",
                    BoldTagType::Bold => "b",
                };
                format!("<{0}{1}>{2}</{0}>",
                        tag,
                        render_attributes(attributes, options),
                        render_inline_elements(elements, options))
            }
            Inline::Break => "<br>".to_string(),
            Inline::Citation { ref attributes, ref elements } => {
                format!("<cite{}>{}</cite>",
                        render_attributes(attributes, options),
                        render_inline_elements(elements, options))
            }
            Inline::Code(ref text) => format!("<code>{}</code>", text),
            Inline::Image { ref attributes, ref alt, ref href, ref src } => {
                let img = if !alt.is_empty() {
                    format!("<img src=\"{0}\" alt=\"{1}\" title=\"{1}\"{2}>",
                            src,
                            alt,
                            render_attributes(attributes, options))
                } else {
                    format!("<img src=\"{}\"{}>",
                            src,
                            render_attributes(attributes, options))
                };

                if !href.is_empty() {
                    format!("<a href=\"{}\">{}</a>", href, img)
                } else {
                    img
                }
            }
            Inline::Italic { ref attributes, ref elements, ref tag_type } => {
                let tag = match *tag_type {
                    ItalicTagType::Emphasis => "em",
                    ItalicTagType::Italic => "i",
                };
                format!("<{0}{1}>{2}</{0}>",
                        tag,
                        render_attributes(attributes, options),
                        render_inline_elements(elements, options))
            }
            Inline::Link { ref attributes, ref elements, ref href, ref title } => {
                if !title.is_empty() {
                    format!("<a href=\"{}\" title=\"{}\"{}>{}</a>",
                            href,
                            title,
                            render_attributes(attributes, options),
                            render_inline_elements(elements, options))
                } else {
                    format!("<a href=\"{}\"{}>{}</a>",
                            href,
                            render_attributes(attributes, options),
                            render_inline_elements(elements, options))
                }
            }
            Inline::Span { ref attributes, ref elements } => {
                format!("<span{}>{}</span>",
                        render_attributes(attributes, options),
                        render_inline_elements(elements, options))
            }
            Inline::Strikethrough { ref attributes, ref elements } => {
                format!("<del{}>{}</del>",
                        render_attributes(attributes, options),
                        render_inline_elements(elements, options))
            }
            Inline::Subscript { ref attributes, ref elements } => {
                format!("<sub{}>{}</sub>",
                        render_attributes(attributes, options),
                        render_inline_elements(elements, options))
            }
            Inline::Superscript { ref attributes, ref elements } => {
                format!("<sup{}>{}</sup>",
                        render_attributes(attributes, options),
                        render_inline_elements(elements, options))
            }
            Inline::Text(ref text) => text.to_string(),
            Inline::Underlined { ref attributes, ref elements } => {
                format!("<ins{}>{}</ins>",
                        render_attributes(attributes, options),
                        render_inline_elements(elements, options))
            }
        };
        res.push_str(&html);
    }
    res
}