htoml

HTML page templates described with a toml config
Log | Files | Refs | README

commit 115667819b4832baa25afd1b26ae9010e552db95
parent 44e9ff0eed1ac0ff35c7040b66ac353dc29149c0
Author: Jackson G. Kaindume <seestem@merely.tech>
Date:   Wed, 10 Aug 2022 15:14:19 +0200

add command line interface

Diffstat:
Msrc/main.rs | 44+++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 43 insertions(+), 1 deletion(-)

diff --git a/src/main.rs b/src/main.rs @@ -1,5 +1,47 @@ +use htoml::Htoml; +use std::fs::File; +use std::io::{prelude::*, BufReader, BufWriter}; +use std::{ + env, + io::Read, + path::{Path, PathBuf}, +}; + pub mod common; pub mod htoml; pub mod sections; -fn main() {} +fn main() -> std::io::Result<()> { + let mut htoml_files: Vec<PathBuf> = vec![]; + + for p in env::args().skip(1) { + let path = PathBuf::from(p); + + if Path::exists(path.as_path()) { + htoml_files.push(path) + } else { + panic!("Path: {:?} does not exist", path); + } + } + + generate_html(htoml_files)?; + Ok(()) +} + +/// Generate HTML files +fn generate_html(htoml_files: Vec<PathBuf>) -> std::io::Result<()> { + for path in htoml_files { + let f = File::open(path)?; + let mut reader = BufReader::new(f); + let mut htoml_str = String::new(); + reader.read_to_string(&mut htoml_str)?; + let htoml: Htoml = toml::from_str(&htoml_str)?; + + let html = htoml.export(); + let f = File::create(htoml.output)?; + let mut writer = BufWriter::new(f); + writer.write_all(html.as_bytes())?; + } + + Ok(()) +}