Skip to main content

Markdown

Struct Markdown 

Source
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

Source

pub fn raw(&self) -> &str

The original document source, byte-for-byte.

Source

pub fn root_elements(&self) -> impl Iterator<Item = ElementRef<'_>> + '_

Iterate the top-level (root) elements of the document, in source order.

Source

pub fn root_count(&self) -> usize

Count of top-level elements.

Source

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);
Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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 name
  • attrs — 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 elements
  • selfClosingtrue for <tag/>, false for <tag>…</tag>
  • location{start: {line, offset}, end: {line, offset}}

Trait Implementations§

Source§

impl Clone for Markdown

Source§

fn clone(&self) -> Markdown

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Markdown

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Markdown

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Prints the original raw source byte-for-byte.

Source§

impl FromStr for Markdown

Source§

fn from_str(input: &str) -> Result<Self, Self::Err>

Equivalent to crate::parse. Lets callers use the standard "...".parse::<Markdown>() form.

Source§

type Err = ParseError

The associated error which can be returned from parsing.
Source§

impl PartialEq for Markdown

Source§

fn eq(&self, other: &Markdown) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl TryFrom<String> for Markdown

Source§

fn try_from(input: String) -> Result<Self, Self::Error>

Equivalent to crate::parse_owned. Lets callers reuse the standard conversion vocabulary for owned input.

Source§

type Error = ParseError

The type returned in the event of a conversion error.
Source§

impl Eq for Markdown

Source§

impl StructuralPartialEq for Markdown

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.