-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #21.
- Loading branch information
Showing
7 changed files
with
185 additions
and
94 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
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,33 @@ | ||
//! HTML rendering. | ||
mod command; | ||
mod markdown; | ||
|
||
pub use command::CommandRenderer; | ||
pub use markdown::MarkdownRenderer; | ||
|
||
/// HTML renderer implementation. | ||
/// | ||
/// Implementors of this trait convert input into HTML. | ||
pub trait Renderer { | ||
/// Potential errors returned by the rendering. If rendering is infallible (for example, | ||
/// markdown can always produce HTML from its input), this type can be set to | ||
/// [`std::convert::Infallible`]. | ||
type Error; | ||
|
||
/// Renders input as HTML. | ||
/// | ||
/// The HTML should be written directly into the `html` buffer. The buffer will be reused | ||
/// between multiple calls to this method, with its capacity already reserved, so this function | ||
/// only needs to write the HTML. | ||
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 when reserving capacity | ||
/// for the output buffer. | ||
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; | ||
|
||
/// Renderer that uses an external command to render input. | ||
/// | ||
/// [`MarkdownRenderer`](crate::render::MarkdownRenderer) 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/) to render markdown: | ||
/// | ||
/// ```no_run | ||
/// use std::process::Command; | ||
/// use aurelius::render::CommandRenderer; | ||
/// | ||
/// let mut pandoc = Command::new("pandoc"); | ||
/// pandoc.args(&["-f", "markdown", "-t", "html"]); | ||
/// | ||
/// CommandRenderer::new(pandoc); | ||
/// ``` | ||
#[derive(Debug)] | ||
pub struct CommandRenderer { | ||
command: RefCell<Command>, | ||
} | ||
|
||
impl CommandRenderer { | ||
/// 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) -> CommandRenderer { | ||
command | ||
.stdin(Stdio::piped()) | ||
.stdout(Stdio::piped()) | ||
.stderr(Stdio::null()); | ||
|
||
CommandRenderer { | ||
command: RefCell::new(command), | ||
} | ||
} | ||
} | ||
|
||
impl Renderer for CommandRenderer { | ||
type Error = io::Error; | ||
|
||
fn render(&self, input: &str, html: &mut String) -> Result<(), Self::Error> { | ||
let child = self.command.borrow_mut().spawn()?; | ||
|
||
child.stdin.unwrap().write_all(input.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 MarkdownRenderer { | ||
options: Options, | ||
} | ||
|
||
impl MarkdownRenderer { | ||
/// Create a new instance of the renderer. | ||
pub fn new() -> MarkdownRenderer { | ||
MarkdownRenderer { | ||
options: Options::ENABLE_FOOTNOTES | ||
| Options::ENABLE_TABLES | ||
| Options::ENABLE_STRIKETHROUGH | ||
| Options::ENABLE_TASKLISTS, | ||
} | ||
} | ||
} | ||
|
||
impl Default for MarkdownRenderer { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
impl Renderer for MarkdownRenderer { | ||
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 | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
use tokio::net::lookup_host; | ||
|
||
use aurelius::render::MarkdownRenderer; | ||
use aurelius::Server; | ||
|
||
mod files; | ||
mod options; | ||
|
||
async fn new_server() -> anyhow::Result<Server> { | ||
async fn new_server() -> anyhow::Result<Server<MarkdownRenderer>> { | ||
let addr = lookup_host("localhost:0").await?.next().unwrap(); | ||
Ok(Server::bind(&addr).await?) | ||
Ok(Server::bind(&addr, MarkdownRenderer::new()).await?) | ||
} |
Oops, something went wrong.