feat: Added date config to site.

This commit is contained in:
Spencer Brower
2026-07-23 11:14:44 -04:00
parent b2ff7038cb
commit b3608fe493
3 changed files with 29 additions and 4 deletions
+19 -4
View File
@@ -207,10 +207,12 @@ test_delete_template_doesnt_leak :: proc(t: ^testing.T) {
@(test) @(test)
test_interp_pipe_basic :: proc(t: ^testing.T) { test_interp_pipe_basic :: proc(t: ^testing.T) {
Scalar_Data :: struct { Scalar_Data :: struct {
name: string, name: string,
date_format: string,
} }
data := Scalar_Data { data := Scalar_Data {
name = "2026-03-15T08:49:54-04:00", name = "2026-03-15T08:49:54-04:00",
date_format = "2 Jan 2006",
} }
tpl, _ := parse("[{{name | format}}]", "<test>", allocator = context.temp_allocator) tpl, _ := parse("[{{name | format}}]", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator) result, _ := render(tpl, data, {}, context.temp_allocator)
@@ -220,10 +222,12 @@ test_interp_pipe_basic :: proc(t: ^testing.T) {
@(test) @(test)
test_interp_pipe_unescaped :: proc(t: ^testing.T) { test_interp_pipe_unescaped :: proc(t: ^testing.T) {
Scalar_Data :: struct { Scalar_Data :: struct {
name: string, name: string,
date_format: string,
} }
data := Scalar_Data { data := Scalar_Data {
name = "2025-12-25T00:00:00Z", name = "2025-12-25T00:00:00Z",
date_format = "2 Jan 2006",
} }
tpl, _ := parse("[{{&name | format}}]", "<test>", allocator = context.temp_allocator) tpl, _ := parse("[{{&name | format}}]", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator) result, _ := render(tpl, data, {}, context.temp_allocator)
@@ -233,10 +237,12 @@ test_interp_pipe_unescaped :: proc(t: ^testing.T) {
@(test) @(test)
test_interp_pipe_dot_current :: proc(t: ^testing.T) { test_interp_pipe_dot_current :: proc(t: ^testing.T) {
List_Data :: struct { List_Data :: struct {
items: [3]string, items: [3]string,
date_format: string,
} }
data := List_Data { data := List_Data {
items = {"2026-01-06T00:00:00Z", "2026-06-15T00:00:00Z", "2026-10-15T00:00:00Z"}, items = {"2026-01-06T00:00:00Z", "2026-06-15T00:00:00Z", "2026-10-15T00:00:00Z"},
date_format = "2 Jan 2006",
} }
tpl, _ := parse("{{#items}}[{{. | format}}]{{/items}}", "<test>", allocator = context.temp_allocator) tpl, _ := parse("{{#items}}[{{. | format}}]{{/items}}", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator) result, _ := render(tpl, data, {}, context.temp_allocator)
@@ -248,13 +254,15 @@ test_interp_pipe_dot_current :: proc(t: ^testing.T) {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
Format_Data :: struct { Format_Data :: struct {
date: string, date: string,
date_format: string,
} }
@(test) @(test)
test_format_typical_iso :: proc(t: ^testing.T) { test_format_typical_iso :: proc(t: ^testing.T) {
data := Format_Data { data := Format_Data {
date = "2026-03-15T08:49:54-04:00", date = "2026-03-15T08:49:54-04:00",
date_format = "2 Jan 2006",
} }
tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator) tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator) result, _ := render(tpl, data, {}, context.temp_allocator)
@@ -265,6 +273,7 @@ test_format_typical_iso :: proc(t: ^testing.T) {
test_format_short_date_only :: proc(t: ^testing.T) { test_format_short_date_only :: proc(t: ^testing.T) {
data := Format_Data { data := Format_Data {
date = "2026-06-06", date = "2026-06-06",
date_format = "2 Jan 2006",
} }
tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator) tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator) result, _ := render(tpl, data, {}, context.temp_allocator)
@@ -275,6 +284,7 @@ test_format_short_date_only :: proc(t: ^testing.T) {
test_format_empty_input_errors :: proc(t: ^testing.T) { test_format_empty_input_errors :: proc(t: ^testing.T) {
data := Format_Data { data := Format_Data {
date = "", date = "",
date_format = "2 Jan 2006",
} }
tpl, _ := parse("[{{date | format}}]", "<test>", allocator = context.temp_allocator) tpl, _ := parse("[{{date | format}}]", "<test>", allocator = context.temp_allocator)
defer delete_template(&tpl) defer delete_template(&tpl)
@@ -286,6 +296,7 @@ test_format_empty_input_errors :: proc(t: ^testing.T) {
test_format_non_date_string_errors :: proc(t: ^testing.T) { test_format_non_date_string_errors :: proc(t: ^testing.T) {
data := Format_Data { data := Format_Data {
date = "abc", date = "abc",
date_format = "2 Jan 2006",
} }
tpl, _ := parse("[{{date | format}}]", "<test>", allocator = context.temp_allocator) tpl, _ := parse("[{{date | format}}]", "<test>", allocator = context.temp_allocator)
defer delete_template(&tpl) defer delete_template(&tpl)
@@ -311,6 +322,7 @@ test_format_non_string_value_errors :: proc(t: ^testing.T) {
test_format_invalid_month_errors :: proc(t: ^testing.T) { test_format_invalid_month_errors :: proc(t: ^testing.T) {
data := Format_Data { data := Format_Data {
date = "2023-13-15", date = "2023-13-15",
date_format = "2 Jan 2006",
} }
tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator) tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator)
defer delete_template(&tpl) defer delete_template(&tpl)
@@ -324,6 +336,7 @@ test_format_inside_section_renders :: proc(t: ^testing.T) {
// partial uses {{.}} for ISO attr and {{. | format}} for display. // partial uses {{.}} for ISO attr and {{. | format}} for display.
data := Format_Data { data := Format_Data {
date = "2025-12-25T00:00:00Z", date = "2025-12-25T00:00:00Z",
date_format = "2 Jan 2006",
} }
tpl, _ := parse( tpl, _ := parse(
"{{#date}}<time datetime=\"{{.}}\">{{. | format}}</time>{{/date}}", "{{#date}}<time datetime=\"{{.}}\">{{. | format}}</time>{{/date}}",
@@ -338,6 +351,7 @@ test_format_inside_section_renders :: proc(t: ^testing.T) {
test_format_inside_section_skips_when_empty :: proc(t: ^testing.T) { test_format_inside_section_skips_when_empty :: proc(t: ^testing.T) {
data := Format_Data { data := Format_Data {
date = "", date = "",
date_format = "2 Jan 2006",
} }
tpl, _ := parse("[{{#date}}<time>{{. | format}}</time>{{/date}}]", "<test>", allocator = context.temp_allocator) tpl, _ := parse("[{{#date}}<time>{{. | format}}</time>{{/date}}]", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator) result, _ := render(tpl, data, {}, context.temp_allocator)
@@ -358,6 +372,7 @@ test_format_handles_all_iso8601_variants :: proc(t: ^testing.T) {
for &c in cases { for &c in cases {
data := Format_Data { data := Format_Data {
date = c.input, date = c.input,
date_format = "2 Jan 2006",
} }
tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator) tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator)
result, _ := render(tpl, data, {}, context.temp_allocator) result, _ := render(tpl, data, {}, context.temp_allocator)
+2
View File
@@ -25,6 +25,7 @@ Base_Data :: struct {
title: string, title: string,
description: string, description: string,
og: Open_Graph, og: Open_Graph,
date_format: string,
} }
Page_Data :: struct { Page_Data :: struct {
@@ -163,6 +164,7 @@ render_site :: proc(site: ^Site) {
params = site.params, params = site.params,
description = site.description, description = site.description,
og = site.og, og = site.og,
date_format = site.date.format,
} }
// Find home page // Find home page
+8
View File
@@ -29,6 +29,12 @@ Site :: struct {
features: bit_set[Feature], features: bit_set[Feature],
markdown_extensions: bit_set[md.Extension], markdown_extensions: bit_set[md.Extension],
og: Open_Graph, og: Open_Graph,
date: Date_Preferences,
}
Date_Preferences :: struct {
format: string,
timezone: string,
} }
Feature :: enum { Feature :: enum {
@@ -51,6 +57,7 @@ Config_File :: struct {
params: json.Value, params: json.Value,
modules: json.Value, modules: json.Value,
og: Open_Graph, og: Open_Graph,
date: Date_Preferences,
} }
// Configuration loaded from command line arguments. Gets folded in to Site // Configuration loaded from command line arguments. Gets folded in to Site
@@ -163,6 +170,7 @@ site_apply_config :: proc(site: ^Site, config: Config_File, config_dir: string)
} }
site.og = config.og site.og = config.og
site.date = config.date
} }
site_apply_path_defaults :: proc(site: ^Site, config_dir: string) { site_apply_path_defaults :: proc(site: ^Site, config_dir: string) {