Crate pochoir_parser

Source
Expand description

An HTML parser supporting templating expressions and statements.

§Parsing HTML

To parse an HTML document without doing fancy stuff it is possible to use the helper functions parse or parse_owned.

use pochoir_parser::parse;

let _tree = parse("index.html", "<h1>Hello!</h1>");

The HTML parser can be customized by responding to special events defined in the ParseEvent enumeration. To do that, you need to make a Builder and use its on_event method to register a single event handler which will handle all events (it is not possible to define several event handlers). You could then match the event received and manipulate the tree using the two other arguments. Note that removing the element being parsed when handling the ParseEvent::BeforeElement event will panic because it will try to insert children to a parent that does not exist. It is not possible to skip parsing children of an element.

use pochoir_parser::{Builder, ParseEvent};

let _tree = Builder::new().on_event(|event, tree, id| {
    if event == ParseEvent::BeforeElement {
        let element_name = tree.get(id).name().unwrap().into_owned();
        println!("Element found: {element_name}");
    }

    Ok(())
}).parse("index.html", r#"<div>Hello</div><main>a<p>Paragraph</p>b</main>"#);

§Manipulating the tree

The tree is not manipulable by itself, you need to get references to some nodes as TreeRefs or TreeRefMut. You can use Tree::get and Tree::get_mut if you know the node’s ID but you can also query the tree using CSS selectors with the Tree::select and Tree::select_all methods. Finally, you can traverse the whole tree using Tree::traverse_breadth and Tree::traverse_depth.

You can then:

Check out Tree, TreeRef and TreeRefMut to learn more about what you can get or set.

Re-exports§

pub use error::Error;
pub use error::Result;
pub use node::Attr;
pub use node::Attrs;
pub use node::Node;
pub use node::ParsedNode;
pub use tree::OwnedTree;
pub use tree::Tree;
pub use tree::TreeRef;
pub use tree::TreeRefId;
pub use tree::TreeRefMut;

Modules§

error
node
Defines types used when parsing data.
tree

Structs§

Builder

Enums§

ParseEvent

Constants§

EMPTY_HTML_ELEMENTS
The list of HTML empty elements.

Functions§

parse
Parse an HTML file with templating expressions and statements and return an HTML tree containing references to the original data string.
parse_owned
Parse an HTML file with templating expressions and statements and return an owned version of the HTML tree.
render
Render a Tree to an HTML string.

Type Aliases§

EventHandlerResult