-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
139 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
mod external; | ||
mod markdown; | ||
|
||
pub use external::ExternalCommand; | ||
pub use markdown::Markdown; | ||
|
||
/// Markdown renderer implementation. | ||
/// | ||
/// Implementors of this trait convert markdown into HTML. | ||
pub trait Renderer { | ||
/// Potential errors returned by rendering. If rendering is infallible (markdown can always | ||
/// produce HTML from its input), this type can be set to [`std::convert::Infallible`]. | ||
type Error; | ||
|
||
/// Renders markdown as HTML. | ||
/// | ||
/// The HTML should be written directly into the `html` buffer. | ||
fn render(&self, input: &str, html: &mut String) -> Result<(), Self::Error>; | ||
|
||
/// A hint for how many bytes the output will be. | ||
/// | ||
/// This hint should be cheap to compute and is not required to be accurate. However, accurate | ||
/// hints may improve performance by saving intermediate allocations. | ||
fn size_hint(&self, input: &str) -> usize { | ||
input.len() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use std::cell::RefCell; | ||
use std::io::{self, prelude::*}; | ||
use std::process::{Command, Stdio}; | ||
|
||
use super::Renderer; | ||
|
||
/// Markdown renderer that uses an external command as a backend. | ||
/// | ||
/// The [`Markdown`] renderer uses an extremely fast, in-memory parser that is sufficient for most | ||
/// use-cases. However, this renderer may be useful if your markdown requires features unsupported | ||
/// by [`pulldown_cmark`]. | ||
/// | ||
/// # Example | ||
/// | ||
/// Creating an external renderer that uses [pandoc](https://pandoc.org/): | ||
/// | ||
/// ```no_run | ||
/// use std::process::Command; | ||
/// use aurelius::ExternalCommand; | ||
/// | ||
/// let mut pandoc = Command::new("pandoc"); | ||
/// pandoc.args(&["-f", "markdown", "-t", "html"]); | ||
/// | ||
/// ExternalCommand::new(pandoc); | ||
/// ``` | ||
#[derive(Debug)] | ||
pub struct ExternalCommand { | ||
command: RefCell<Command>, | ||
} | ||
|
||
impl ExternalCommand { | ||
/// Create a new external command renderer that will spawn processes using the given `command`. | ||
/// | ||
/// The provided [`Command`] should expect markdown input on stdin and print HTML on stdout. | ||
pub fn new(mut command: Command) -> ExternalCommand { | ||
command | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::null()); | ||
|
||
ExternalCommand { | ||
command: RefCell::new(command), | ||
} | ||
} | ||
} | ||
|
||
impl Renderer for ExternalCommand { | ||
type Error = io::Error; | ||
|
||
fn render(&self, markdown: &str, html: &mut String) -> Result<(), Self::Error> { | ||
let child = self.command.borrow_mut().spawn()?; | ||
|
||
child.stdin.unwrap().write_all(markdown.as_bytes())?; | ||
|
||
child.stdout.unwrap().read_to_string(html)?; | ||
|
||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
use std::convert::Infallible; | ||
|
||
use pulldown_cmark::{html, Options, Parser}; | ||
|
||
use super::Renderer; | ||
|
||
/// Markdown renderer that uses [`pulldown_cmark`] as the backend. | ||
#[derive(Debug)] | ||
pub struct Markdown { | ||
options: Options, | ||
} | ||
|
||
impl Markdown { | ||
/// Create a new instance of the renderer. | ||
pub fn new() -> Markdown { | ||
Markdown { | ||
options: Options::ENABLE_FOOTNOTES | ||
| Options::ENABLE_TABLES | ||
| Options::ENABLE_STRIKETHROUGH | ||
| Options::ENABLE_TASKLISTS, | ||
} | ||
} | ||
} | ||
|
||
impl Default for Markdown { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Renderer for Markdown { | ||
type Error = Infallible; | ||
|
||
fn render(&self, markdown: &str, html: &mut String) -> Result<(), Self::Error> { | ||
let parser = Parser::new_ext(markdown, self.options); | ||
|
||
html::push_html(html, parser); | ||
|
||
Ok(()) | ||
} | ||
|
||
fn size_hint(&self, input: &str) -> usize { | ||
input.len() * 3 / 2 | ||
} | ||
} |