serialize to html instead of text [CI SKIP]
This commit is contained in:
parent
f07983db53
commit
5b1c58c659
2
Cargo.lock
generated
2
Cargo.lock
generated
@ -147,7 +147,9 @@ dependencies = [
|
||||
name = "htmlextract"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"html5ever 0.22.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"kuchiki 0.7.2 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"markup5ever 0.7.5 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
"structopt 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||
]
|
||||
|
||||
|
@ -13,3 +13,5 @@ opt-level = 'z'
|
||||
[dependencies]
|
||||
kuchiki = "0.7.2"
|
||||
structopt = "0.2.15"
|
||||
html5ever = "0.22.5"
|
||||
markup5ever = "0.7.5"
|
||||
|
11
src/main.rs
11
src/main.rs
@ -4,6 +4,9 @@ use structopt::StructOpt;
|
||||
|
||||
mod opts;
|
||||
|
||||
use html5ever::serialize::{serialize, SerializeOpts};
|
||||
use kuchiki::NodeRef;
|
||||
use markup5ever::serialize::Serialize as MSerialize;
|
||||
use opts::Opts;
|
||||
use std::error::Error;
|
||||
use std::fs::File;
|
||||
@ -20,6 +23,13 @@ fn read_html<I: Read>(source: &mut I) -> io::Result<String> {
|
||||
Ok(html)
|
||||
}
|
||||
|
||||
fn serialize_node<N: MSerialize>(node: &N) -> Result<String, Box<dyn Error>> {
|
||||
let mut bytes = Vec::new();
|
||||
serialize(&mut bytes, node, SerializeOpts::default())?;
|
||||
let s = String::from_utf8(bytes)?;
|
||||
Ok(s)
|
||||
}
|
||||
|
||||
fn main() -> Result<(), Box<Error>> {
|
||||
let stdin = stdin();
|
||||
let opt = Opts::from_args();
|
||||
@ -58,6 +68,7 @@ fn main() -> Result<(), Box<Error>> {
|
||||
//let text = text_node.as_text().unwrap().borrow();
|
||||
|
||||
if let Some(child) = as_node.first_child() {
|
||||
dbg!(serialize_node(&child));
|
||||
//TODO: Convert Nodes to String, as this only works for Nodes containing plain text
|
||||
if let Some(text) = child.as_text() {
|
||||
// Prints:
|
||||
|
Loading…
x
Reference in New Issue
Block a user