HtmlWriter

Struct HtmlWriter 

Source
pub struct HtmlWriter {
    pub options: HtmlWriterOptions,
    /* private fields */
}
Expand description

HTML writer for serializing CommonMark AST nodes to HTML.

HtmlWriter provides a flexible API for generating HTML content from AST nodes. It can be used:

  • Directly with individual nodes through methods like write_node
  • For building HTML elements programmatically using the tag and attribute methods
  • As part of the CommonMarkWriter’s HTML rendering process
  • In custom node implementations via the html_impl=true attribute

§Examples

§Basic usage

use cmark_writer::{HtmlWriter, Node};

let mut writer = HtmlWriter::new();
let para = Node::Paragraph(vec![Node::Text("Hello, world!".into())]);
writer.write_node(&para).unwrap();

let output = writer.into_string().unwrap();
assert_eq!(output, "<p>Hello, world!</p>\n");

§Building HTML elements manually

use cmark_writer::HtmlWriter;

let mut writer = HtmlWriter::new();

// Create a custom HTML element
writer.start_tag("div").unwrap();
writer.attribute("class", "container").unwrap();
writer.finish_tag().unwrap();

writer.start_tag("h1").unwrap();
writer.finish_tag().unwrap();
writer.text("Welcome").unwrap();
writer.end_tag("h1").unwrap();

writer.end_tag("div").unwrap();

let output = writer.into_string().unwrap();
assert_eq!(output, "<div class=\"container\"><h1>Welcome</h1></div>");

Fields§

§options: HtmlWriterOptions

Writer options

Implementations§

Source§

impl HtmlWriter

Source

pub fn new() -> Self

Creates a new HTML writer with default options.

Source

pub fn with_options(options: HtmlWriterOptions) -> Self

Creates a new HTML writer with the specified options.

Source

pub fn with_diagnostic_sink( self, sink: Box<dyn DiagnosticSink + 'static>, ) -> Self

Replace the diagnostic sink used to capture non-fatal issues.

Source

pub fn set_diagnostic_sink(&mut self, sink: Box<dyn DiagnosticSink + 'static>)

Swap the diagnostic sink on an existing writer.

Source

pub fn diagnostic_sink(&mut self) -> &mut dyn DiagnosticSink

Get a mutable handle to the diagnostic sink.

Source

pub fn set_options(&mut self, options: HtmlWriterOptions)

Updates the writer’s options at runtime.

Source

pub fn options(&self) -> &HtmlWriterOptions

Gets a reference to the current options.

Source

pub fn options_mut(&mut self) -> &mut HtmlWriterOptions

Gets a mutable reference to the current options.

Source

pub fn with_modified_options<F>(self, f: F) -> Self
where F: FnOnce(&mut HtmlWriterOptions),

Creates a new writer with modified options using a closure.

Source

pub fn into_string(self) -> HtmlWriteResult<EcoString>

Consumes the writer and returns the generated HTML string.

Source

pub fn start_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()>

Starts an HTML tag with the given name.

Source

pub fn attribute(&mut self, key: &str, value: &str) -> HtmlWriteResult<()>

Adds an attribute to the currently open tag.

Source

pub fn finish_tag(&mut self) -> HtmlWriteResult<()>

Finishes the current open tag.

Source

pub fn end_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()>

Closes an HTML tag with the given name.

Source

pub fn text(&mut self, text: &str) -> HtmlWriteResult<()>

Writes text content, escaping HTML special characters.

Source

pub fn self_closing_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()>

Writes a self-closing tag with only a tag name.

Source

pub fn finish_self_closing_tag(&mut self) -> HtmlWriteResult<()>

Finishes the current open tag as a self-closing tag.

Source

pub fn write_trusted_html(&mut self, html: &str) -> HtmlWriteResult<()>

Writes HTML content that is trusted to be well-formed and safe.

Prefer this when the HTML fragment originates from the renderer itself (e.g. structural newlines or tags we synthesise). External or user-provided content should go through Self::write_untrusted_html to ensure escaping.

Source

pub fn write_untrusted_html(&mut self, html: &str) -> HtmlWriteResult<()>

Writes HTML content that may contain characters requiring escaping.

This is a semantic alias for Self::text, making call sites explicit about handling potentially untrusted content.

Source

pub fn raw_html(&mut self, html: &str) -> HtmlWriteResult<()>

👎Deprecated since 0.8.0: Use write_trusted_html for trusted fragments or write_untrusted_html for escaping

Writes an HTML fragment without additional escaping.

§Deprecation

Prefer using Self::write_trusted_html or Self::write_untrusted_html to make the trust boundary explicit at the call site.

Source

pub fn write_node(&mut self, node: &Node) -> HtmlWriteResult<()>

Writes an AST Node to HTML using the configured options.

Trait Implementations§

Source§

impl Debug for HtmlWriter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for HtmlWriter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl NodeHandler for HtmlWriter

Source§

type Error = HtmlWriteError

Error type produced during traversal.
Source§

fn document(&mut self, children: &[Node]) -> HtmlWriteResult<()>

Source§

fn paragraph(&mut self, content: &[Node]) -> HtmlWriteResult<()>

Source§

fn text(&mut self, text: &EcoString) -> HtmlWriteResult<()>

Source§

fn emphasis(&mut self, content: &[Node]) -> HtmlWriteResult<()>

Source§

fn strong(&mut self, content: &[Node]) -> HtmlWriteResult<()>

Source§

fn thematic_break(&mut self) -> HtmlWriteResult<()>

Source§

fn heading( &mut self, level: u8, content: &[Node], _heading_type: &HeadingType, ) -> HtmlWriteResult<()>

Source§

fn inline_code(&mut self, code: &EcoString) -> HtmlWriteResult<()>

Source§

fn code_block( &mut self, language: &Option<EcoString>, content: &EcoString, _kind: &CodeBlockType, ) -> HtmlWriteResult<()>

Source§

fn html_block(&mut self, content: &EcoString) -> HtmlWriteResult<()>

Source§

fn html_element(&mut self, element: &HtmlElement) -> HtmlWriteResult<()>

Source§

fn block_quote(&mut self, content: &[Node]) -> HtmlWriteResult<()>

Source§

fn unordered_list(&mut self, items: &[ListItem]) -> HtmlWriteResult<()>

Source§

fn ordered_list( &mut self, start: u32, items: &[ListItem], ) -> HtmlWriteResult<()>

Source§

fn table(&mut self, headers: &[Node], rows: &[Vec<Node>]) -> HtmlWriteResult<()>

Source§

fn image( &mut self, url: &EcoString, title: &Option<EcoString>, alt: &[Node], ) -> HtmlWriteResult<()>

Source§

fn soft_break(&mut self) -> HtmlWriteResult<()>

Source§

fn hard_break(&mut self) -> HtmlWriteResult<()>

Source§

fn custom(&mut self, node: &dyn CustomNode) -> HtmlWriteResult<()>

Source§

fn unsupported(&mut self, node: &Node) -> HtmlWriteResult<()>

Source§

fn visit_node(&mut self, node: &Node) -> Result<(), Self::Error>

Dispatch a single node. Most implementers will not override this and will instead implement the per-variant methods below.
Source§

fn visit_nodes(&mut self, nodes: &[Node]) -> Result<(), Self::Error>

Visit a sequence of nodes.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.