use clap::Parser;
use parse_hyperlinks::renderer::links2html_writer;
use parse_hyperlinks::renderer::text_links2html_writer;
use parse_hyperlinks::renderer::text_rawlinks2html_writer;
use std::fs::File;
use std::io;
use std::io::Read;
use std::io::Write;
use std::path::Path;
use std::path::PathBuf;
use std::process;
use std::sync::LazyLock;
#[derive(Debug, Eq, PartialEq, Parser)]
#[command(
version,
name = "atext2html",
about,
long_about = "Render source text with markup hyperlinks.",
disable_version_flag = true
)]
pub struct Args {
#[arg(long, short = 'r')]
pub render_links: bool,
#[arg(long, short = 'l')]
pub only_links: bool,
#[structopt(name = "FILE")]
pub inputs: Vec<PathBuf>,
#[arg(long, short = 'o')]
pub output: Option<PathBuf>,
#[arg(long, short = 'V')]
pub version: bool,
}
pub static ARGS: LazyLock<Args> = LazyLock::new(Args::parse);
const VERSION: Option<&'static str> = option_env!("CARGO_PKG_VERSION");
const AUTHOR: &str = "(c) Jens Getreu, 2020-2025";
fn main() -> Result<(), ::std::io::Error> {
if ARGS.version {
println!("Version {}, {}", VERSION.unwrap_or("unknown"), AUTHOR);
process::exit(0);
};
let renderer = match (ARGS.render_links, ARGS.only_links) {
(false, false) => |(inbuf, mut output): (&str, &mut dyn Write)| -> Result<_, _> {
text_rawlinks2html_writer(inbuf, &mut output)
},
(true, false) => |(inbuf, mut output): (&str, &mut dyn Write)| -> Result<_, _> {
text_links2html_writer(inbuf, &mut output)
},
(_, true) => |(inbuf, mut output): (&str, &mut dyn Write)| -> Result<_, _> {
links2html_writer(inbuf, &mut output)
},
};
let mut output = if let Some(outname) = &ARGS.output {
let file = File::create(Path::new(&outname))?;
Box::new(file) as Box<dyn Write>
} else {
Box::new(io::stdout()) as Box<dyn Write>
};
if (ARGS.inputs.is_empty()) || ((ARGS.inputs.len() == 1) && ARGS.inputs[0] == Path::new("-")) {
let mut inbuf = String::new();
Read::read_to_string(&mut io::stdin(), &mut inbuf)?;
renderer((&inbuf, &mut output))?;
} else {
for filename in ARGS.inputs.iter() {
let mut inbuf = String::new();
let mut file = File::open(filename)?;
Read::read_to_string(&mut file, &mut inbuf)?;
renderer((&inbuf, &mut output))?;
}
};
Ok(())
}