CustomNode

Trait CustomNode 

Source
pub trait CustomNode:
    Debug
    + Send
    + Sync {
    // Required methods
    fn clone_box(&self) -> Box<dyn CustomNode>;
    fn eq_box(&self, other: &dyn CustomNode) -> bool;
    fn is_block(&self) -> bool;
    fn as_any(&self) -> &dyn Any;
    fn as_any_mut(&mut self) -> &mut dyn Any;

    // Provided methods
    fn write_block(&self, writer: &mut BlockWriterProxy<'_>) -> WriteResult<()> { ... }
    fn write_inline(
        &self,
        writer: &mut InlineWriterProxy<'_>,
    ) -> WriteResult<()> { ... }
    fn html_write(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> { ... }
    fn type_name(&self) -> &'static str { ... }
}
Expand description

Trait for implementing custom node behavior for the CommonMark AST.

This trait defines methods that all custom node types must implement. Users can implement dedicated block or inline rendering methods for CommonMark output and optionally override the html_write method for HTML output.

The recommended way to implement this trait is through the custom_node macro, which provides a default implementation of most methods and requires users to implement only the node-specific logic.

§Example

use ecow::EcoString;
use cmark_writer_macros::custom_node;
use cmark_writer::error::WriteResult;
use cmark_writer::writer::{HtmlWriteResult, HtmlWriter, InlineWriterProxy};

// Define a custom node with support for both CommonMark and HTML output
#[derive(Debug, Clone, PartialEq)]
#[custom_node(block=false, html_impl=true)]
struct HighlightNode {
    content: EcoString,
    color: EcoString,
}

impl HighlightNode {
    // Required for CommonMark output
    fn write_custom(&self, writer: &mut InlineWriterProxy) -> WriteResult<()> {
        writer.write_str("<span style=\"background-color: ")?;
        writer.write_str(&self.color)?;
        writer.write_str("\">")?;
        writer.write_str(&self.content)?;
        writer.write_str("</span>")?;
        Ok(())
    }

    // Optional HTML-specific implementation
    fn write_html_custom(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()> {
        writer.start_tag("span")?;
        writer.attribute("style", &format!("background-color: {}", self.color))?;
        writer.finish_tag()?;
        writer.text(&self.content)?;
        writer.end_tag("span")?;
        Ok(())
    }
}

Required Methods§

Source

fn clone_box(&self) -> Box<dyn CustomNode>

Clone the custom node

Source

fn eq_box(&self, other: &dyn CustomNode) -> bool

Check if two custom nodes are equal

Source

fn is_block(&self) -> bool

Whether the custom node is a block element

Source

fn as_any(&self) -> &dyn Any

Convert to Any for type casting

Source

fn as_any_mut(&mut self) -> &mut dyn Any

Convert to mutable Any for type casting

Provided Methods§

Source

fn write_block(&self, writer: &mut BlockWriterProxy<'_>) -> WriteResult<()>

Write the custom node as a block element using the restricted block writer proxy.

Block custom nodes should implement this method to emit valid block-level content.

Source

fn write_inline(&self, writer: &mut InlineWriterProxy<'_>) -> WriteResult<()>

Write the custom node as an inline element using the restricted inline writer proxy.

Inline custom nodes should implement this method to emit valid inline content.

Source

fn html_write(&self, writer: &mut HtmlWriter) -> HtmlWriteResult<()>

Writes the HTML representation of the custom node to the provided HTML writer.

By default, this writes an HTML comment indicating that HTML rendering is not implemented for this custom node type. When using the custom_node macro with html_impl=true, this method delegates to the user-defined write_html_custom method.

Users should either:

  1. Override this method directly, or
  2. Use the custom_node macro with html_impl=true and implement the write_html_custom method.
Source

fn type_name(&self) -> &'static str

Get the type name of the custom node for pattern matching

Trait Implementations§

Source§

impl Clone for Box<dyn CustomNode>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl PartialEq for Box<dyn CustomNode>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.

Implementors§