Trait hoedown::renderer::Render
[−]
[src]
pub trait Render: Sized { fn render(&mut self, input: &Markdown) -> Buffer { ... } fn render_to(&mut self, input: &Markdown, output: &mut Buffer) { ... } fn render_inline(&mut self, input: &Markdown) -> Buffer { ... } fn render_inline_to(&mut self, input: &Markdown, output: &mut Buffer) { ... } unsafe fn to_hoedown(&mut self) -> hoedown_renderer { ... } fn code_block(
&mut self,
output: &mut Buffer,
text: Option<&Buffer>,
lang: Option<&Buffer>
) { ... } fn quote_block(&mut self, output: &mut Buffer, content: Option<&Buffer>) { ... } fn header(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
level: i32
) { ... } fn horizontal_rule(&mut self, output: &mut Buffer) { ... } fn list(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
flags: List
) { ... } fn list_item(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
flags: List
) { ... } fn paragraph(&mut self, output: &mut Buffer, content: Option<&Buffer>) { ... } fn table(&mut self, output: &mut Buffer, content: Option<&Buffer>) { ... } fn table_header(&mut self, output: &mut Buffer, content: Option<&Buffer>) { ... } fn table_body(&mut self, output: &mut Buffer, content: Option<&Buffer>) { ... } fn table_row(&mut self, output: &mut Buffer, content: Option<&Buffer>) { ... } fn table_cell(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
flags: Table
) { ... } fn footnotes(&mut self, output: &mut Buffer, content: Option<&Buffer>) { ... } fn footnote_definition(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
num: u32
) { ... } fn html_block(&mut self, output: &mut Buffer, text: Option<&Buffer>) { ... } fn autolink(
&mut self,
output: &mut Buffer,
link: Option<&Buffer>,
link_type: AutoLink
) -> bool { ... } fn code_span(&mut self, output: &mut Buffer, text: Option<&Buffer>) -> bool { ... } fn double_emphasis(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool { ... } fn emphasis(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool { ... } fn underline(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool { ... } fn highlight(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool { ... } fn quote_span(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool { ... } fn image(
&mut self,
output: &mut Buffer,
link: Option<&Buffer>,
title: Option<&Buffer>,
alt: Option<&Buffer>
) -> bool { ... } fn line_break(&mut self, output: &mut Buffer) -> bool { ... } fn link(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
link: Option<&Buffer>,
title: Option<&Buffer>
) -> bool { ... } fn triple_emphasis(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool { ... } fn strikethrough(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool { ... } fn superscript(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool { ... } fn footnote_reference(&mut self, output: &mut Buffer, num: u32) -> bool { ... } fn math(
&mut self,
output: &mut Buffer,
text: Option<&Buffer>,
displaymode: i32
) -> bool { ... } fn html_span(&mut self, output: &mut Buffer, text: Option<&Buffer>) -> bool { ... } fn entity(&mut self, output: &mut Buffer, text: Option<&Buffer>) { ... } fn normal_text(&mut self, output: &mut Buffer, text: Option<&Buffer>) { ... } fn before_render(&mut self, output: &mut Buffer, inline_render: bool) { ... } fn after_render(&mut self, output: &mut Buffer, inline_render: bool) { ... } }
Represents render behavior
Types that implement this trait can be used to render a Buffer
document.
All methods have default implementations which may be implemented by the implementing type as required, depending on which callbacks it's interested in.
The default implementations attempt to be as neutral as possible, with the exception of the block callbacks. In the underlying library, hoedown skips the block if the handler is not registered. This behavior can be confusing when creating a custom renderer, so the default implementations for block handlers is to output a message into the output buffer to make it clearer.
Type | Action |
---|---|
block | ignore the block |
span | pass through markdown to output |
rest | pass through content to output |
Below is an example of a custom renderer that collects emphasis elements into a vector that can then be inspected after rendering.
struct EmphCollector { // html renderer to delegate to html: html::Html, // collection of emphasis elements emphs: Vec<String>, } impl EmphCollector { fn new() -> EmphCollector { EmphCollector { html: html::Html::new(html::Flags::empty(), ::std::i32::MAX), emphs: vec![], } } } impl Render for EmphCollector { // pass the content straight to the output buffer fn paragraph(&mut self, ob: &mut Buffer, content: Option<&Buffer>) { content.map(|c| ob.pipe(c)); } fn emphasis(&mut self, ob: &mut Buffer, content: Option<&Buffer>) -> bool { // collect the emphasis element content .and_then(|c| c.to_str().ok()) .map(|s| self.emphs.push(String::from(s))); // delegate rendering the emphasis element to the html renderer self.html.emphasis(ob, content) } } let doc = Markdown::new("this _one_ that _two_ another _three_ pass it _around_"); let mut collector = EmphCollector::new(); let output = collector.render(&doc); assert_eq!( collector.emphs, vec![ String::from("one"), String::from("two"), String::from("three"), String::from("around")]); assert_eq!( "this <em>one</em> that <em>two</em> another <em>three</em> pass it <em>around</em>", output.to_str().unwrap());
Provided Methods
fn render(&mut self, input: &Markdown) -> Buffer
Render the document to a buffer that is returned
fn render_to(&mut self, input: &Markdown, output: &mut Buffer)
Render the document into the given buffer
fn render_inline(&mut self, input: &Markdown) -> Buffer
Render the document as inline to a buffer that is returned
fn render_inline_to(&mut self, input: &Markdown, output: &mut Buffer)
Render the document as inline into the given buffer
unsafe fn to_hoedown(&mut self) -> hoedown_renderer
Converts the type into an underlying hoedown_renderer
structure.
The default implementation of this should suffice for the majority of implementations, unless you know what you're doing.
The default implementation is unsafe because it stores a pointer to self
in the
underlying library. Problems could arise if that pointer is used after self
has
stopped existing.
This library avoids this issue by tightly controlling the the render process
to ensure that the renderer outlives the document (the document is what
requires to_hoedown
)
fn code_block(
&mut self,
output: &mut Buffer,
text: Option<&Buffer>,
lang: Option<&Buffer>
)
&mut self,
output: &mut Buffer,
text: Option<&Buffer>,
lang: Option<&Buffer>
)
Runs when a codeblock is encountered
The lang
parameter will be empty if it's an indented codeblock
or if no language was specified in a fenced codeblock.
The default implementation outputs an error string.
Not run if the DISABLE_INDENTED_CODE
extension is enabled.
fn quote_block(&mut self, output: &mut Buffer, content: Option<&Buffer>)
Runs when a block quote is encountered
The default implementation outputs an error string.
fn header(&mut self, output: &mut Buffer, content: Option<&Buffer>, level: i32)
Runs when a header is encountered
The default implementation outputs an error string.
fn horizontal_rule(&mut self, output: &mut Buffer)
Runs when a horizontal rule is encountered
The default implementation outputs an error string.
fn list(&mut self, output: &mut Buffer, content: Option<&Buffer>, flags: List)
Runs when a list is encountered.
The default implementation outputs an error string.
fn list_item(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
flags: List
)
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
flags: List
)
Runs when a list item is encountered.
The default implementation outputs an error string.
fn paragraph(&mut self, output: &mut Buffer, content: Option<&Buffer>)
Runs when a paragraph is encountered.
The default implementation outputs an error string.
fn table(&mut self, output: &mut Buffer, content: Option<&Buffer>)
Runs when a table is encountered.
Only runs if the TABLES
extension is enabled.
The default implementation outputs an error string.
fn table_header(&mut self, output: &mut Buffer, content: Option<&Buffer>)
Runs when a table header is encountered.
Only runs if the TABLES
extension is enabled.
The default implementation outputs an error string.
fn table_body(&mut self, output: &mut Buffer, content: Option<&Buffer>)
Runs when a table body is encountered.
Only runs if the TABLES
extension is enabled.
The default implementation outputs an error string.
fn table_row(&mut self, output: &mut Buffer, content: Option<&Buffer>)
Runs when a table row is encountered.
Only runs if the TABLES
extension is enabled.
The default implementation outputs an error string.
fn table_cell(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
flags: Table
)
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
flags: Table
)
Runs when a table cell is encountered.
Only runs if the TABLES
extension is enabled.
The default implementation outputs an error string.
fn footnotes(&mut self, output: &mut Buffer, content: Option<&Buffer>)
Runs when footnotes are encountered.
Only runs if the FOOTNOTES
extension is enabled.
The default implementation outputs an error string.
fn footnote_definition(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
num: u32
)
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
num: u32
)
Runs when a footnote definition is encountered.
Only runs if the FOOTNOTES
extension is enabled.
The default implementation outputs an error string.
fn html_block(&mut self, output: &mut Buffer, text: Option<&Buffer>)
Runs when a raw html block is encountered.
The default implementation outputs an error string.
fn autolink(
&mut self,
output: &mut Buffer,
link: Option<&Buffer>,
link_type: AutoLink
) -> bool
&mut self,
output: &mut Buffer,
link: Option<&Buffer>,
link_type: AutoLink
) -> bool
Runs when an autolink candidate is encountered.
Only runs if the AUTOLINK
extension is enabled.
The default implementation passes the context markdown to the buffer verbatim.
fn code_span(&mut self, output: &mut Buffer, text: Option<&Buffer>) -> bool
Runs when a code span is encountered.
The default implementation passes the context markdown to the buffer verbatim.
fn double_emphasis(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool
Runs when double emphasis is encountered.
e.g. **double emphasis**
The default implementation passes the context markdown to the buffer verbatim.
fn emphasis(&mut self, output: &mut Buffer, content: Option<&Buffer>) -> bool
Runs when emphasis is encountered.
The default implementation passes the context markdown to the buffer verbatim.
fn underline(&mut self, output: &mut Buffer, content: Option<&Buffer>) -> bool
Runs when underline is encountered.
Only runs if the UNDERLINE
extension is enabled.
The default implementation passes the context markdown to the buffer verbatim.
fn highlight(&mut self, output: &mut Buffer, content: Option<&Buffer>) -> bool
Runs when highlight is encountered.
Only runs if the HIGHLIGHT
extension is enabled.
The default implementation passes the context markdown to the buffer verbatim.
fn quote_span(&mut self, output: &mut Buffer, content: Option<&Buffer>) -> bool
Runs when a quote is encountered.
Only runs if the QUOTE
extension is enabled.
The default implementation passes the context markdown to the buffer verbatim.
fn image(
&mut self,
output: &mut Buffer,
link: Option<&Buffer>,
title: Option<&Buffer>,
alt: Option<&Buffer>
) -> bool
&mut self,
output: &mut Buffer,
link: Option<&Buffer>,
title: Option<&Buffer>,
alt: Option<&Buffer>
) -> bool
Runs when an image is encountered.
e.g. ![alt](link title)
The default implementation passes the context markdown to the buffer verbatim.
fn line_break(&mut self, output: &mut Buffer) -> bool
Runs when a line break is encountered.
The default implementation passes the context markdown to the buffer verbatim.
fn link(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
link: Option<&Buffer>,
title: Option<&Buffer>
) -> bool
&mut self,
output: &mut Buffer,
content: Option<&Buffer>,
link: Option<&Buffer>,
title: Option<&Buffer>
) -> bool
Runs when a link is encountered.
e.g. [content](link title)
The default implementation passes the context markdown to the buffer verbatim.
fn triple_emphasis(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool
Runs when triple emphasis is encountered.
e.g. ***strongly emphasized***
The default implementation passes the context markdown to the buffer verbatim.
fn strikethrough(
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool
&mut self,
output: &mut Buffer,
content: Option<&Buffer>
) -> bool
Runs when strikethrough is encountered.
Only runs if the STRIKETHROUGH
extension is enabled.
The default implementation passes the context markdown to the buffer verbatim.
fn superscript(&mut self, output: &mut Buffer, content: Option<&Buffer>) -> bool
Runs when superscript is encountered.
Only runs if the SUPERSCRIPT
extension is enabled.
The default implementation passes the context markdown to the buffer verbatim.
fn footnote_reference(&mut self, output: &mut Buffer, num: u32) -> bool
Runs when a footnote reference is encountered.
Only runs if the FOOTNOTES
extension is enabled.
The default implementation passes the context markdown to the buffer verbatim.
fn math(
&mut self,
output: &mut Buffer,
text: Option<&Buffer>,
displaymode: i32
) -> bool
&mut self,
output: &mut Buffer,
text: Option<&Buffer>,
displaymode: i32
) -> bool
Runs when math is encountered.
Only runs if the MATH
extension is enabled.
The default implementation passes the context markdown to the buffer verbatim.
fn html_span(&mut self, output: &mut Buffer, text: Option<&Buffer>) -> bool
Runs when raw html span is encountered.
The default implementation passes the context markdown to the buffer verbatim.
fn entity(&mut self, output: &mut Buffer, text: Option<&Buffer>)
Runs when an html entity is encountered.
The default implementation passes the entity to the output buffer verbatim.
fn normal_text(&mut self, output: &mut Buffer, text: Option<&Buffer>)
Runs when plain text is encountered.
The default implementation passes the entity to the output buffer verbatim.
fn before_render(&mut self, output: &mut Buffer, inline_render: bool)
Runs before the document is processed.
The default implementation does nothing.
fn after_render(&mut self, output: &mut Buffer, inline_render: bool)
Runs after the document has been processed.
The default implementation does nothing.