Skip to content

Commit

Permalink
html: expose some things, remove output capture name.
Browse files Browse the repository at this point in the history
  • Loading branch information
kivikakk committed Feb 25, 2025
1 parent 8495aab commit d8b7553
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
11 changes: 6 additions & 5 deletions examples/custom_formatter.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
use comrak::{create_formatter, nodes::NodeValue};
use std::io::Write;

create_formatter!(CustomFormatter, {
NodeValue::Emph => |output, entering| {
NodeValue::Emph => |context, entering| {
if entering {
output.write_all(b"<i>")?;
context.write_all(b"<i>")?;
} else {
output.write_all(b"</i>")?;
context.write_all(b"</i>")?;
}
},
NodeValue::Strong => |context, entering| {
use std::io::Write;
context.write_all(if entering { b"<b>" } else { b"</b>" })?;
},
NodeValue::Image(ref nl) => |output, node, entering, suppress_children| {
NodeValue::Image(ref nl) => |context, node, entering, suppress_children| {
assert!(node.data.borrow().sourcepos == (3, 1, 3, 18).into());
if entering {
output.write_all(nl.url.to_uppercase().as_bytes())?;
context.write_all(nl.url.to_uppercase().as_bytes())?;
*suppress_children = true;
}
},
Expand Down
27 changes: 14 additions & 13 deletions src/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,8 @@ pub enum RenderMode {
/// are available:
///
/// * `context`: the <code>[&mut] [Context]</code>, giving access to rendering
/// options, plugins, and output appending.
/// * `output`: `context` cast to <code>[&mut] dyn [Write]</code> when you don't
/// need the whole thing.
/// options, plugins, and output appending via its <code>[Write]</code>
/// implementation.
/// * `node`: the <code>[&][&][AstNode]</code> being formatted, when the
/// [`NodeValue`]'s contents aren't enough.
/// * `entering`: [`true`] when the node is being first descended into,
Expand All @@ -85,22 +84,23 @@ pub enum RenderMode {
///
/// ```
/// # use comrak::{create_formatter, parse_document, Arena, Options, nodes::NodeValue};
/// # use std::io::Write;
/// create_formatter!(CustomFormatter, {
/// NodeValue::Emph => |output, entering| {
/// NodeValue::Emph => |context, entering| {
/// if entering {
/// output.write_all(b"<i>")?;
/// context.write_all(b"<i>")?;
/// } else {
/// output.write_all(b"</i>")?;
/// context.write_all(b"</i>")?;
/// }
/// },
/// NodeValue::Strong => |context, entering| {
/// use std::io::Write;
/// context.write_all(if entering { b"<b>" } else { b"</b>" })?;
/// },
/// NodeValue::Image(ref nl) => |output, node, entering, suppress_children| {
/// NodeValue::Image(ref nl) => |context, node, entering, suppress_children| {
/// assert!(node.data.borrow().sourcepos == (3, 1, 3, 18).into());
/// if entering {
/// output.write_all(nl.url.to_uppercase().as_bytes())?;
/// context.write_all(nl.url.to_uppercase().as_bytes())?;
/// *suppress_children = true;
/// }
/// },
Expand Down Expand Up @@ -205,9 +205,6 @@ macro_rules! formatter_captures {
(($context:ident, $node:ident, $entering:ident, $suppress_children:ident), context, $bind:ident) => {
let $bind = $context;
};
(($context:ident, $node:ident, $entering:ident, $suppress_children:ident), output, $bind:ident) => {
let $bind: &mut dyn ::std::io::Write = $context;
};
(($context:ident, $node:ident, $entering:ident, $suppress_children:ident), entering, $bind:ident) => {
let $bind = $entering;
};
Expand All @@ -218,7 +215,7 @@ macro_rules! formatter_captures {
let mut $bind = &mut $suppress_children;
};
(($context:ident, $node:ident, $entering:ident, $suppress_children:ident), $unknown:ident, $bind:ident) => {
compile_error!(concat!("unknown capture '", stringify!($unknown), "'; available are 'context', 'output', 'entering', 'node', 'suppress_children'"));
compile_error!(concat!("unknown capture '", stringify!($unknown), "'; available are 'context', 'entering', 'node', 'suppress_children'"));
};
(($context:ident, $node:ident, $entering:ident, $suppress_children:ident), ($capture:ident)) => {
$crate::formatter_captures!(($context, $node, $entering, $suppress_children), $capture, $capture);
Expand Down Expand Up @@ -1541,7 +1538,11 @@ fn render_wiki_link<'a>(

// Helpers

fn collect_text<'a>(node: &'a AstNode<'a>, output: &mut Vec<u8>) {
/// Recurses through a node and all of its children in depth-first (document)
/// order, appending the literal contents of text, code and math blocks to
/// an output buffer. Line breaks and soft breaks are represented as a single
/// whitespace character.
pub fn collect_text<'a>(node: &'a AstNode<'a>, output: &mut Vec<u8>) {
match node.data.borrow().value {
NodeValue::Text(ref literal) | NodeValue::Code(NodeCode { ref literal, .. }) => {
output.extend_from_slice(literal.as_bytes())
Expand Down
3 changes: 2 additions & 1 deletion src/html/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ pub struct Context<'o, 'c> {
pub options: &'o Options<'c>,
/// [`Plugins`] in use in this render.
pub plugins: &'o Plugins<'o>,
/// [`Anchorizer`] instance used in this render.
pub anchorizer: Anchorizer,

pub(super) anchorizer: Anchorizer,
pub(super) footnote_ix: u32,
pub(super) written_footnote_ix: u32,
}
Expand Down

0 comments on commit d8b7553

Please sign in to comment.