add support for %NAME% and url_string (permalinks)

This commit is contained in:
Trevor Bentley
2023-01-17 00:24:49 +01:00
parent f787ec91b3
commit 48b6dd056a
5 changed files with 45 additions and 14 deletions
+2 -1
View File
@@ -25,7 +25,7 @@ use crate::{
git::{dir_listing, parse_repo, GitFile, GitRepo, GitsyMetadata},
loud, louder, loudest, normal, normal_noln,
settings::{GitsyCli, GitsyRepoDescriptions, GitsySettings, GitsySettingsRepo},
template::{DirFilter, FileFilter, HexFilter, MaskFilter, OctFilter, Pagination, TsDateFn, TsTimestampFn},
template::{DirFilter, FileFilter, HexFilter, MaskFilter, OctFilter, Pagination, TsDateFn, TsTimestampFn, UrlStringFilter},
util::{GitsyError, GitsyErrorKind, VERBOSITY},
};
use git2::{Error, Repository};
@@ -289,6 +289,7 @@ impl GitsyGenerator {
tera.register_filter("hex", HexFilter {});
tera.register_filter("oct", OctFilter {});
tera.register_filter("mask", MaskFilter {});
tera.register_filter("url_string", UrlStringFilter {});
tera.register_function("ts_to_date", TsDateFn {});
tera.register_function("ts_to_git_timestamp", TsTimestampFn {});
Ok(tera)
+1 -1
View File
@@ -143,7 +143,7 @@ impl SafePathVar for GitObject {
let mut dst = PathBuf::new();
let safe_full_hash = sanitize_path_component(&self.full_hash);
let safe_ref = self.ref_name.as_deref()
.map(|v| sanitize_path_component(&v))
.map(|v| sanitize_path_component(&urlify_path(v)))
.unwrap_or("%REF%".to_string());
for cmp in src.components() {
let cmp = cmp.as_os_str().to_string_lossy()
-1
View File
@@ -32,7 +32,6 @@ use settings::{GitsyCli, GitsySettings};
// TODO:
//
// * favicon
// * permalinks (at least for README?)
// * better error propagation
// * automated tests
// * documentation + examples
+10
View File
@@ -21,6 +21,7 @@
* along with Itsy-Gitsy. If not, see <http://www.gnu.org/licenses/>.
*/
use crate::git::GitFile;
use crate::util::{sanitize_path_component, urlify_path};
use chrono::{naive::NaiveDateTime, offset::FixedOffset, DateTime};
use serde::Serialize;
use std::collections::HashMap;
@@ -116,6 +117,15 @@ impl Filter for MaskFilter {
}
}
pub struct UrlStringFilter;
impl Filter for UrlStringFilter {
fn filter(&self, value: &Value, _args: &HashMap<String, Value>) -> Result<Value, tera::Error> {
let v: String = try_get_value!("url_string", "value", String, value);
let sanitized = sanitize_path_component(&urlify_path(&v));
Ok(to_value(sanitize_path_component(&sanitized)).unwrap())
}
}
pub struct TsDateFn;
impl Function for TsDateFn {
fn call(&self, args: &HashMap<String, Value>) -> Result<Value, tera::Error> {