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"
|
name = "htmlextract"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
dependencies = [
|
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)",
|
"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)",
|
"structopt 0.2.15 (registry+https://github.com/rust-lang/crates.io-index)",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -13,3 +13,5 @@ opt-level = 'z'
|
|||||||
[dependencies]
|
[dependencies]
|
||||||
kuchiki = "0.7.2"
|
kuchiki = "0.7.2"
|
||||||
structopt = "0.2.15"
|
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;
|
mod opts;
|
||||||
|
|
||||||
|
use html5ever::serialize::{serialize, SerializeOpts};
|
||||||
|
use kuchiki::NodeRef;
|
||||||
|
use markup5ever::serialize::Serialize as MSerialize;
|
||||||
use opts::Opts;
|
use opts::Opts;
|
||||||
use std::error::Error;
|
use std::error::Error;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
@ -20,6 +23,13 @@ fn read_html<I: Read>(source: &mut I) -> io::Result<String> {
|
|||||||
Ok(html)
|
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>> {
|
fn main() -> Result<(), Box<Error>> {
|
||||||
let stdin = stdin();
|
let stdin = stdin();
|
||||||
let opt = Opts::from_args();
|
let opt = Opts::from_args();
|
||||||
@ -58,6 +68,7 @@ fn main() -> Result<(), Box<Error>> {
|
|||||||
//let text = text_node.as_text().unwrap().borrow();
|
//let text = text_node.as_text().unwrap().borrow();
|
||||||
|
|
||||||
if let Some(child) = as_node.first_child() {
|
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
|
//TODO: Convert Nodes to String, as this only works for Nodes containing plain text
|
||||||
if let Some(text) = child.as_text() {
|
if let Some(text) = child.as_text() {
|
||||||
// Prints:
|
// Prints:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user