Struct Builder

Source
pub struct Builder<'a> { /* private fields */ }

Implementations§

Source§

impl<'a> Builder<'a>

Source

pub fn new() -> Self

Create a new Builder with default values.

Source

pub fn parse_owned<P: AsRef<Path>>( self, file_path: P, data: &str, ) -> Result<OwnedTree>

Parse an HTML file with templating expressions and statements and return an owned version of the HTML tree.

If you don’t want an owned version, use parse.

§Errors

This function returns an error if an HTML element is malformed or if an expression or a statement cannot be parsed.

See the module documentation.

Source

pub fn parse<P: AsRef<Path>>(self, file_path: P, data: &str) -> Result<Tree<'_>>

Parse an HTML file with templating expressions and statements and return an HTML tree containing references to the original data string.

If you want an owned version, use parse_owned.

§Errors

This function returns an error if an HTML element is malformed or if an expression or a statement cannot be parsed.

See the module documentation.

Source

pub fn on_event<F: FnMut(ParseEvent, &mut Tree<'_>, TreeRefId) -> EventHandlerResult + 'a>( self, on_event: F, ) -> Self

Register a handler function called when an event is emitted. Only one event handler can be defined, if this function is called several times, only the last event handler will be used.

Care needs to be taken when intercepting the ParseEvent::BeforeElement event because the current element being parsed must not be removed because its children are not parsed yet, it will panic! If you want to remove the element, intercepting the ParseEvent::AfterElement is required.

ParseEvent::BeforeElement and ParseEvent::AfterElement will both be called when an empty element is encountered

§Example
use pochoir_parser::{Builder, ParseEvent};

let file_path = "index.html";
let source = r#"<div>Hello</div><main>a<p>Paragraph</p>b</main>"#;
let mut elements = vec![];

let _tree = Builder::new().on_event(|event, tree, id| {
    if event == ParseEvent::BeforeElement {
        elements.push(tree.get(id).name().unwrap().into_owned());
    }

    Ok(())
}).parse(file_path, source);

assert_eq!(elements, vec!["div".to_string(), "main".to_string(), "p".to_string()]);

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 Default for Builder<'_>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Builder<'a>

§

impl<'a> !RefUnwindSafe for Builder<'a>

§

impl<'a> !Send for Builder<'a>

§

impl<'a> !Sync for Builder<'a>

§

impl<'a> Unpin for Builder<'a>

§

impl<'a> !UnwindSafe for Builder<'a>

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.