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=trueattribute
§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(¶).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: HtmlWriterOptionsWriter options
Implementations§
Source§impl HtmlWriter
impl HtmlWriter
Sourcepub fn with_options(options: HtmlWriterOptions) -> Self
pub fn with_options(options: HtmlWriterOptions) -> Self
Creates a new HTML writer with the specified options.
Sourcepub fn with_diagnostic_sink(
self,
sink: Box<dyn DiagnosticSink + 'static>,
) -> Self
pub fn with_diagnostic_sink( self, sink: Box<dyn DiagnosticSink + 'static>, ) -> Self
Replace the diagnostic sink used to capture non-fatal issues.
Sourcepub fn set_diagnostic_sink(&mut self, sink: Box<dyn DiagnosticSink + 'static>)
pub fn set_diagnostic_sink(&mut self, sink: Box<dyn DiagnosticSink + 'static>)
Swap the diagnostic sink on an existing writer.
Sourcepub fn diagnostic_sink(&mut self) -> &mut dyn DiagnosticSink
pub fn diagnostic_sink(&mut self) -> &mut dyn DiagnosticSink
Get a mutable handle to the diagnostic sink.
Sourcepub fn set_options(&mut self, options: HtmlWriterOptions)
pub fn set_options(&mut self, options: HtmlWriterOptions)
Updates the writer’s options at runtime.
Sourcepub fn options(&self) -> &HtmlWriterOptions
pub fn options(&self) -> &HtmlWriterOptions
Gets a reference to the current options.
Sourcepub fn options_mut(&mut self) -> &mut HtmlWriterOptions
pub fn options_mut(&mut self) -> &mut HtmlWriterOptions
Gets a mutable reference to the current options.
Sourcepub fn with_modified_options<F>(self, f: F) -> Selfwhere
F: FnOnce(&mut HtmlWriterOptions),
pub fn with_modified_options<F>(self, f: F) -> Selfwhere
F: FnOnce(&mut HtmlWriterOptions),
Creates a new writer with modified options using a closure.
Sourcepub fn into_string(self) -> HtmlWriteResult<EcoString>
pub fn into_string(self) -> HtmlWriteResult<EcoString>
Consumes the writer and returns the generated HTML string.
Sourcepub fn start_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()>
pub fn start_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()>
Starts an HTML tag with the given name.
Sourcepub fn attribute(&mut self, key: &str, value: &str) -> HtmlWriteResult<()>
pub fn attribute(&mut self, key: &str, value: &str) -> HtmlWriteResult<()>
Adds an attribute to the currently open tag.
Sourcepub fn finish_tag(&mut self) -> HtmlWriteResult<()>
pub fn finish_tag(&mut self) -> HtmlWriteResult<()>
Finishes the current open tag.
Sourcepub fn end_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()>
pub fn end_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()>
Closes an HTML tag with the given name.
Sourcepub fn text(&mut self, text: &str) -> HtmlWriteResult<()>
pub fn text(&mut self, text: &str) -> HtmlWriteResult<()>
Writes text content, escaping HTML special characters.
Sourcepub fn self_closing_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()>
pub fn self_closing_tag(&mut self, tag_name: &str) -> HtmlWriteResult<()>
Writes a self-closing tag with only a tag name.
Sourcepub fn finish_self_closing_tag(&mut self) -> HtmlWriteResult<()>
pub fn finish_self_closing_tag(&mut self) -> HtmlWriteResult<()>
Finishes the current open tag as a self-closing tag.
Sourcepub fn write_trusted_html(&mut self, html: &str) -> HtmlWriteResult<()>
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.
Sourcepub fn write_untrusted_html(&mut self, html: &str) -> HtmlWriteResult<()>
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.
Sourcepub 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
pub fn raw_html(&mut self, html: &str) -> HtmlWriteResult<()>
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.
Sourcepub fn write_node(&mut self, node: &Node) -> HtmlWriteResult<()>
pub fn write_node(&mut self, node: &Node) -> HtmlWriteResult<()>
Writes an AST Node to HTML using the configured options.