#![cfg_attr(feature = "unstable", feature(plugin))]
#![cfg_attr(feature = "unstable", plugin(string_cache_plugin))]
extern crate html5ever;
#[macro_use]
extern crate string_cache;
extern crate tendril;
use std::io::{self, Read};
use std::iter::repeat;
use std::default::Default;
use std::string::String;
use tendril::{ByteTendril, ReadExt};
use html5ever::{parse, one_input};
use html5ever::rcdom::{Document, Doctype, Text, Comment, Element, RcDom, Handle};
fn walk(indent: usize, handle: Handle) {
let node = handle.borrow();
print!("{}", repeat(" ").take(indent).collect::<String>());
match node.node {
Document
=> println!("#Document"),
Doctype(ref name, ref public, ref system)
=> println!("<!DOCTYPE {} \"{}\" \"{}\">", *name, *public, *system),
Text(ref text)
=> println!("#text: {}", escape_default(text)),
Comment(ref text)
=> println!("<!-- {} -->", escape_default(text)),
Element(ref name, _, ref attrs) => {
assert!(name.ns == ns!(html));
print!("<{}", name.local);
for attr in attrs.iter() {
assert!(attr.name.ns == ns!(""));
print!(" {}=\"{}\"", attr.name.local, attr.value);
}
println!(">");
}
}
for child in node.children.iter() {
walk(indent+4, child.clone());
}
}
pub fn escape_default(s: &str) -> String {
s.chars().flat_map(|c| c.escape_default()).collect()
}
fn main() {
let mut input = ByteTendril::new();
io::stdin().read_to_tendril(&mut input).unwrap();
let input = input.try_reinterpret().unwrap();
let dom: RcDom = parse(one_input(input), Default::default());
walk(0, dom.document);
if !dom.errors.is_empty() {
println!("\nParse errors:");
for err in dom.errors.into_iter() {
println!(" {}", err);
}
}
}