pochoir-parser 0.12.2

HTML parser for the pochoir template engine
Documentation

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 [TreeRef]s 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:

  • Insert nodes using [Tree::insert] by passing the ID of the parent node you have queried before and a hand-built [Node]
  • Change the node's text using [TreeRefMut::set_text]
  • Change the node's attributes (if it is an element) using [TreeRefMut::set_attr]
  • Replace the node with another using [TreeRefMut::replace_node]
  • Get the queried node parent using [TreeRef::parent]
  • etc.

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