]> git.proxmox.com Git - rustc.git/blame - vendor/html5ever/examples/html2html.rs
New upstream version 1.42.0+dfsg0+pve1
[rustc.git] / vendor / html5ever / examples / html2html.rs
CommitLineData
83c7162d
XL
1// Copyright 2014-2017 The html5ever Project Developers. See the
2// COPYRIGHT file at the top-level directory of this distribution.
3//
4// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
5// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
7// option. This file may not be copied, modified, or distributed
8// except according to those terms.
9
10//! Parse and re-serialize a HTML5 document.
11//!
12//! This is meant to produce the exact same output (ignoring stderr) as
13//!
14//! java -classpath htmlparser-1.4.jar nu.validator.htmlparser.tools.HTML2HTML
15//!
16//! where htmlparser-1.4.jar comes from http://about.validator.nu/htmlparser/
17
18extern crate html5ever;
19
83c7162d 20use std::default::Default;
dc9dc135 21use std::io::{self, Write};
83c7162d 22
83c7162d
XL
23use html5ever::driver::ParseOpts;
24use html5ever::rcdom::RcDom;
25use html5ever::tendril::TendrilSink;
26use html5ever::tree_builder::TreeBuilderOpts;
dc9dc135 27use html5ever::{parse_document, serialize};
83c7162d
XL
28
29fn main() {
30 let opts = ParseOpts {
31 tree_builder: TreeBuilderOpts {
32 drop_doctype: true,
33 ..Default::default()
34 },
35 ..Default::default()
36 };
37 let stdin = io::stdin();
38 let dom = parse_document(RcDom::default(), opts)
39 .from_utf8()
40 .read_from(&mut stdin.lock())
41 .unwrap();
42
43 // The validator.nu HTML2HTML always prints a doctype at the very beginning.
dc9dc135
XL
44 io::stdout()
45 .write_all(b"<!DOCTYPE html>\n")
46 .ok()
47 .expect("writing DOCTYPE failed");
83c7162d 48 serialize(&mut io::stdout(), &dom.document, Default::default())
dc9dc135
XL
49 .ok()
50 .expect("serialization failed");
83c7162d 51}