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>
impl<S: AsRef<str>> Document<S>
Sourcepub fn parse(source: S, options: &ParseOptions) -> Result<Self, StreamError>
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.
Sourcepub fn root(&self) -> NodeId
pub fn root(&self) -> NodeId
The document root. A finished document always has exactly one root.
Sourcepub fn set_kind(
&mut self,
id: NodeId,
kind: EventKind<'static>,
) -> Result<(), StreamError>
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.
Sourcepub fn text_mut(&mut self, id: NodeId) -> Result<&mut String, StreamError>
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.
Sourcepub fn events(&self) -> DocumentEvents<'_, S> ⓘ
pub fn events(&self) -> DocumentEvents<'_, S> ⓘ
Traverse without a heap-allocated traversal stack or cloned owned strings.
Source§impl<S: AsRef<str>> Document<S>
impl<S: AsRef<str>> Document<S>
Sourcepub fn children(&self, id: NodeId) -> Children<'_> ⓘ
pub fn children(&self, id: NodeId) -> Children<'_> ⓘ
Direct child IDs in sibling order. Invalid IDs yield an empty iterator.
Sourcepub fn descendants(&self, id: NodeId) -> Descendants<'_> ⓘ
pub fn descendants(&self, id: NodeId) -> Descendants<'_> ⓘ
Preorder node IDs in a subtree, including id. Invalid IDs yield an empty iterator.
Sourcepub fn subtree_events(&self, id: NodeId) -> DocumentEvents<'_, S> ⓘ
pub fn subtree_events(&self, id: NodeId) -> DocumentEvents<'_, S> ⓘ
A subtree’s events, without surrounding document events unless id is the root.
Sourcepub fn text_content(&self, id: NodeId) -> String
pub fn text_content(&self, id: NodeId) -> String
Plain semantic text, including image alternative text, code and raw HTML.
Sourcepub fn headings(&self, options: &HeadingOptions) -> Vec<HeadingEntry>
pub fn headings(&self, options: &HeadingOptions) -> Vec<HeadingEntry>
Extract headings and anchors using the HTML writer’s rules. Filtered headings still reserve their anchors.
Sourcepub fn syntax(&self, id: NodeId) -> Option<NodeSyntax<'_>>
pub fn syntax(&self, id: NodeId) -> Option<NodeSyntax<'_>>
Optional authored syntax, inferred lazily from the original source unless explicitly overridden.
Sourcepub fn set_syntax(
&mut self,
id: NodeId,
syntax: Option<NodeSyntax<'static>>,
) -> Result<(), StreamError>
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.
Sourcepub fn frontmatter_value(&self, id: NodeId) -> Option<&str>
pub fn frontmatter_value(&self, id: NodeId) -> Option<&str>
Frontmatter’s body without its opening and closing fences. Edited bodies are returned directly.
Sourcepub fn render_html(
&self,
options: &CompileOptions,
output: &mut impl Write,
) -> Result<(), StreamError>
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.
Sourcepub fn render_node(
&self,
id: NodeId,
options: &CompileOptions,
output: &mut impl Write,
) -> Result<(), StreamError>
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.
Sourcepub fn append_child(
&mut self,
parent: NodeId,
kind: EventKind<'static>,
) -> Result<NodeId, StreamError>
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.
Sourcepub fn detach(&mut self, id: NodeId) -> Result<(), StreamError>
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.
Sourcepub fn attach(
&mut self,
parent: NodeId,
id: NodeId,
before: Option<NodeId>,
) -> Result<(), StreamError>
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.
Sourcepub fn extract_children(
&mut self,
parent: NodeId,
) -> Result<Vec<NodeId>, StreamError>
pub fn extract_children( &mut self, parent: NodeId, ) -> Result<Vec<NodeId>, StreamError>
Sourcepub 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>
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.