use clap::Parser;
use html_parser::{Dom, Result};
use std::{
fs::File,
io::{self, Read},
path::PathBuf,
};
#[derive(Debug, Parser)]
struct Opt {
#[arg(short, long)]
pretty_print: bool,
#[arg(short, long)]
debug: bool,
input: Option<PathBuf>,
}
fn main() -> Result<()> {
let opt = Opt::parse();
let mut content = String::with_capacity(100_000);
if let Some(path) = opt.input {
let mut file = File::open(path)?;
file.read_to_string(&mut content)?;
} else {
let stdin = io::stdin();
let mut handle = stdin.lock();
handle.read_to_string(&mut content)?;
};
let dom = Dom::parse(&content)?;
if opt.debug {
for error in &dom.errors {
println!("# {}", error);
}
}
if opt.pretty_print {
println!("{}", dom.to_json_pretty()?);
} else {
println!("{}", dom.to_json()?);
}
Ok(())
}