summaryrefslogtreecommitdiff
path: root/src/codegen.rs
diff options
context:
space:
mode:
authorDaniel Silverstone <dsilvers@digital-scurf.org>2023-08-12 11:34:57 +0100
committerDaniel Silverstone <dsilvers@digital-scurf.org>2023-08-12 11:34:57 +0100
commitdb8aef62e629b1bac2c72bb2590ba4caa90aa8d9 (patch)
tree9058546eb10974f2d4a61007b5395953a54ef06e /src/codegen.rs
parent985ec2f83c9f679ff9b50c6d1e856db2502c3114 (diff)
downloadsubplot-db8aef62e629b1bac2c72bb2590ba4caa90aa8d9.tar.gz
subplot: Enable passing of location into the template expansion
Signed-off-by: Daniel Silverstone <dsilvers@digital-scurf.org>
Diffstat (limited to 'src/codegen.rs')
-rw-r--r--src/codegen.rs17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/codegen.rs b/src/codegen.rs
index 41b5556..5855d8b 100644
--- a/src/codegen.rs
+++ b/src/codegen.rs
@@ -1,3 +1,4 @@
+use crate::html::Location;
use crate::{resource, Document, SubplotError, TemplateSpec};
use std::collections::HashMap;
use std::fs::File;
@@ -57,6 +58,7 @@ fn tera(tmplspec: &TemplateSpec, templatename: &str) -> Result<Tera, SubplotErro
tera.register_filter("base64", base64);
tera.register_filter("nameslug", nameslug);
tera.register_filter("commentsafe", commentsafe);
+ tera.register_filter("location", locationfilter);
let dirname = tmplspec.template_filename().parent().unwrap();
for helper in tmplspec.helpers() {
let helper_path = dirname.join(helper);
@@ -93,6 +95,21 @@ fn base64(v: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> {
}
}
+fn locationfilter(v: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> {
+ let location: Location = serde_json::from_value(v.clone())?;
+ Ok(Value::String(format!(
+ "{:?}",
+ match location {
+ Location::Known {
+ filename,
+ line,
+ col,
+ } => format!("{}:{}:{}", filename.display(), line, col),
+ Location::Unknown => "unknown".to_string(),
+ }
+ )))
+}
+
fn nameslug(name: &Value, _: &HashMap<String, Value>) -> tera::Result<Value> {
match name {
Value::String(s) => {