Skip to main content

HtmlWriter

Struct HtmlWriter 

Source
pub struct HtmlWriter { /* private fields */ }
Expand description

HTML output writer with pre-allocated, reusable buffer.

§Example

use ferromark::HtmlWriter;

let mut writer = HtmlWriter::with_capacity_for(1000);
writer.write_str("<p>");
writer.write_escaped_text(b"Hello <World>");
writer.write_str("</p>");

let html = writer.into_string();
assert_eq!(html, "<p>Hello &lt;World&gt;</p>");

Implementations§

Source§

impl HtmlWriter

Source

pub fn new() -> Self

Create a new writer with default capacity.

Source

pub fn with_capacity_for(input_len: usize) -> Self

Create with pre-allocated capacity based on expected input size.

Typical HTML is ~1.25x input size; we reserve extra for safety.

Source

pub fn with_capacity(capacity: usize) -> Self

Create with explicit capacity.

Source

pub fn write_bytes(&mut self, bytes: &[u8])

Write raw bytes without escaping.

Source

pub fn write_str(&mut self, s: &'static str)

Write a static string (compile-time known).

Source

pub fn write_string(&mut self, s: &str)

Write a dynamic string without escaping.

Source

pub fn write_byte(&mut self, b: u8)

Write a single byte.

Source

pub fn write_escaped_text(&mut self, text: &[u8])

Write text with HTML escaping (for text content).

Source

pub fn write_text_with_entities(&mut self, text: &[u8])

Write text with entity decoding and HTML escaping (for inline text content). First decodes HTML entities, then escapes for output.

Source

pub fn write_escaped_range(&mut self, input: &[u8], range: Range)

Write text with HTML escaping from a range.

Source

pub fn write_escaped_attr(&mut self, attr: &[u8])

Write attribute value with full escaping (including quotes).

Write URL/title with backslash escape processing and HTML attribute escaping. Backslash-escaped punctuation characters have the backslash removed.

Write link title with entity decoding, backslash escape processing, and HTML escaping.

Source

pub fn write_url_encoded(&mut self, url: &[u8])

Write autolink URL with percent-encoding and HTML escaping. Used for autolink hrefs per CommonMark spec.

Write link destination with backslash escape processing and URL encoding. Used for link destinations in [text](url) syntax.

Source

pub fn newline(&mut self)

Write a newline.

Source

pub fn len(&self) -> usize

Current output length.

Source

pub fn is_empty(&self) -> bool

Check if output is empty.

Source

pub fn clear(&mut self)

Clear output for reuse (keeps capacity).

Source

pub fn as_bytes(&self) -> &[u8]

Get output as byte slice.

Source

pub fn as_str(&self) -> &str

Get output as str (assumes valid UTF-8).

Source

pub fn into_vec(self) -> Vec<u8>

Take ownership of output buffer.

Source

pub fn into_string(self) -> String

Take ownership as String.

Source

pub fn buffer_mut(&mut self) -> &mut Vec<u8>

Get mutable reference to internal buffer.

Use with caution - allows bypassing escaping.

Source

pub fn open_tag(&mut self, tag: &'static str)

Write opening tag: <tagname>

Source

pub fn close_tag(&mut self, tag: &'static str)

Write closing tag: </tagname>

Source

pub fn self_closing_tag(&mut self, tag: &'static str)

Write self-closing tag: <tagname />

Source

pub fn open_tag_nl(&mut self, tag: &'static str)

Write opening tag with newline: <tagname>\n

Source

pub fn close_tag_nl(&mut self, tag: &'static str)

Write closing tag with newline: </tagname>\n

Source

pub fn paragraph_start(&mut self)

Write paragraph start: <p>

Source

pub fn paragraph_end(&mut self)

Write paragraph end: </p>\n

Source

pub fn heading_start(&mut self, level: u8)

Write heading start: <hN>

Source

pub fn heading_start_with_id(&mut self, level: u8, id: &str)

Write heading start with ID: <hN id="slug">

Source

pub fn heading_end(&mut self, level: u8)

Write heading end: </hN>\n

Source

pub fn code_block_start(&mut self, lang: Option<&[u8]>)

Write code block start with optional language class. Processes backslash escapes in the language string.

Source

pub fn write_info_string_attr(&mut self, info: &[u8])

Write fenced code info string with entity decoding and attribute escaping.

Source

pub fn code_block_end(&mut self)

Write code block end: </code></pre>\n

Source

pub fn thematic_break(&mut self)

Write thematic break: <hr />\n

Source

pub fn blockquote_start(&mut self)

Write blockquote start: <blockquote>\n

Source

pub fn blockquote_end(&mut self)

Write blockquote end: </blockquote>\n

Source

pub fn callout_start(&mut self, callout: CalloutType)

Write callout/admonition start.

Source

pub fn callout_end(&mut self)

Write callout/admonition end.

Source

pub fn ul_start(&mut self)

Write list start (unordered): <ul>\n

Source

pub fn ul_end(&mut self)

Write list end (unordered): </ul>\n

Source

pub fn ol_start(&mut self, start: Option<u32>)

Write list start (ordered): <ol>\n or <ol start="N">\n

Source

pub fn ol_end(&mut self)

Write list end (ordered): </ol>\n

Source

pub fn li_start(&mut self)

Write list item start: <li>

Source

pub fn li_end(&mut self)

Write list item end: </li>\n

Source

pub fn table_start(&mut self)

Write table start: <table>\n

Source

pub fn table_end(&mut self)

Write table end: </table>\n

Source

pub fn thead_start(&mut self)

Write thead start: <thead>\n

Source

pub fn thead_end(&mut self)

Write thead end: </thead>\n

Source

pub fn tbody_start(&mut self)

Write tbody start: <tbody>\n

Source

pub fn tbody_end(&mut self)

Write tbody end: </tbody>\n

Source

pub fn tr_start(&mut self)

Write tr start: <tr>\n

Source

pub fn tr_end(&mut self)

Write tr end: </tr>\n

Source

pub fn th_start(&mut self, align: Alignment)

Write th start with optional alignment: <th> or <th align="...">

Source

pub fn th_end(&mut self)

Write th end: </th>\n

Source

pub fn td_start(&mut self, align: Alignment)

Write td start with optional alignment: <td> or <td align="...">

Source

pub fn td_end(&mut self)

Write td end: </td>\n

Source

pub fn inline_code(&mut self, content: &[u8])

Write inline code: <code>escaped_content</code>

Source

pub fn em_start(&mut self)

Write emphasis start: <em>

Source

pub fn em_end(&mut self)

Write emphasis end: </em>

Source

pub fn strong_start(&mut self)

Write strong start: <strong>

Source

pub fn strong_end(&mut self)

Write strong end: </strong>

Source

pub fn del_start(&mut self)

Write strikethrough start: <del>

Source

pub fn del_end(&mut self)

Write strikethrough end: </del>

Write link start: <a href="url">

Write link end: </a>

Source

pub fn line_break(&mut self)

Write line break: <br />\n

Source

pub fn write_html_filtered(&mut self, html: &[u8])

Write raw HTML with GFM disallowed-tag filtering. Replaces < with &lt; before disallowed tag names.

Trait Implementations§

Source§

impl Default for HtmlWriter

Source§

fn default() -> Self

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

impl Write for HtmlWriter

Source§

fn write_str(&mut self, s: &str) -> Result

Writes a string slice into this writer, returning whether the write succeeded. Read more
1.1.0 · Source§

fn write_char(&mut self, c: char) -> Result<(), Error>

Writes a char into this writer, returning whether the write succeeded. Read more
1.0.0 · Source§

fn write_fmt(&mut self, args: Arguments<'_>) -> Result<(), Error>

Glue for usage of the write! macro with implementors of this trait. Read more

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.