pub struct Builder<'a> { /* private fields */ }
Implementations§
Source§impl<'a> Builder<'a>
impl<'a> Builder<'a>
Sourcepub fn parse_owned<P: AsRef<Path>>(
self,
file_path: P,
data: &str,
) -> Result<OwnedTree>
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.
Sourcepub fn parse<P: AsRef<Path>>(self, file_path: P, data: &str) -> Result<Tree<'_>>
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.
Sourcepub fn on_event<F: FnMut(ParseEvent, &mut Tree<'_>, TreeRefId) -> EventHandlerResult + 'a>(
self,
on_event: F,
) -> Self
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()]);