gemrendr 0.3.1

Turns Gemtext into idiomatic HTML
Documentation
//! gemrendr   Turns Gemtext into idiomatic HTML.
//! Copyright (C) 2025  AverageHelper
//!
//! This program is free software: you can redistribute it and/or modify
//! it under the terms of the GNU General Public License as published by
//! the Free Software Foundation, either version 3 of the License, or
//! (at your option) any later version.
//!
//! This program is distributed in the hope that it will be useful,
//! but WITHOUT ANY WARRANTY; without even the implied warranty of
//! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//! GNU General Public License for more details.
//!
//! You should have received a copy of the GNU General Public License
//! along with this program.  If not, see <https://www.gnu.org/licenses/>.

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();

	// Print the license file on request
	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();
	};

	// "Interactive" mode (some extra info for the user, using stderr so we don't mess with I/O)
	if in_file.is_terminal() {
		// GPL notice and directions
		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()
	}
}