markdown.renderer.utils

index · markdown.renderer

Overview

Shared utility functions for markdown renderers. Used by static, streaming, and table_layout modules.

Functions

NameSignature
normalize_pathnormalize_path(path) -> normalized
resolve_image_pathresolve_image_path(href, base_path, image_resolver, hide_images) -> abs_path
image_layoutimage_layout(tss, width, class_attr, img_w, img_h, cell_w, cell_h) -> cols, pad_str, rows
get_block_indentget_block_indent(tss, style_key) -> indent
get_list_indent_per_levelget_list_indent_per_level(tss) -> indent
expand_tabsexpand_tabs(line, tabstop) -> expanded
clamp_display_widthclamp_display_width(text, width) -> clamped
clamp_display_width_measuredclamp_display_width_measured(text, width) -> clamped, visual_width
take_prefix_by_displaytake_prefix_by_display(text, max_width) -> prefix, char_count, display_width
wrap_contentwrap_content(content, width) -> lines
inline_elements_for_taginline_elements_for_tag(tag, attrs) -> elements
normalize_table_cell_contentnormalize_table_cell_content(content) -> normalized
render_thematic_breakrender_thematic_break(tss, width) -> rendered
compute_list_markercompute_list_marker(tss, depth, list, item_count, task_info) -> marker_text, marker_width
clip_content_with_rangesclip_content_with_ranges(content, max_width) -> clipped, width
build_table_border_charsbuild_table_border_chars(tss) -> b
build_table_border_linebuild_table_border_line(styled_border, b, col_widths, line_type) -> line
build_wrapped_cell_line_contentbuild_wrapped_cell_line_content(content, parts) -> line_content, line_width
render_bordered_blockrender_bordered_block(tss, style_base, lines, width, label, indent, pad) -> rendered
render_bordered_contentrender_bordered_content(tss, style_base, content, width, label, indent, pad) -> rendered
compute_div_layoutcompute_div_layout(tss, class, current_width) -> layout
render_blockquote_linesrender_blockquote_lines(tss, content) -> lines

normalize_path(path) -> normalized

Normalize a file path: resolve . and .. segments

resolve_image_path(href, base_path, image_resolver, hide_images) -> abs_path

Resolve an image href to a renderable PNG path, or return nil

Local hrefs are resolved against base_path and must point to an existing PNG file. Schemed hrefs (http://…, wiki:…, media:…) are delegated to the image_resolver callback when one is provided. The hide_images flag short-circuits everything.

image_layout(tss, width, class_attr, img_w, img_h, cell_w, cell_h) -> cols, pad_str, rows

Compute display layout for an inline PNG image

Resolves an image's display width in columns from its size class (small/medium in class_attr, else the default w), reading values from the image TSS object: fractions (<1) of the content width or absolute columns. When the terminal cell pixel width is known, the result is capped at the image's natural size so small images are never upscaled. Returns the column count, the left padding string that centers the image, and the estimated row count (true cell aspect ratio when both cell dimensions are known, 2:1 otherwise).

get_block_indent(tss, style_key) -> indent

Get block indentation from TSS for a style key

get_list_indent_per_level(tss) -> indent

Get indent columns per list nesting level from TSS

expand_tabs(line, tabstop) -> expanded

Expand tab characters to spaces

clamp_display_width(text, width) -> clamped

Clamp text to a maximum display width

clamp_display_width_measured(text, width) -> clamped, visual_width

Clamp text to a maximum display width, returning both clamped text and its visual width

take_prefix_by_display(text, max_width) -> prefix, char_count, display_width

Get a UTF-8 prefix that fits within a display width

wrap_content(content, width) -> lines

Wrap content with inline ranges to fit a display width

Word-wraps content.plain at width display columns, splitting content.ranges across the resulting lines. Returns an array of content tables {plain, ranges} — one per wrapped line — with range positions adjusted to each line's local coordinate system. Returns the original content in a single-element array when no wrapping is needed.

inline_elements_for_tag(tag, attrs) -> elements

Map an inline tag (with optional attrs) to TSS element names

normalize_table_cell_content(content) -> normalized

Normalize table cell content to {plain, ranges} form

render_thematic_break(tss, width) -> rendered

Render a thematic break and return the styled string with trailing newlines

compute_list_marker(tss, depth, list, item_count, task_info) -> marker_text, marker_width

Compute list marker text, width, and continuation indent

Computes the rendered list marker for a list item. list must have ordered (bool) and start (number) fields. task_info, when non-nil, must have a checked field. Returns the styled marker text (including base indent) and its display width.

clip_content_with_ranges(content, max_width) -> clipped, width

Clip content with inline ranges to a maximum display width

build_table_border_chars(tss) -> b

Build the 14-character table border map from TSS

build_table_border_line(styled_border, b, col_widths, line_type) -> line

Build a styled table border line (top, separator, or bottom)

line_type is one of "top", "separator", or "bottom". styled_border is a function that applies table.border styling to a string. b is the border char map from build_table_border_chars.

build_wrapped_cell_line_content(content, parts) -> line_content, line_width

Build wrapped cell line content with range remapping

Given the original cell content (with .plain and .ranges) and parts from table_layout.wrap_text_with_spans, builds a new content table for one wrapped line with ranges remapped to local coordinates. Returns {plain, ranges} and the display width.

render_bordered_block(tss, style_base, lines, width, label, indent, pad) -> rendered

Render a bordered block (code blocks) returning styled string

render_bordered_content(tss, style_base, content, width, label, indent, pad) -> rendered

Render pre-styled content with borders (for divs)

Unlike render_bordered_block, this takes already-rendered content with ANSI codes and wraps it in border characters.

compute_div_layout(tss, class, current_width) -> layout

Compute div layout parameters from TSS

Returns {style_base, el_width, pad, align} for a fenced div.

render_blockquote_lines(tss, content) -> lines

Render blockquote lines with bar prefix

Returns an array of bar-prefixed lines from captured blockquote content.