html-to-markdown-rs 3.18.6

High-performance HTML to Markdown converter using the astral-tl parser. Part of the Xberg ecosystem.
Documentation
//! Handlers for semantic inline elements with attributes.
//!
//! Processes semantic inline elements that often carry semantic meaning through attributes:
//! - `<cite>` - Citation/reference to a source
//! - `<q>` - Inline quotation
//! - `<abbr>` - Abbreviation with optional title explanation
//! - `<dfn>` - Definition of a term
//! - `<time>` - Machine-readable date/time
//! - `<data>` - Machine-readable data value
//!
//! These elements carry semantic meaning in HTML5 but often have minimal
//! visual distinction in rendered Markdown. Some are formatted with emphasis
//! or have their attributes included in the output.

use crate::converter::utility::content::chomp_inline;
use crate::converter::walk::{CaptureEnd, Close, Open};
use std::rc::Rc;

/// Handles the `<dfn>` element.
///
/// A dfn element marks a term that is being defined. The content represents
/// the term, and its definition would typically appear in surrounding context.
/// It is rendered as emphasized (italic) text.
///
/// # Behavior
///
/// - Content is collected from children
/// - Non-empty content is wrapped with the configured emphasis symbol (default: `*`)
/// - Inline suffix handling is applied (e.g., footnote references)
pub fn handle_dfn(
    _tag_name: &str,
    node_handle: &tl::NodeHandle,
    parser: &tl::Parser,
    output: &mut String,
    handler: super::HandlerContext<'_>,
) {
    if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
        let mut content = String::with_capacity(32);
        let children = tag.children();
        {
            for child_handle in children.top().iter() {
                super::walk_node(
                    child_handle,
                    parser,
                    &mut content,
                    crate::converter::block::container::HandlerContext::new(
                        handler.options,
                        handler.ctx,
                        handler.depth + 1,
                        handler.dom_ctx,
                    ),
                );
            }
        }

        let (prefix, suffix, trimmed) = chomp_inline(&content);
        if !trimmed.is_empty() {
            output.push_str(prefix);
            output.push(handler.options.strong_em_symbol);
            output.push_str(trimmed);
            output.push(handler.options.strong_em_symbol);
            append_inline_suffix(
                output,
                suffix,
                !trimmed.is_empty(),
                node_handle,
                parser,
                handler.dom_ctx,
            );
        }
    }
}

/// Handles the `<abbr>` element.
///
/// An abbr element marks an abbreviation or acronym. The `title` attribute
/// provides the expansion of the abbreviation, which is appended in parentheses
/// if present.
///
/// # Behavior
///
/// - Content is collected from children
/// - Non-empty content is output as-is
/// - If `title` attribute exists, it is appended in parentheses: `abbr (title)`
///
/// # Example
///
/// ```html
/// <abbr title="HyperText Markup Language">HTML</abbr>
/// ```
///
/// Produces: `HTML (HyperText Markup Language)`
pub fn handle_abbr(
    _tag_name: &str,
    node_handle: &tl::NodeHandle,
    parser: &tl::Parser,
    output: &mut String,
    handler: super::HandlerContext<'_>,
) {
    if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
        let mut content = String::with_capacity(32);
        let children = tag.children();
        {
            for child_handle in children.top().iter() {
                super::walk_node(
                    child_handle,
                    parser,
                    &mut content,
                    crate::converter::block::container::HandlerContext::new(
                        handler.options,
                        handler.ctx,
                        handler.depth + 1,
                        handler.dom_ctx,
                    ),
                );
            }
        }

        let trimmed = content.trim();
        if !trimmed.is_empty() {
            output.push_str(trimmed);

            if handler.options.expand_abbreviations
                && let Some(title) = crate::converter::utility::attributes::decoded_attribute(tag, "title")
            {
                let trimmed_title = title.trim();
                if !trimmed_title.is_empty() {
                    output.push_str(" (");
                    output.push_str(trimmed_title);
                    output.push(')');
                }
            }
        }
    }
}

/// Handles the `<time>` and `<data>` elements.
///
/// Time and data elements contain machine-readable content in their attributes
/// and human-readable content in their text. For Markdown purposes, we output
/// only the human-readable text content, as Markdown doesn't have a way to
/// preserve machine-readable metadata.
///
/// # Behavior
///
/// - Content is extracted from children and output as-is
/// - Attributes (datetime, value) are not rendered in Markdown output
pub fn handle_time_data(
    _tag_name: &str,
    node_handle: &tl::NodeHandle,
    parser: &tl::Parser,
    output: &mut String,
    handler: super::HandlerContext<'_>,
) {
    if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
        let children = tag.children();
        {
            for child_handle in children.top().iter() {
                super::walk_node(
                    child_handle,
                    parser,
                    output,
                    crate::converter::block::container::HandlerContext::new(
                        handler.options,
                        handler.ctx,
                        handler.depth + 1,
                        handler.dom_ctx,
                    ),
                );
            }
        }
    }
}

/// Handles the `<cite>` element.
///
/// A cite element marks the title of a cited work (book, article, website, etc.).
/// It is rendered as emphasized (italic) text in block mode, or as plain text in inline mode.
///
/// # Behavior
///
/// - **Block mode**: Content is wrapped with emphasis markers (default: `*`)
/// - **Inline mode**: Content is output as-is without formatting
pub fn handle_cite(
    _tag_name: &str,
    node_handle: &tl::NodeHandle,
    parser: &tl::Parser,
    output: &mut String,
    handler: super::HandlerContext<'_>,
) {
    if let Some(tl::Node::Tag(tag)) = node_handle.get(parser) {
        let mut content = String::with_capacity(32);
        let children = tag.children();
        {
            for child_handle in children.top().iter() {
                super::walk_node(
                    child_handle,
                    parser,
                    &mut content,
                    crate::converter::block::container::HandlerContext::new(
                        handler.options,
                        handler.ctx,
                        handler.depth + 1,
                        handler.dom_ctx,
                    ),
                );
            }
        }

        let trimmed = content.trim();
        if !trimmed.is_empty() {
            if handler.ctx.convert_as_inline {
                output.push_str(trimmed);
            } else {
                output.push('*');
                output.push_str(trimmed);
                output.push('*');
            }
        }
    }
}

