]>
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; | |
3dfed10e | 19 | extern crate markup5ever_rcdom as rcdom; |
83c7162d | 20 | |
83c7162d | 21 | use std::default::Default; |
dc9dc135 | 22 | use std::io::{self, Write}; |
83c7162d | 23 | |
83c7162d | 24 | use html5ever::driver::ParseOpts; |
83c7162d XL |
25 | use html5ever::tendril::TendrilSink; |
26 | use html5ever::tree_builder::TreeBuilderOpts; | |
dc9dc135 | 27 | use html5ever::{parse_document, serialize}; |
3dfed10e | 28 | use rcdom::{RcDom, SerializableHandle}; |
83c7162d XL |
29 | |
30 | fn main() { | |
31 | let opts = ParseOpts { | |
32 | tree_builder: TreeBuilderOpts { | |
33 | drop_doctype: true, | |
34 | ..Default::default() | |
35 | }, | |
36 | ..Default::default() | |
37 | }; | |
38 | let stdin = io::stdin(); | |
39 | let dom = parse_document(RcDom::default(), opts) | |
40 | .from_utf8() | |
41 | .read_from(&mut stdin.lock()) | |
42 | .unwrap(); | |
43 | ||
44 | // The validator.nu HTML2HTML always prints a doctype at the very beginning. | |
dc9dc135 XL |
45 | io::stdout() |
46 | .write_all(b"<!DOCTYPE html>\n") | |
47 | .ok() | |
48 | .expect("writing DOCTYPE failed"); | |
3dfed10e XL |
49 | let document: SerializableHandle = dom.document.clone().into(); |
50 | serialize(&mut io::stdout(), &document, Default::default()) | |
dc9dc135 XL |
51 | .ok() |
52 | .expect("serialization failed"); | |
83c7162d | 53 | } |