Skip to main content

Builder

Struct Builder 

Source
pub struct Builder { /* private fields */ }
Expand description

Programmatic construction of a document — the write-path mirror of Document::parse. Build the tree bottom-up (add children, then the container, wiring them with Builder::set_children); every add* method returns the new node’s NodeId. Then render, serialize, query, or dump the subtree rooted at any id, on demand, without consuming the builder. All input strings are copied, so caller buffers need not outlive a call.

Implementations§

Source§

impl Builder

Source

pub fn new() -> Result<Self, Error>

Create an empty builder.

Source

pub fn add(&mut self, kind: VoidKind) -> Result<NodeId, Error>

Add a void-payload node (attach children later with Builder::set_children).

Source

pub fn add_text(&mut self, kind: TextKind, text: &str) -> Result<NodeId, Error>

Add a single-string-payload node (a str, code span, url, comment, …).

Source

pub fn add_heading(&mut self, level: u32) -> Result<NodeId, Error>

Add a heading of the given level (attach its inline children afterward).

Source

pub fn add_code_block( &mut self, lang: Option<&str>, text: &str, ) -> Result<NodeId, Error>

Add a code block, with an optional info-string language.

Source

pub fn add_raw_block( &mut self, format: &str, text: &str, ) -> Result<NodeId, Error>

Add a raw block targeting format (e.g. "html").

Source

pub fn add_metadata(&mut self, lang: &str, text: &str) -> Result<NodeId, Error>

Add a document-metadata block written in config language lang.

Source

pub fn add_raw_inline( &mut self, format: &str, text: &str, ) -> Result<NodeId, Error>

Add a raw inline targeting format.

Source

pub fn add_smart_punctuation( &mut self, kind: SmartPunctuation, text: &str, ) -> Result<NodeId, Error>

Add a smart-punctuation node standing for text (its source spelling).

Add a link with an optional destination and/or reference label (attach the link text as children).

Source

pub fn add_image( &mut self, destination: Option<&str>, reference: Option<&str>, ) -> Result<NodeId, Error>

Add an image — like Builder::add_link, but children are the alt text.

Source

pub fn add_directive( &mut self, form: DirectiveForm, name: &str, ) -> Result<NodeId, Error>

Add a generic directive of the given form and name.

Source

pub fn add_element(&mut self, name: &str) -> Result<NodeId, Error>

Add a generic named element (the escape hatch for HTML/XML tags).

Source

pub fn add_processing_instruction( &mut self, target: &str, data: &str, ) -> Result<NodeId, Error>

Add an XML processing instruction (<?target data?>).

Source

pub fn add_footnote(&mut self, label: &str) -> Result<NodeId, Error>

Add a footnote definition with the given label.

Source

pub fn add_reference( &mut self, label: &str, destination: &str, ) -> Result<NodeId, Error>

Add a link/image reference definition (labeldestination).

Source

pub fn add_bullet_list( &mut self, style: BulletStyle, tight: bool, ) -> Result<NodeId, Error>

Add a bullet list.

Source

pub fn add_ordered_list( &mut self, numbering: OrderedNumbering, delim: OrderedDelim, tight: bool, start: Option<u32>, ) -> Result<NodeId, Error>

Add an ordered list, with an optional explicit start number.

Source

pub fn add_task_list(&mut self, tight: bool) -> Result<NodeId, Error>

Add a task list.

Source

pub fn add_task_list_item(&mut self, checked: bool) -> Result<NodeId, Error>

Add a task-list item with the given checkbox state.

Source

pub fn add_row(&mut self, head: bool) -> Result<NodeId, Error>

Add a table row (head marks a header row).

Source

pub fn add_cell( &mut self, head: bool, alignment: Alignment, ) -> Result<NodeId, Error>

Add a table cell (head marks a header cell).

Source

pub fn set_children( &mut self, parent: NodeId, children: &[NodeId], ) -> Result<(), Error>

Set parent’s children to children (in order), replacing any it had. Each child id should appear in exactly one set_children call.

Source

pub fn set_attrs( &mut self, id: NodeId, attrs: &[(&str, Option<&str>)], ) -> Result<(), Error>

Attach {...} attributes to id ((key, Some(value)), or (key, None) for a bare attribute), replacing any it had. An empty slice clears them.

Source

pub fn render_html(&mut self, root: NodeId) -> Result<Vec<u8>, Error>

Render the subtree rooted at root to HTML (generic whole-vocabulary printer — a built tree has no djot/Markdown side tables).

Source

pub fn serialize( &mut self, root: NodeId, format: Format, ) -> Result<Vec<u8>, Error>

Serialize the subtree rooted at root to format’s source syntax. Returns Error::UnsupportedFormat when the target can’t represent the built tree (e.g. semantic kinds into XML).

Source

pub fn ast_json(&mut self, root: NodeId) -> Result<Vec<u8>, Error>

Encode the subtree rooted at root as pretty-printed JSON.

Source

pub fn query( &mut self, root: NodeId, selector: &str, ) -> Result<Vec<QueryMatch>, Error>

Resolve a selector against the subtree rooted at root (same grammar as Document::query).

Trait Implementations§

Source§

impl Debug for Builder

Source§

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

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

impl Drop for Builder

Source§

fn drop(&mut self)

Executes the destructor for this type. Read more
Source§

fn pin_drop(self: Pin<&mut Self>)

🔬This is a nightly-only experimental API. (pin_ergonomics)
Execute the destructor for this type, but different to Drop::drop, it requires self to be pinned. Read more

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> 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, 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.