/* * Copyright (c) 2023 Emma Tebibyte * SPDX-License-Identifier: AGPL-3.0-or-later * * This file is part of Plaque. * * Plaque is free software: you can redistribute it and/or modify it under the * terms of the GNU Affero General Public License as published by the Free * Software Foundation, either version 3 of the License, or (at your option) any * later version. * * Plaque 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 Affero General Public License for more * details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see https://www.gnu.org/licenses/. */ mod config; mod parse; use std::{ env::args, io::Write, fs::{ File, read_dir }, }; use config::*; use parse::*; use pulldown_cmark::{ Parser, Options, html }; use tl::Node; use yacexits::*; fn main() { let argv = args().collect::>(); let mut options = Options::empty(); options.insert(Options::all()); let config = Config::read_config( get_path("XDG_CONFIG_DIR").unwrap_or_else(|(err, code)| { eprintln!("{}: {}", argv[0], err); exit(code); }) ).unwrap_or_else(|(err, code)| { eprintln!("{}: {}", argv[0], err); exit(code); }); let mut files = get_contents( read_dir(&config.content_dir).unwrap_or_else(|_| { eprintln!( "{}: {}: No such file or directory.", argv[0], &config.content_dir ); exit(EX_UNAVAILABLE); }) ).unwrap_or_else(|(err, code)| { eprintln!("{}: {}", argv[0], err); exit(code); }); let mut templates = get_templates( read_dir(&config.template_dir).unwrap_or_else(|_| { eprintln!( "{}: {}: No such file or directory.", argv[0], &config.template_dir ); exit(EX_UNAVAILABLE); }) ).unwrap_or_else(|(err, code)| { eprintln!("{}: {}", argv[0], err); exit(code); }); for content_file in files.drain() { let (content_name, (md, toml)) = content_file; let mut output = String::new(); html::push_html(&mut output, Parser::new_ext(&md, options)); let meta_info = PageMeta::read_meta( toml.to_owned() ).unwrap_or_else(|(err, code)| { eprintln!("{}: {}", argv[0], err); exit(code); }); let dom = templates.get_mut(&meta_info.template).unwrap_or_else(|| { eprintln!( "{}: {}: No such template “{}”.", argv[0], content_name, meta_info.template, ); exit(EX_UNAVAILABLE); }).get_mut_ref(); let mut html_file = File::create(format!( "{}/{}.html", config.output_dir, meta_info.template, )).unwrap_or_else(|_| { eprintln!( "{}: {}: Unable to create output file.", argv[0], config.output_dir, ); exit(EX_OSERR); }); html_file.write(output.as_bytes()).unwrap_or_else(|err| { eprintln!("{}: {}", argv[0], err); exit(EX_SOFTWARE); }); } }