/// Handles the `<q>` element.
///
/// A q element marks an inline quotation. In Markdown, it is rendered as
/// quoted text enclosed in double quotes. Backslashes and quotes within
/// the content are escaped.
///
/// # Behavior
///
/// - **Block mode**: Content is wrapped in escaped double quotes: `"content"`
/// - **Inline mode**: Content is output as-is without quotes
///
/// # Escaping
///
/// Internal backslashes and double quotes are escaped:
/// - `\` → `\\`
/// - `"` → `\"`
///
/// Lists the children in `children`. The walk captures their text for [`close_q`]. Where the
/// links of inline code are collected, the children write to the buffer of the element and
/// [`close_collected_q`] ends it.
pub fn open_q(
    node_handle: &tl::NodeHandle,
    parser: &tl::Parser,
    output: &mut String,
    handler: super::HandlerContext<'_>,
    children: &mut Vec<tl::NodeHandle>,
) -> Option<Open> {
    let Some(tl::Node::Tag(tag)) = node_handle.get(parser) else {
        return None;
    };
    children.extend(tag.children().top().iter().copied());
    if let Some(links) = handler.ctx.inline_code_links.as_ref() {
        let end = CollectedQuoteEnd {
            link_count: links.borrow().len(),
            start: output.len(),
        };
        output.push('"');
        return Some(Open {
            scope: None,
            close: Close::CollectedQuote(end),
        });
    }
    let q_ctx = super::Context {
        text_in_markers: true,
        ..handler.ctx.clone()
    };
    Some(Open {
        scope: Some(Rc::new(q_ctx)),
        close: Close::Capture(CaptureEnd::Quote),
    })
}

/// Write the quotation marks around `content`, the text of the children of a `q`.
pub fn close_q(content: &str, output: &mut String, handler: super::HandlerContext<'_>) {
    // ~keep Where the white space of the source is kept, no space moves out of the marks.
    let (prefix, suffix, trimmed) =
        if crate::converter::utility::content::keeps_source_white_space(handler.options, handler.ctx) {
            ("", "", content.trim())
        } else {
            chomp_inline(content)
        };
    if trimmed.is_empty() {
        // ~keep issue #481: `<q>` wraps in quotes, which a whitespace-only body has
        // ~keep nothing to wrap -- but the body is still the word separator the source
        // ~keep wrote, and dropping it outright joined the words either side together.
        if !content.is_empty() {
            crate::converter::inline::wrapped::emit_whitespace_only_inline_body(content, output);
        }
        return;
    }
    if !handler.ctx.convert_as_inline && content.contains("\n\n") {
        let quoted = format!("\"{trimmed}\"");
        crate::converter::block::div::push_block(output, handler.options, handler.ctx, &quoted);
        return;
    }
    crate::converter::utility::content::push_inline_prefix(output, prefix, handler.options, handler.ctx);
    output.push('"');
    output.push_str(trimmed);
    output.push('"');
    output.push_str(suffix);
}

/// What `close_collected_q` needs from `open_q`: the count of collected links and the place of
/// the opening mark.
pub struct CollectedQuoteEnd {
    link_count: usize,
    start: usize,
}

/// End a `q` whose children wrote to the buffer of the element.
pub fn close_collected_q(end: &CollectedQuoteEnd, output: &mut String, handler: super::HandlerContext<'_>) {
    let Some(links) = handler.ctx.inline_code_links.as_ref() else {
        return;
    };
    let CollectedQuoteEnd { link_count, start } = *end;
    if links.borrow().len() == link_count {
        let body = output[start + 1..].to_string();
        output.truncate(start);
        if body.trim().is_empty() {
            crate::converter::inline::wrapped::emit_whitespace_only_inline_body(&body, output);
        } else {
            output.push('"');
            output.push_str(body.trim());
            output.push('"');
        }
    } else {
        // ~keep Quoted links were collected in this buffer; preserve their offsets.
        output.push('"');
    }
}

/// Dispatcher for semantic inline attribute elements.
///
/// Routes `<cite>`, `<q>`, `<abbr>`, `<dfn>`, `<time>`, and `<data>` elements
/// to their respective handlers.
pub fn handle(
    tag_name: &str,
    node_handle: &tl::NodeHandle,
    parser: &tl::Parser,
    output: &mut String,
    handler: super::HandlerContext<'_>,
) {
    match tag_name {
        "dfn" => handle_dfn(tag_name, node_handle, parser, output, handler),
        "abbr" => handle_abbr(tag_name, node_handle, parser, output, handler),
        "time" | "data" => handle_time_data(tag_name, node_handle, parser, output, handler),
        "cite" => handle_cite(tag_name, node_handle, parser, output, handler),
        _ => {}
    }
}

/// Appends inline suffix to the output.
///
/// This is a placeholder for integrating with other inline formatting systems
/// (e.g., footnote references). For now, it simply outputs the suffix.
fn append_inline_suffix(
    output: &mut String,
    suffix: &str,
    _is_nonempty: bool,
    _node_handle: &tl::NodeHandle,
    _parser: &tl::Parser,
    _dom_ctx: &super::DomContext,
) {
    output.push_str(suffix);
}