finish code cleanup

This commit is contained in:
KitsuneCafe 2024-02-04 06:38:11 -05:00
parent 8ac7254c01
commit 7298594f6e
4 changed files with 52 additions and 128 deletions

22
Cargo.lock generated
View file

@ -422,9 +422,9 @@ dependencies = [
[[package]]
name = "iana-time-zone"
version = "0.1.59"
version = "0.1.60"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b6a67363e2aa4443928ce15e57ebae94fd8949958fd1223c4cfc0cd473ad7539"
checksum = "e7ffbb5a1b541ea2561f8c41c087286cc091e21e556a4f09a8f6cbf17b69b141"
dependencies = [
"android_system_properties",
"core-foundation-sys",
@ -531,9 +531,9 @@ checksum = "523dc4f511e55ab87b694dc30d0f820d60906ef06413f93d4d7a1385599cc149"
[[package]]
name = "miniz_oxide"
version = "0.7.1"
version = "0.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e7810e0be55b428ada41041c41f32c9f1a42817901b4ccf45fa3d4b6561e74c7"
checksum = "9d811f3e15f28568be3407c8e7fdb6514c1cda3cb30683f15b6a1a1dc4ea14a7"
dependencies = [
"adler",
]
@ -835,12 +835,12 @@ dependencies = [
[[package]]
name = "roxy_core"
version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-core.git#8121a30b0c306ac5e5b48dcee5ea0b7ec12003df"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-core.git#3059a3b77492be751cba36d012557bd9622c62a7"
[[package]]
name = "roxy_markdown_parser"
version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-markdown-parser.git#221f1156700d839a366ed15a6e3090d77a3ad29b"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-markdown-parser.git#64f90e1897f1e3c823901de9db4fd1d7b805ad3e"
dependencies = [
"pulldown-cmark",
"roxy_core",
@ -849,7 +849,7 @@ dependencies = [
[[package]]
name = "roxy_markdown_tera_rewriter"
version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-markdown-tera-rewriter.git#5c33c0c2625c239694c8b7bb16f5da902cfd5e44"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-markdown-tera-rewriter.git#54cfd04c53f06679f6cb87abb3c11f4b09f673fa"
dependencies = [
"once_cell",
"regex",
@ -859,7 +859,7 @@ dependencies = [
[[package]]
name = "roxy_syntect"
version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-syntect.git#0cdfa876f9ca13be475dfac77b0d37ebb7d45da3"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-syntect.git#39c3a9e4b2f294936df5141b717efcbbe22deea7"
dependencies = [
"once_cell",
"regex",
@ -870,7 +870,7 @@ dependencies = [
[[package]]
name = "roxy_tera_parser"
version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-tera-parser.git#d8bd4aa1f0bbe4f9982ae17711ed69fead71c327"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-tera-parser.git#d7a364b1af1c2ec400d951fcd41560f929feb12c"
dependencies = [
"once_cell",
"regex",
@ -1048,9 +1048,9 @@ dependencies = [
[[package]]
name = "time"
version = "0.3.32"
version = "0.3.34"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fe80ced77cbfb4cb91a94bf72b378b4b6791a0d9b7f09d0be747d1bdff4e68bd"
checksum = "c8248b6521bb14bc45b4067159b9b6ad792e2d6d754d6c41fb50e29fefe38749"
dependencies = [
"deranged",
"itoa",

View file

@ -1,72 +0,0 @@
use std::{
fmt::Display,
io::{Error as IOError, ErrorKind},
path::StripPrefixError,
};
use glob::PatternError;
#[derive(Debug)]
pub struct Error<'a> {
message: String,
source: Option<&'a dyn std::error::Error>,
}
impl<'a> Display for Error<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", &self.message)?;
if let Some(source) = self.source {
write!(f, " ({})", &source)?;
}
Ok(())
}
}
impl<'a> std::error::Error for Error<'a> {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
self.source
}
}
impl<'a> From<String> for Error<'a> {
fn from(value: String) -> Self {
Self {
message: value,
source: None,
}
}
}
impl<'a> From<PatternError> for Error<'a> {
fn from(value: PatternError) -> Self {
Self {
message: value.to_string(),
source: Some(&value),
}
}
}
impl<'a> From<StripPrefixError> for Error<'a> {
fn from(value: StripPrefixError) -> Self {
Self {
message: value.to_string(),
source: Some(&value),
}
}
}
impl<'a> From<Error<'a>> for IOError {
fn from(value: Error) -> Self {
IOError::new(ErrorKind::Other, value.message)
}
}
impl<'a> From<&dyn std::error::Error> for Error<'a> {
fn from(value: &dyn std::error::Error) -> Self {
Self {
message: value.to_string(),
source: Some(&value),
}
}
}

View file

@ -1,6 +1,5 @@
use std::path::{PathBuf, Path, StripPrefixError};
use crate::error::Error;
use roxy_core::error::Error;
#[derive(Debug, Clone)]
pub(crate) struct FilePath<'a, P: AsRef<Path>> {

View file

@ -1,12 +1,11 @@
pub mod config;
pub mod context;
pub mod error;
mod file_path;
use config::Config;
use context::Context;
use error::Error;
use file_path::FilePath;
use roxy_core::error::Error;
use roxy_markdown_parser::MarkdownParser;
use roxy_markdown_tera_rewriter::{MarkdownTeraPreformatter, MarkdownTeraRewriter};
@ -26,6 +25,10 @@ use roxy_core::roxy::{Parser, Roxy};
const DEFAULT_THEME: &'static str = "base16-ocean.dark";
fn handle_err<E: std::error::Error + 'static>(err: E) -> Error {
Error::new(err.to_string(), err)
}
#[derive(Clap)]
#[command(name = "Roxy")]
#[command(author = "KitsuneCafe")]
@ -42,7 +45,8 @@ fn get_files<P: AsRef<Path> + std::fmt::Debug>(path: &P) -> Result<Vec<PathBuf>,
.to_str()
.ok_or_else(|| Error::from(format!("{path:?} is not a valid path.")))?;
let files: Vec<PathBuf> = glob(path)?
let files: Vec<PathBuf> = glob(path)
.map_err(handle_err)?
.filter_map(|x| x.ok())
.filter(|f| Path::is_file(f))
.collect();
@ -64,7 +68,7 @@ fn load_config(path: &Path) -> Config {
fn context_from_meta_files<'a, T: AsRef<Path>>(
files: &Vec<&PathBuf>,
file_path: &'a FilePath<T>,
) -> Result<Context, Error<'a>> {
) -> Result<Context, Error> {
let mut context = Context::new();
for path in files {
@ -72,49 +76,18 @@ fn context_from_meta_files<'a, T: AsRef<Path>>(
let mut file = File::open(path).map(BufReader::new)?;
file.read_to_end(&mut buf)?;
let mut str = String::from_utf8(buf)?;
let toml: Table = toml::from_str(&mut str)?;
let mut str = String::from_utf8(buf).map_err(handle_err)?;
let toml: Table = toml::from_str(&mut str).map_err(handle_err)?;
context.insert(&file_path.strip_root(path)?, &tera::to_value(toml)?);
context.insert(
&file_path.strip_root(path)?,
&tera::to_value(toml).map_err(handle_err)?,
);
}
Ok(context)
}
fn create_parser<'a, T: AsRef<Path>>(
file: &Path,
file_path: &FilePath<T>,
context: &'a Context,
theme: &str,
) -> Result<Parser<'a>, Error<'a>> {
let mut parser = Parser::new();
let mut preformatter = MarkdownTeraPreformatter::new();
parser.push(&mut preformatter);
let mut syntect = SyntectParser::new(theme);
parser.push(&mut syntect);
let mut md = MarkdownParser::new();
parser.push(&mut md);
let mut rewriter = MarkdownTeraRewriter::new();
parser.push(&mut rewriter);
let file_name = file.with_extension("html");
let output_path = file_path.to_output(&file_name)?;
if let Ok(path) = &file_path.strip_root(&file_name) {
if let Some(current_context) = context.get(path) {
context.insert(&"this", &current_context.clone());
}
}
let mut tera = tera::Tera::default();
let mut html = TeraParser::new(&mut tera, TeraParserOptions::default());
html.add_context(context.to_inner());
Ok(parser)
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let opts = Options::parse();
@ -131,11 +104,35 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
let theme = config.theme.unwrap_or(DEFAULT_THEME.to_string());
for file in files {
let mut parser = create_parser(file, &file_path, &context, &theme)?;
let file_name = file.with_extension("html");
let output_path = file_path.to_output(&file_name)?;
let mut parser = Parser::new();
let mut preformatter = MarkdownTeraPreformatter::new();
parser.push(&mut preformatter);
let mut syntect = SyntectParser::new(theme.as_str());
parser.push(&mut syntect);
let mut md = MarkdownParser::new();
parser.push(&mut md);
let mut rewriter = MarkdownTeraRewriter::new();
parser.push(&mut rewriter);
let file_name = file.with_extension("html");
if let Ok(path) = &file_path.strip_root(&file_name) {
if let Some(current_context) = context.get(path) {
context.insert(&"this", &current_context.clone());
}
}
let mut tera = tera::Tera::default();
let mut html = TeraParser::new(&mut tera, TeraParserOptions::default());
html.add_context(context.to_inner());
parser.push(&mut html);
Roxy::process_file(&file, &output_path, &mut parser).unwrap();
}