aboutsummaryrefslogtreecommitdiff
path: root/lib/render/template.ml
diff options
context:
space:
mode:
authorLukasz Kasprzak <lukas@labunix.xyz>2026-08-19 08:17:30 +0200
committerLukasz Kasprzak <lukas@labunix.xyz>2026-08-19 08:17:30 +0200
commitda9cf402ceed8102aec1a9d008f8e918a23d39f3 (patch)
tree3c9a7bd748f886633117cce7059ba304c23541cb /lib/render/template.ml
parenta526d934cea17ff1b76ddad5c5f2934fb85c11cb (diff)
downloadcolitur-da9cf402ceed8102aec1a9d008f8e918a23d39f3.tar.gz
colitur-da9cf402ceed8102aec1a9d008f8e918a23d39f3.zip
feat(render): template renderer with mandatory escaping
Every interpolated value is escaped for the template's flavour; the template's own literal text never is, because that is the author's markup. There is no raw form, so a template cannot opt out. Scope is a stack with outward fallback, so a grid template can reach the year number from inside a week without the view duplicating it into every cell. A missing key renders empty -- the one deliberate silence, so a template survives a rite that does not set every optional field. Mutation-tested: dropping the Escape.apply call reddens the data-cannot-escape-flavour case.
Diffstat (limited to 'lib/render/template.ml')
-rw-r--r--lib/render/template.ml55
1 files changed, 55 insertions, 0 deletions
diff --git a/lib/render/template.ml b/lib/render/template.ml
index 3e35d53..7efa92b 100644
--- a/lib/render/template.ml
+++ b/lib/render/template.ml
@@ -94,3 +94,58 @@ let build items =
go [] [] items
let parse src = match lex src with Error e -> Error e | Ok items -> build items
+
+(* Scope is a STACK, innermost first: a section pushes its own object, and a
+ lookup falls back outward. Without the fallback a grid template could not
+ reach the year number from inside a week. *)
+let rec lookup stack path =
+ match stack with
+ | [] -> None
+ | top :: rest -> (
+ match descend top path with Some v -> Some v | None -> lookup rest path)
+
+and descend v path =
+ match (v, path) with
+ | _, [] -> Some v
+ | Obj kvs, k :: tl -> (
+ match List.assoc_opt k kvs with Some v' -> descend v' tl | None -> None)
+ | _ -> None
+
+let truthy = function
+ | Bool b -> b
+ | Str "" -> false
+ | Str _ -> true
+ | List [] -> false
+ | List _ -> true
+ | Obj _ -> true
+
+let render ~flavour nodes value =
+ let b = Buffer.create 4096 in
+ let rec go stack nodes =
+ List.iter
+ (fun node ->
+ match node with
+ | Text t -> Buffer.add_string b t
+ | Var p -> (
+ match lookup stack p with
+ | Some (Str s) -> Buffer.add_string b (Escape.apply flavour s)
+ | Some (Bool true) -> Buffer.add_string b "true"
+ | Some (Bool false) -> ()
+ | Some (List _) | Some (Obj _) | None -> ())
+ | Section (p, body) -> (
+ match lookup stack p with
+ | None -> ()
+ | Some (List items) -> List.iter (fun it -> go (it :: stack) body) items
+ | Some v when truthy v -> go (v :: stack) body
+ | Some _ -> ())
+ | Inverted (p, body) -> (
+ match lookup stack p with
+ | None -> go stack body
+ | Some v -> if not (truthy v) then go stack body))
+ nodes
+ in
+ go [ value ] nodes;
+ Buffer.contents b
+
+let render_string ~flavour src value =
+ match parse src with Error e -> Error e | Ok nodes -> Ok (render ~flavour nodes value)