hoedown/
markdown.rs

1use std::io::Read;
2
3use buffer::Buffer;
4use extensions::Extension;
5
6/// Markdown document
7#[derive(Clone)]
8pub struct Markdown {
9    pub contents: Buffer,
10    pub extensions: Extension,
11    pub max_nesting: usize,
12}
13
14impl Markdown {
15    pub fn new(body: &str) -> Markdown {
16        Markdown::from(body.as_bytes())
17    }
18
19    /// Construct a markdown document from a given Reader
20    ///
21    /// By default it enables no Hoedown extensions and sets the maximum
22    /// block depth to parse at 16. This may be changed with the `with_extensions`
23    /// and `with_max_nesting` builder methods.
24    ///
25    /// Note that `Buffer` also implements `Reader`, so it can be used with this
26    /// method.
27    pub fn read_from<R>(reader: R) -> Markdown
28    where R: Read {
29        Markdown {
30            contents: Buffer::read_from(reader),
31            extensions: Extension::empty(),
32            max_nesting: 16,
33        }
34    }
35
36    /// Builder method to specify Hoedown extensions
37    pub fn extensions(mut self, extensions: Extension) -> Markdown {
38        self.extensions = extensions;
39        self
40    }
41
42    /// Builder method to specify the maximum block depth to parse
43    pub fn max_nesting(mut self, max_nesting: usize) -> Markdown {
44        self.max_nesting = max_nesting;
45        self
46    }
47}
48
49impl From<Buffer> for Markdown {
50    fn from(buffer: Buffer) -> Markdown {
51        Markdown {
52            contents: buffer,
53            extensions: Extension::empty(),
54            max_nesting: 16,
55        }
56    }
57}
58
59impl<'a> From<&'a [u8]> for Markdown {
60    fn from(bytes: &[u8]) -> Markdown {
61        let buffer = Buffer::from(bytes);
62
63        Markdown {
64            contents: buffer,
65            extensions: Extension::empty(),
66            max_nesting: 16,
67        }
68    }
69}
70
71