Trait CustomNode

Source
pub trait CustomNode:
    Debug
    + Send
    + Sync {
    // Required methods
    fn write(&self, writer: &mut CommonMarkWriter) -> WriteResult<()>;
    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 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 the write method 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 cmark_writer_macros::custom_node;
use cmark_writer::CommonMarkWriter;
use cmark_writer::writer::HtmlWriter;
use cmark_writer::error::WriteResult;
use cmark_writer::writer::HtmlWriteResult;

// 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: String,
    color: String,
}

impl HighlightNode {
    // Required for CommonMark output
    fn write_custom(&self, writer: &mut CommonMarkWriter) -> 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 write(&self, writer: &mut CommonMarkWriter) -> WriteResult<()>

Write the custom node content to the CommonMarkWriter (for CommonMark output).

When using the custom_node macro, this method delegates to the user-defined write_custom method that must be implemented on the node type.

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 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 copy 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§