]>
Commit | Line | Data |
---|---|---|
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 | ||
18 | extern crate html5ever; | |
19 | ||
83c7162d | 20 | use std::default::Default; |
dc9dc135 | 21 | use std::io::{self, Write}; |
83c7162d | 22 | |
83c7162d XL |
23 | use html5ever::driver::ParseOpts; |
24 | use html5ever::rcdom::RcDom; | |
25 | use html5ever::tendril::TendrilSink; | |
26 | use html5ever::tree_builder::TreeBuilderOpts; | |
dc9dc135 | 27 | use html5ever::{parse_document, serialize}; |
83c7162d XL |
28 | |
29 | fn 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 | } |