Skip to content
This repository has been archived by the owner on Oct 10, 2024. It is now read-only.

Commit

Permalink
Add relative paths, better errors
Browse files Browse the repository at this point in the history
  • Loading branch information
emmyoh committed Sep 10, 2022
1 parent 55e6b2a commit 825454c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ actix-files = "0.6.2"
actix-http = "3.2.1"
actix-service = "2.0.2"
actix-web = "4.1.0"
anyhow = "1.0.64"
chrono = { version = "0.4.22", features = ["alloc", "std", "clock", "serde", "unstable-locales"] }
clap = { version = "3.2.20", features = ["cargo", "suggestions", "color", "wrap_help", "unicode"] }
derive_more = { version = "0.99.17", features = ["default", "convert_case", "generate-parsing-rs", "testing-helpers", "nightly", "peg", "rustc_version" ] }
Expand All @@ -53,6 +52,7 @@ lazy_static = "1.4.0"
liquid = "0.26.0"
liquid-core = "0.26.0"
liquid-lib = { version = "0.26.0", features = ["all", "stdlib", "jekyll", "shopify", "extra"] }
miette = { version = "5.3.1", features = ["fancy"] }
mimalloc = { version = "0.1.29", default-features = false }
notify = "5.0.0"
pathdiff = "0.2.1"
Expand Down
14 changes: 9 additions & 5 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ lib.rs - Handling Mokk Files (.mokkf)
File:
Term for a document or page written in accordance to the Mokk specification
*/
use anyhow::Context;
use chrono::DateTime;
use derive_more::{Constructor, Div, Error, From, Into, Mul, Rem, Shl, Shr};
use liquid::*;
use miette::{IntoDiagnostic, WrapErr};
use pulldown_cmark::{html, Options, Parser};
use relative_path::RelativePath;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -426,7 +426,8 @@ pub fn render(
true => {
let template = create_liquid_parser()
.parse(text_to_render)
.with_context(|| {
.into_diagnostic()
.wrap_err_with(|| {
format!(
"Could not parse the Mokk file at {}/{}.mokkf",
page.directory, page.name
Expand All @@ -435,7 +436,8 @@ pub fn render(
.unwrap();
template
.render(&get_contexts(page, collections))
.with_context(|| {
.into_diagnostic()
.wrap_err_with(|| {
format!(
"Could not render the Mokk file at {}/{}.mokkf",
page.directory, page.name
Expand All @@ -446,7 +448,8 @@ pub fn render(
false => {
let template = create_liquid_parser()
.parse(text_to_render)
.with_context(|| {
.into_diagnostic()
.wrap_err_with(|| {
format!(
"Could not parse the Mokk file at {}/{}.mokkf",
page.directory, page.name
Expand All @@ -455,7 +458,8 @@ pub fn render(
.unwrap();
let liquid_render = template
.render(&get_contexts(page, collections))
.with_context(|| {
.into_diagnostic()
.wrap_err_with(|| {
format!(
"Could not render the Mokk file at {}/{}.mokkf",
page.directory, page.name
Expand Down
32 changes: 21 additions & 11 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ use actix_web::{
dev::{ServiceRequest, ServiceResponse},
HttpServer,
};
use anyhow::Context;
use clap::{arg, crate_version, ArgMatches, Command};
use glob::glob;
use lazy_static::lazy_static;
use miette::{miette, IntoDiagnostic, WrapErr};
use mimalloc::MiMalloc;
use notify::{Config, RecommendedWatcher, RecursiveMode, Watcher};
use std::collections::HashMap;
Expand Down Expand Up @@ -165,12 +165,17 @@ async fn serve_mokk(matches: &clap::ArgMatches) {
/// * `PATH` - Path to a Mokk (required)
#[inline(always)]
async fn host(matches: &clap::ArgMatches) {
let path = matches
.value_of("PATH")
.with_context(|| "No path to a Mokk was given".to_string())
.unwrap();
let path_buf = std::fs::canonicalize(
matches
.value_of("PATH")
.ok_or(miette!("No path to a Mokk was given"))
.unwrap(),
)
.unwrap();
let path = path_buf.to_str().unwrap();
env::set_current_dir(path)
.with_context(|| format!("Could not read a Mokk at {}", path))
.into_diagnostic()
.wrap_err_with(|| format!("Could not read a Mokk at {}", path))
.unwrap();
let port = matches.value_of("PORT").unwrap();
HttpServer::new(|| match Path::new("./output/index.html").is_file() {
Expand Down Expand Up @@ -254,18 +259,23 @@ fn build(matches: &clap::ArgMatches) -> HashMap<String, Vec<dokkoo::Page>> {
let lock = stdout.lock();
let mut buf_out = BufWriter::new(lock);

let path = matches
.value_of("PATH")
.with_context(|| "No path to a Mokk was given".to_string())
.unwrap();
let path_buf = std::fs::canonicalize(
matches
.value_of("PATH")
.ok_or(miette!("No path to a Mokk was given"))
.unwrap(),
)
.unwrap();
let path = path_buf.to_str().unwrap();
let mut collections: HashMap<String, Vec<dokkoo::Page>> = HashMap::new(); // Collections store

// Sort files into vectors of path buffers; for when we compile root files last
let mut root_files: Vec<PathBuf> = vec![];
let mut files: Vec<PathBuf> = vec![];

env::set_current_dir(path)
.with_context(|| format!("Could not read a Mokk at {}", path))
.into_diagnostic()
.wrap_err_with(|| format!("Could not read a Mokk at {}", path))
.unwrap(); // Set working directory to one passed to subcommand

for entry in glob(&format!("{}/*/*.mokkf", path)).unwrap() {
Expand Down

0 comments on commit 825454c

Please sign in to comment.