Skip to content

Commit

Permalink
Add year function and fix bug where lang IDs wouldn't be found sometimes
Browse files Browse the repository at this point in the history
  • Loading branch information
kosude committed May 31, 2024
1 parent 4a6df3e commit 0179f82
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 22 deletions.
3 changes: 3 additions & 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 @@ -5,6 +5,7 @@ description = "Quickly initialise files and folders with preconfigured boilerpla
edition = "2018"

[dependencies]
chrono = "0.4.38"
clap = { version = "4.4.18", features = ["derive"] }
colored = "2.1.0"
dirs = "5.0.1"
Expand Down
4 changes: 2 additions & 2 deletions examples/templates/file/copyright
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{%- if lang != "unknown" -%}
{%- set com = comment_by_lang(lang_id=lang) -%}
{{ com.0 }}
{{ com.1 }} Copyright (c) {{ year }} {{ copyright_holders }}.
{{ com.1 }} Copyright (c) {{ year() }} {{ copyright_holders }}.
{{ com.1 }} All Rights Reserved.
{{ com.1 }}
{{ com.1 }} See the LICENCE file for more information.
{{ com.2 }}
{%- else -%}
Copyright (c) {{ year }} {{ copyright_holders }}.
Copyright (c) {{ year() }} {{ copyright_holders }}.
All Rights Reserved.

See the LICENCE file for more information.
Expand Down
10 changes: 2 additions & 8 deletions src/templater/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use std::sync::{Arc, Mutex};

use tera::Tera;

use super::fn_decls;
use super::register_functions;

pub type ContextArcMutex = Arc<Mutex<Context>>;

Expand All @@ -21,13 +21,7 @@ pub struct Context {
impl Context {
pub fn new() -> Self {
let mut tera = Tera::default();

tera.register_function("licence", fn_decls::licence());
tera.register_function("lang_by_filename", fn_decls::lang_by_filename());
tera.register_function("comment_by_lang", fn_decls::comment_by_lang());

tera.register_filter("wrap", fn_decls::wrap());

register_functions(&mut tera);
Self { tera }
}

Expand Down
10 changes: 10 additions & 0 deletions src/templater/fn_decls.rs → src/templater/functions/fn_decls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,20 @@
* See the LICENCE file for more information.
*/

use chrono::Datelike;
use license::License;
use std::collections::HashMap;
use tera::{from_value, to_value, Filter, Function, Map, Result, Value};

use super::language_specifics;

function! {
/// Get the current year.
pub fn year() {
chrono::Utc::now().year()
}
}

function! {
/// Expand the specified licence by its SPDX code to it full form.
/// See https://spdx.org/licenses/ for more.
Expand All @@ -32,12 +40,14 @@ function! {
}

function! {
/// Get the associated language ID from the specified filename.
pub fn lang_by_filename(filename: String,) {
language_specifics::lang_id_from_filename(&filename).unwrap_or("unknown")
}
}

function! {
/// Get the comment style associated with the specified language id.
pub fn comment_by_lang(lang_id: String,) {
to_value(language_specifics::comment_style_from_lang(&lang_id)).unwrap_or("unknown".into())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ macro_rules! function {
) => {
$(#[$($attrss)*])*
pub fn $fname() -> impl Function {
#[allow(unused_variables)]
Box::new(|argv: &HashMap<String, Value>| -> Result<Value> {
$(
let $arg = get_arg_helper!(argv, $arg: $typ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,15 @@ pub fn lang_id_from_filename<S: AsRef<str>>(filename: S) -> Option<&'static str>
/// Get the associated language ID from the specified file extension.
fn from_ext<S: AsRef<str>>(ext: S) -> Option<&'static str> {
let ext = ext.as_ref().to_lowercase();
EXT_BY_LANG_ID
.binary_search_by_key(&ext.as_str(), |&(ext, _)| ext)
.ok()
.map(|i| EXT_BY_LANG_ID[i].1)
EXT_BY_LANG_ID.iter().find(|x| x.0 == ext).map(|x| x.1)
}

/// Get the associated language ID from the specified filename, if it is a standard recognised filename.
fn from_standard_filename<S: AsRef<str>>(filename: S) -> Option<&'static str> {
FILENAME_BY_LANG_ID
.binary_search_by_key(&filename.as_ref(), |&(fname, _)| fname)
.ok()
.map(|i| FILENAME_BY_LANG_ID[i].1)
.iter()
.find(|x| x.0 == filename.as_ref())
.map(|x| x.1)
}

/// A struct containing comment style information.
Expand Down
24 changes: 24 additions & 0 deletions src/templater/functions/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* Copyright (c) 2024 Jack Bennett.
* All Rights Reserved.
*
* See the LICENCE file for more information.
*/

use tera::Tera;

#[macro_use]
pub mod fn_utils;
pub mod fn_decls;

mod language_specifics;

/// Register all functions and filters onto the given Tera context
pub fn register_functions(tera: &mut Tera) {
tera.register_function("year", fn_decls::year());
tera.register_function("licence", fn_decls::licence());
tera.register_function("lang_by_filename", fn_decls::lang_by_filename());
tera.register_function("comment_by_lang", fn_decls::comment_by_lang());

tera.register_filter("wrap", fn_decls::wrap());
}
7 changes: 2 additions & 5 deletions src/templater/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@ pub use built_ins::*;
mod context;
pub use context::*;

#[macro_use]
pub mod fn_utils;
pub mod fn_decls;

mod language_specifics;
mod functions;
pub use functions::*;

mod renderer;
pub use renderer::*;
Expand Down

0 comments on commit 0179f82

Please sign in to comment.