#![deny(warnings)]
use std::{
fs,
io::{self, Read},
};
use clap::Parser;
#[derive(Parser)]
#[command(name = "h2md")]
struct Cli {
input: Option<String>,
#[arg(short = 'o')]
output: Option<String>,
#[arg(short = 'c', long = "compress")]
compressed: bool,
}
fn main() -> Result<(), h2md::Error> {
let cli = Cli::parse();
let html: Vec<u8> = if let Some(ref path) = cli.input {
fs::read(path)?
} else {
let mut buf = Vec::new();
io::stdin().read_to_end(&mut buf)?;
buf
};
if html.is_empty() {
return Ok(());
}
let opts = h2md::Options {
compressed: cli.compressed,
};
if let Some(ref path) = cli.output {
let mut file = fs::File::create(path)?;
h2md::convert_with(&html, &mut file, &opts)?;
} else {
let stdout = io::stdout();
let mut handle = stdout.lock();
h2md::convert_with(&html, &mut handle, &opts)?;
}
Ok(())
}