pub struct Markdown { /* private fields */ }Expand description
A parsed markdown + embedded XML document.
Returned by crate::parse / crate::parse_fragment. Holds the
original source, the parsed element tree, and the byte ranges of any
XML trivia (comments, CDATA sections) the parser skipped — those ranges
are excluded from text() so consumers don’t see comment markers as
content.
Implementations§
Source§impl Markdown
impl Markdown
Sourcepub fn root_elements(&self) -> impl Iterator<Item = ElementRef<'_>> + '_
pub fn root_elements(&self) -> impl Iterator<Item = ElementRef<'_>> + '_
Iterate the top-level (root) elements of the document, in source order.
Sourcepub fn root_count(&self) -> usize
pub fn root_count(&self) -> usize
Count of top-level elements.
Sourcepub fn select(&self, sel: &Selector) -> impl Iterator<Item = ElementRef<'_>>
pub fn select(&self, sel: &Selector) -> impl Iterator<Item = ElementRef<'_>>
Query the document with a compiled selector.
Returns every matching element in source order. Each element appears at most once even when multiple compounds in a union would match it.
let doc = marxml::parse(r#"<task id="1"/><task id="2"/>"#)?;
let sel = marxml::Selector::parse("task")?;
let tasks: Vec<_> = doc.select(&sel).collect();
assert_eq!(tasks.len(), 2);Sourcepub fn update(&self, sel: &Selector, new_attrs: &[(&str, &str)]) -> String
pub fn update(&self, sel: &Selector, new_attrs: &[(&str, &str)]) -> String
Update or insert attributes on every element matching sel. Returns
the new raw document. The original Markdown is unchanged.
If an attribute name in new_attrs is already present on a matched
element, its value is replaced. Otherwise the attribute is appended
at the end of the element’s attribute list.
The rewritten opening tag uses canonical whitespace: a single space
between attributes, with the closing > (or />) attached. Authors
of pretty-printed source may notice spacing changes on touched tags.
Use crate::escape_attr when the value contains user-controlled
bytes — update escapes for you, but the helper documents that
intent at the call site.
§Panics
Panics when new_attrs contains an entry whose name is not a valid
XML name (see crate::is_valid_name) or repeats an earlier name.
Both conditions are programmer errors; use Self::try_update for
runtime-sourced attribute slices that may carry bad input.
Sourcepub fn replace_content(&self, sel: &Selector, new_body: &str) -> String
pub fn replace_content(&self, sel: &Selector, new_body: &str) -> String
Replace the inner content of every element matching sel with
new_body. Returns the new raw document.
Sourcepub fn replace_in(
&self,
sel: &Selector,
pattern: &Regex,
replacement: &str,
) -> String
pub fn replace_in( &self, sel: &Selector, pattern: &Regex, replacement: &str, ) -> String
Run a regex replace_all over the inner content of every element
matching sel. Returns the new raw document.
replacement is written verbatim; $1 / $name / ${name} are not
interpreted as capture references.
Sourcepub fn replace_text(&self, sel: &Selector, new_body: &str) -> String
pub fn replace_text(&self, sel: &Selector, new_body: &str) -> String
Like Self::replace_content, but new_body is run through
crate::escape_text before being spliced. Use this for replacement
strings sourced from untrusted text.
Sourcepub fn replace_text_in(
&self,
sel: &Selector,
pattern: &Regex,
replacement: &str,
) -> String
pub fn replace_text_in( &self, sel: &Selector, pattern: &Regex, replacement: &str, ) -> String
Like Self::replace_in, but replacement is run through
crate::escape_text before being spliced.
Sourcepub fn try_update(
&self,
sel: &Selector,
new_attrs: &[(&str, &str)],
) -> Result<MutationReport, MutateError>
pub fn try_update( &self, sel: &Selector, new_attrs: &[(&str, &str)], ) -> Result<MutationReport, MutateError>
Fallible variant of Self::update. Returns a crate::MutationReport
(with the rewritten document and applied/skipped counts) on success,
or a crate::MutateError on programmer error (invalid XML name or
duplicate key in new_attrs).
§Errors
See crate::MutateError.
Sourcepub fn replace_content_report(
&self,
sel: &Selector,
new_body: &str,
) -> MutationReport
pub fn replace_content_report( &self, sel: &Selector, new_body: &str, ) -> MutationReport
Like Self::replace_content but returns a crate::MutationReport
so callers can see how many matches were applied vs. skipped because
of overlap with an outer match. Never fails — the report carries the
rewritten output alongside the counts.
Sourcepub fn replace_in_report(
&self,
sel: &Selector,
pattern: &Regex,
replacement: &str,
) -> MutationReport
pub fn replace_in_report( &self, sel: &Selector, pattern: &Regex, replacement: &str, ) -> MutationReport
Like Self::replace_in but returns a crate::MutationReport.
Never fails — the report carries the rewritten output alongside the
applied/skipped counts.
Sourcepub fn to_xml(&self, opts: &SerializeOpts) -> String
pub fn to_xml(&self, opts: &SerializeOpts) -> String
Serialize the parsed XML elements back to a flat XML string.
Surrounding markdown text is dropped — this is just the structured
payload. Pass crate::SerializeOpts::pretty for indented multi-line output.
Sourcepub fn to_json(&self) -> Value
pub fn to_json(&self) -> Value
Serialize the element tree as a serde_json::Value.
Top-level result is an array of root elements. Each element is an object with these fields:
tag— element tag nameattrs— object of attribute key/value pairs (values decoded)text— direct text content of the element, with child-element markup excluded. Decoded entity references appear as their literal characters.children— array of recursively-serialized child elementsselfClosing—truefor<tag/>,falsefor<tag>…</tag>location—{start: {line, offset}, end: {line, offset}}