Crates einbinden:
cargo add html5ever@\* markup5ever@\* tendril@\*
``` use { html5ever::{ parse_fragment, rcdom::RcDom, serialize::{AttrRef, Serialize, Serializer, TraversalScope}, QualName, }, markup5ever::{ namespace_url, ns, LocalName, }, std::io, tendril::TendrilSink, };
struct SimpleSerializer;
impl Serializer for SimpleSerializer {
/// Serialize the start of an element, for example <div class="test">
.
fn start_elem<'a, AttrIter>(&mut self, name: QualName, attrs: AttrIter) -> io::Result<()>
where
AttrIter: Iterator
/// Serialize the end of an element, for example `</div>`.
fn end_elem(&mut self, name: QualName) -> io::Result<()> {
println!("close: </{}>", &name.local);
Ok(())
}
/// Serialize a plain text node.
fn write_text(&mut self, text: &str) -> io::Result<()> {
println!("text: '{}'", text.escape_default());
Ok(())
}
/// Serialize a comment node, for example `<!-- comment -->`.
fn write_comment(&mut self, _text: &str) -> io::Result<()> {
Ok(())
}
/// Serialize a doctype node, for example `<!doctype html>`.
fn write_doctype(&mut self, _name: &str) -> io::Result<()> {
Ok(())
}
/// Serialize a processing instruction node, for example
/// `<?xml-stylesheet type="text/xsl" href="style.xsl"?>`.
fn write_processing_instruction(&mut self, _target: &str, _data: &str) -> io::Result<()> {
Ok(())
}
}
fn main() { let html = r#"
import Text.HTML.TagSoup
main :: IO ()
main = print $ parseTags tags
okay
"#; let dom = parse_fragment( RcDom::default(), // sink Default::default(), // opts QualName::new(None, ns!(html), LocalName::from("body")), // context_name vec![], // context_attrs ) .one(html);
// the first child is <html>
dom.document.children.borrow()[0].serialize(
&mut SimpleSerializer,
TraversalScope::ChildrenOnly(None),
).unwrap();
} ```