mod args;
mod constants;
use ::clap::{CommandFactory, Parser, error::ErrorKind};
use ::gemrendr::{RenderOptions, html_from_gemtext};
use ::std::io::{IsTerminal, Write, read_to_string, stdin, stdout};
use args::Args;
use constants::{COPYRIGHT_YEAR, PKG_AUTHORS, PKG_NAME};
fn main() {
let args = Args::parse();
if args.license {
print!(include_str!("../LICENSE"));
return;
}
let in_file = if args.in_file.expect("clap ensures this").to_string_lossy() == "-" {
stdin()
} else {
Args::command()
.error(
ErrorKind::InvalidValue,
"Only stdin is supported. Use `-` for in_file",
)
.exit();
};
let mut out_file = if args.out_file.expect("clap ensures this").to_string_lossy() == "-" {
stdout()
} else {
Args::command()
.error(
ErrorKind::InvalidValue,
"Only stdin is supported. Use `-` for out_file",
)
.exit();
};
if in_file.is_terminal() {
eprintln!(
"{PKG_NAME} Copyright (C) {COPYRIGHT_YEAR} {PKG_AUTHORS}
This program comes with ABSOLUTELY NO WARRANTY. This is
free software, and you are welcome to redistribute it
under certain conditions; type `{PKG_NAME} --license' for details.
Enter Gemtext to parse; send EOF (Ctrl+D) to commit:
---",
);
}
let document = match read_to_string(in_file) {
Ok(d) => d,
Err(err) => Args::command()
.error(ErrorKind::Io, format!("{err}"))
.exit(),
};
let options = RenderOptions {
copy_button_style: args.copy_button_style,
empty_line_tag: args.empty_line_tag,
preamble: !args.no_preamble,
};
let html = html_from_gemtext(&document, options);
if let Err(err) = write!(out_file, "{html}") {
Args::command()
.error(ErrorKind::Io, format!("{err}"))
.exit()
}
}