Skip to content

Commit

Permalink
Allow a user template override
Browse files Browse the repository at this point in the history
  • Loading branch information
simoncozens committed Jul 17, 2024
1 parent 80a3542 commit 6392a17
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 13 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ brotli = "6.0.0"
lazy_static = "1.4.0"
zeno = "0.3.1"
homedir = "0.3.3"
walkdir = "2.5.0"
5 changes: 5 additions & 0 deletions src/bin/diffenator3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ struct Cli {
#[clap(long = "output", default_value = "out", requires = "html")]
output: String,

/// Directory for custom templates
#[clap(long = "templates", requires = "html")]
templates: Option<String>,

/// Location in design space, in the form axis=123,other=456
#[clap(long = "location")]
location: Option<String>,
Expand Down Expand Up @@ -261,6 +265,7 @@ fn do_html(cli: &Cli, font_a: &DFont, font_b: &DFont, diff: Map<String, serde_js
font_face_new,
font_style_old,
font_style_new,
cli.templates.as_ref(),
)
.unwrap_or_else(|err| die("rendering HTML", err));

Expand Down
44 changes: 31 additions & 13 deletions src/html.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use serde::Serialize;
use serde_json::json;
use std::{
collections::HashMap,
path::{Path, PathBuf},
};

use lazy_static::lazy_static;
use serde::Serialize;
use serde_json::json;
use tera::{Context, Tera};
use walkdir::WalkDir;

use crate::dfont::DFont;

Expand Down Expand Up @@ -96,14 +95,12 @@ impl CSSFontStyle {
}
}

lazy_static! {
pub static ref TEMPLATES: Tera = {
let homedir = create_user_home_templates_directory();
Tera::new(&format!("{}/*", homedir.to_str().unwrap())).unwrap_or_else(|e| {
println!("Problem parsing templates: {:?}", e);
std::process::exit(1)
})
};
fn template_engine() -> Tera {
let homedir = create_user_home_templates_directory();
Tera::new(&format!("{}/*", homedir.to_str().unwrap())).unwrap_or_else(|e| {
println!("Problem parsing templates: {:?}", e);
std::process::exit(1)
})
}

pub fn create_user_home_templates_directory() -> PathBuf {
Expand Down Expand Up @@ -163,8 +160,29 @@ pub fn render_output(
font_face_new: CSSFontFace,
font_style_old: CSSFontStyle,
font_style_new: CSSFontStyle,
user_templates: Option<&String>,
) -> Result<String, tera::Error> {
TEMPLATES.render(
let mut tera = template_engine();
if let Some(template_dir) = user_templates {
for entry in WalkDir::new(template_dir) {
if entry.as_ref().is_ok_and(|e| e.file_type().is_dir()) {
continue;
}
let path = entry.as_ref().unwrap().path();
if let Err(e) =
tera.add_template_file(path, path.strip_prefix(template_dir).unwrap().to_str())
{
println!("Problem adding template file: {:?}", e);
std::process::exit(1)
}
}
if let Err(e) = tera.build_inheritance_chains() {
println!("Problem building inheritance chains: {:?}", e);
std::process::exit(1)
}
}

tera.render(
"diffenator.html",
&Context::from_serialize(json!({
"diff": {
Expand Down

0 comments on commit 6392a17

Please sign in to comment.