Skip to main content

Document

Struct Document 

Source
pub struct Document<S = String> { /* private fields */ }
Expand description

An owned source buffer and a direct semantic AST.

Implementations§

Source§

impl<S: AsRef<str>> Document<S>

Source

pub fn parse(source: S, options: &ParseOptions) -> Result<Self, StreamError>

Parse an owned or borrowed UTF-8 buffer into an arena document.

§Errors

Returns stream validation errors; built-in Markdown parsing has no syntax errors.

Source

pub fn source(&self) -> &str

The immutable source buffer.

Source

pub fn nodes(&self) -> &[Node]

All nodes in stable arena order.

Source

pub fn root(&self) -> NodeId

The document root. A finished document always has exactly one root.

Source

pub fn node(&self, id: NodeId) -> Option<&Node>

Access a node by stable index.

Source

pub fn set_kind( &mut self, id: NodeId, kind: EventKind<'static>, ) -> Result<(), StreamError>

Replace a node’s semantic value while preserving the arena’s structure and provenance.

§Errors

Rejects invalid source ranges, end events, changing the root identity, and replacing a container with children by a leaf.

Source

pub fn text_mut(&mut self, id: NodeId) -> Result<&mut String, StreamError>

Edit the text of a text/code/HTML/opaque leaf. Source-backed values detach on first mutation.

§Errors

Returns StreamError::InvalidRange for an invalid node ID or a non-text node.

Source

pub fn events(&self) -> DocumentEvents<'_, S> ⓘ

Traverse without a heap-allocated traversal stack or cloned owned strings.

Source§

impl<S: AsRef<str>> Document<S>

Source

pub fn children(&self, id: NodeId) -> Children<'_> ⓘ

Direct child IDs in sibling order. Invalid IDs yield an empty iterator.

Source

pub fn descendants(&self, id: NodeId) -> Descendants<'_> ⓘ

Preorder node IDs in a subtree, including id. Invalid IDs yield an empty iterator.

Source

pub fn subtree_events(&self, id: NodeId) -> DocumentEvents<'_, S> ⓘ

A subtree’s events, without surrounding document events unless id is the root.

Source

pub fn text_content(&self, id: NodeId) -> String

Plain semantic text, including image alternative text, code and raw HTML.

Source

pub fn headings(&self, options: &HeadingOptions) -> Vec<HeadingEntry>

Extract headings and anchors using the HTML writer’s rules. Filtered headings still reserve their anchors.

Source

pub fn syntax(&self, id: NodeId) -> Option<NodeSyntax<'_>>

Optional authored syntax, inferred lazily from the original source unless explicitly overridden.

Source

pub fn set_syntax( &mut self, id: NodeId, syntax: Option<NodeSyntax<'static>>, ) -> Result<(), StreamError>

Override authored syntax. None disconnects the node from inferred source syntax.

§Errors

Rejects invalid node IDs, source ranges and metadata for an incompatible node kind.

Source

pub fn frontmatter_value(&self, id: NodeId) -> Option<&str>

Frontmatter’s body without its opening and closing fences. Edited bodies are returned directly.

Source

pub fn render_html( &self, options: &CompileOptions, output: &mut impl Write, ) -> Result<(), StreamError>

Render the whole document through the shared event HTML writer.

§Errors

Propagates output writer errors.

Source

pub fn render_node( &self, id: NodeId, options: &CompileOptions, output: &mut impl Write, ) -> Result<(), StreamError>

Render one subtree through the shared event HTML writer.

§Errors

Rejects invalid node IDs and propagates output writer errors.

Source

pub fn append_child( &mut self, parent: NodeId, kind: EventKind<'static>, ) -> Result<NodeId, StreamError>

Append a new semantic node. Its source provenance is empty; values may still reference the source buffer.

§Errors

Rejects invalid parents, document/end kinds, source ranges, or forbidden nested capture containers.

Source

pub fn detach(&mut self, id: NodeId) -> Result<(), StreamError>

Detach a subtree without moving its arena slots or invalidating IDs.

§Errors

Rejects invalid IDs and detaching the document root.

Source

pub fn attach( &mut self, parent: NodeId, id: NodeId, before: Option<NodeId>, ) -> Result<(), StreamError>

Move an existing subtree into a container, optionally before one of its children. No node or text allocation is required.

§Errors

Rejects cycles, invalid IDs/positions, root moves, and forbidden capture nesting before modifying the tree.

Source

pub fn extract_children( &mut self, parent: NodeId, ) -> Result<Vec<NodeId>, StreamError>

Detach all direct children, returning their stable IDs in order.

§Errors

Rejects invalid node IDs.

Source

pub fn replace_heading_section<T: AsRef<str>>( &mut self, parent: NodeId, heading_index: usize, replacement: &Document<T>, replace_heading: bool, remove_subsections: bool, ) -> Result<bool, StreamError>

Replace a heading section with another document’s root children, materializing their source-backed values into this document.

heading_index counts direct children, including separator nodes. With remove_subsections, the section extends through headings deeper than the selected heading. Detached original nodes retain their IDs and can still be inspected or reattached.

§Errors

Rejects invalid parent IDs. Returns false when the selected child is not a heading.

Trait Implementations§

Source§

impl<S: Clone> Clone for Document<S>

Source§

fn clone(&self) -> Self

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<S: Debug> Debug for Document<S>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<S> Freeze for Document<S>
where S: Freeze,

§

impl<S> RefUnwindSafe for Document<S>
where S: RefUnwindSafe,

§

impl<S> Send for Document<S>
where S: Send,

§

impl<S> Sync for Document<S>
where S: Sync,

§

impl<S> Unpin for Document<S>
where S: Unpin,

§

impl<S> UnsafeUnpin for Document<S>
where S: UnsafeUnpin,

§

impl<S> UnwindSafe for Document<S>
where S: UnwindSafe,

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.