mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
refactor: render_template now accepts only Template_Context objects.
This commit is contained in:
@@ -81,6 +81,7 @@ main :: proc () {
|
|||||||
- [ ] Integrity hash
|
- [ ] Integrity hash
|
||||||
- Allows users to verify their output didn't change after upgrading to a new version
|
- Allows users to verify their output didn't change after upgrading to a new version
|
||||||
- [ ] Content-hash fingerprinting for CSS and JS cache busting
|
- [ ] Content-hash fingerprinting for CSS and JS cache busting
|
||||||
|
- [ ] merge `render_{section,home_html,page_html}` procs.
|
||||||
- [ ] try to combine render_page_html and render_home_html?
|
- [ ] try to combine render_page_html and render_home_html?
|
||||||
- [ ] Avoid `json.Value` / `json.Object` where possible.
|
- [ ] Avoid `json.Value` / `json.Object` where possible.
|
||||||
- [ ] Create a json schema file for `thor.json`.
|
- [ ] Create a json schema file for `thor.json`.
|
||||||
|
|||||||
+64
-77
@@ -10,6 +10,28 @@ import "core:strings"
|
|||||||
import "core:time"
|
import "core:time"
|
||||||
import "core:time/datetime"
|
import "core:time/datetime"
|
||||||
|
|
||||||
|
Template_Context :: struct {
|
||||||
|
params: json.Value,
|
||||||
|
now: string,
|
||||||
|
content: string,
|
||||||
|
title: string,
|
||||||
|
description: string,
|
||||||
|
og: Open_Graph,
|
||||||
|
date_format: string,
|
||||||
|
timezone: ^datetime.TZ_Region,
|
||||||
|
|
||||||
|
// Page Data
|
||||||
|
page_title: string,
|
||||||
|
date: string,
|
||||||
|
|
||||||
|
// Home data
|
||||||
|
pages: [dynamic]Page_Context,
|
||||||
|
|
||||||
|
// Section Data
|
||||||
|
// TODO: Remove "posts" from the Odin code
|
||||||
|
posts: [dynamic]Page_Context,
|
||||||
|
}
|
||||||
|
|
||||||
Page_Context :: struct {
|
Page_Context :: struct {
|
||||||
permalink: string,
|
permalink: string,
|
||||||
title: string,
|
title: string,
|
||||||
@@ -18,34 +40,6 @@ Page_Context :: struct {
|
|||||||
year: string,
|
year: string,
|
||||||
}
|
}
|
||||||
|
|
||||||
Base_Data :: struct {
|
|
||||||
now: string,
|
|
||||||
params: json.Value,
|
|
||||||
content: string,
|
|
||||||
title: string,
|
|
||||||
description: string,
|
|
||||||
og: Open_Graph,
|
|
||||||
date_format: string,
|
|
||||||
timezone: ^datetime.TZ_Region,
|
|
||||||
}
|
|
||||||
|
|
||||||
Page_Data :: struct {
|
|
||||||
using base: Base_Data,
|
|
||||||
page_title: string,
|
|
||||||
date: string,
|
|
||||||
}
|
|
||||||
|
|
||||||
Home_Data :: struct {
|
|
||||||
using base: Base_Data,
|
|
||||||
pages: [dynamic]Page_Context,
|
|
||||||
}
|
|
||||||
|
|
||||||
Section_Data :: struct {
|
|
||||||
using base: Base_Data,
|
|
||||||
page_title: string,
|
|
||||||
posts: [dynamic]Page_Context,
|
|
||||||
}
|
|
||||||
|
|
||||||
build_page_context :: proc(page: Page) -> Page_Context {
|
build_page_context :: proc(page: Page) -> Page_Context {
|
||||||
return Page_Context {
|
return Page_Context {
|
||||||
permalink = page.permalink,
|
permalink = page.permalink,
|
||||||
@@ -131,7 +125,7 @@ capitalize :: proc(s: string) -> string {
|
|||||||
|
|
||||||
render_template :: proc(
|
render_template :: proc(
|
||||||
content_tpl: mustache.Template,
|
content_tpl: mustache.Template,
|
||||||
data: any,
|
data: Template_Context,
|
||||||
partials: map[string]mustache.Template,
|
partials: map[string]mustache.Template,
|
||||||
) -> string {
|
) -> string {
|
||||||
result, err := mustache.render(content_tpl, data, partials)
|
result, err := mustache.render(content_tpl, data, partials)
|
||||||
@@ -162,14 +156,13 @@ render_site :: proc(site: ^Site) {
|
|||||||
now, ok2 := time.time_to_rfc3339(time.now(), offset, false, allocator)
|
now, ok2 := time.time_to_rfc3339(time.now(), offset, false, allocator)
|
||||||
assert(ok2)
|
assert(ok2)
|
||||||
|
|
||||||
// Build base data once
|
ctx := Template_Context {
|
||||||
base := Base_Data {
|
now = now,
|
||||||
now = now,
|
params = site.params,
|
||||||
params = site.params,
|
description = site.description,
|
||||||
description = site.description,
|
og = site.og,
|
||||||
og = site.og,
|
date_format = site.date.format,
|
||||||
date_format = site.date.format,
|
timezone = site.tz,
|
||||||
timezone = site.tz,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find home page
|
// Find home page
|
||||||
@@ -198,7 +191,7 @@ render_site :: proc(site: ^Site) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
tpl := get_template(&site.vfs, page.layout, &template_cache)
|
tpl := get_template(&site.vfs, page.layout, &template_cache)
|
||||||
html := render_page_html(page, site, tpl, partials, base)
|
html := render_page_html(page, site, tpl, partials, ctx)
|
||||||
if .Minify in site.features {
|
if .Minify in site.features {
|
||||||
html = minify_html(html)
|
html = minify_html(html)
|
||||||
}
|
}
|
||||||
@@ -226,7 +219,7 @@ render_site :: proc(site: ^Site) {
|
|||||||
has_section_index,
|
has_section_index,
|
||||||
section_tpl,
|
section_tpl,
|
||||||
partials,
|
partials,
|
||||||
base,
|
ctx,
|
||||||
)
|
)
|
||||||
if .Minify in site.features {
|
if .Minify in site.features {
|
||||||
html = minify_html(html)
|
html = minify_html(html)
|
||||||
@@ -237,7 +230,7 @@ render_site :: proc(site: ^Site) {
|
|||||||
// Render home page
|
// Render home page
|
||||||
if has_home {
|
if has_home {
|
||||||
home_tpl := get_template(&site.vfs, "home", &template_cache)
|
home_tpl := get_template(&site.vfs, "home", &template_cache)
|
||||||
home_html := render_home_html(home, site, home_tpl, partials, base)
|
home_html := render_home_html(home, site, home_tpl, partials, ctx)
|
||||||
if .Minify in site.features {
|
if .Minify in site.features {
|
||||||
home_html = minify_html(home_html)
|
home_html = minify_html(home_html)
|
||||||
}
|
}
|
||||||
@@ -271,18 +264,15 @@ render_page_html :: proc(
|
|||||||
site: ^Site,
|
site: ^Site,
|
||||||
content_tpl: mustache.Template,
|
content_tpl: mustache.Template,
|
||||||
partials: map[string]mustache.Template,
|
partials: map[string]mustache.Template,
|
||||||
base: Base_Data,
|
ctx: Template_Context,
|
||||||
) -> string {
|
) -> string {
|
||||||
is_article := page.section != ""
|
ctx := ctx
|
||||||
data := Page_Data {
|
ctx.title = fmt.tprintf("%s | %s", page.title, site.title)
|
||||||
base = base,
|
ctx.page_title = page.title
|
||||||
}
|
ctx.content = page.body_html
|
||||||
data.title = fmt.tprintf("%s | %s", page.title, site.title)
|
ctx.date = page.date
|
||||||
data.page_title = page.title
|
ctx.og = og_for_page(site.og, page)
|
||||||
data.content = page.body_html
|
return render_template(content_tpl, ctx, partials)
|
||||||
data.date = page.date
|
|
||||||
data.og = og_for_page(site.og, page)
|
|
||||||
return render_template(content_tpl, data, partials)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render_home_html :: proc(
|
render_home_html :: proc(
|
||||||
@@ -290,7 +280,7 @@ render_home_html :: proc(
|
|||||||
site: ^Site,
|
site: ^Site,
|
||||||
content_tpl: mustache.Template,
|
content_tpl: mustache.Template,
|
||||||
partials: map[string]mustache.Template,
|
partials: map[string]mustache.Template,
|
||||||
base: Base_Data,
|
ctx: Template_Context,
|
||||||
) -> string {
|
) -> string {
|
||||||
list_pages := make([dynamic]Page_Context)
|
list_pages := make([dynamic]Page_Context)
|
||||||
defer delete(list_pages)
|
defer delete(list_pages)
|
||||||
@@ -301,14 +291,13 @@ render_home_html :: proc(
|
|||||||
append(&list_pages, build_page_context(page))
|
append(&list_pages, build_page_context(page))
|
||||||
}
|
}
|
||||||
|
|
||||||
data := Home_Data {
|
ctx := ctx
|
||||||
base = base,
|
ctx.title = site.title
|
||||||
}
|
ctx.content = home.body_html
|
||||||
data.title = site.title
|
ctx.pages = list_pages
|
||||||
data.content = home.body_html
|
ctx.og = og_for_page(site.og, home)
|
||||||
data.pages = list_pages
|
|
||||||
data.og = og_for_page(site.og, home)
|
return render_template(content_tpl, ctx, partials)
|
||||||
return render_template(content_tpl, data, partials)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
render_section :: proc(
|
render_section :: proc(
|
||||||
@@ -318,7 +307,7 @@ render_section :: proc(
|
|||||||
has_index: bool,
|
has_index: bool,
|
||||||
content_tpl: mustache.Template,
|
content_tpl: mustache.Template,
|
||||||
partials: map[string]mustache.Template,
|
partials: map[string]mustache.Template,
|
||||||
base: Base_Data,
|
ctx: Template_Context,
|
||||||
) -> string {
|
) -> string {
|
||||||
posts := make([dynamic]Page_Context)
|
posts := make([dynamic]Page_Context)
|
||||||
defer delete(posts)
|
defer delete(posts)
|
||||||
@@ -329,25 +318,23 @@ render_section :: proc(
|
|||||||
append(&posts, build_page_context(page))
|
append(&posts, build_page_context(page))
|
||||||
}
|
}
|
||||||
|
|
||||||
data := Section_Data {
|
ctx := ctx
|
||||||
base = base,
|
|
||||||
}
|
|
||||||
if has_index {
|
if has_index {
|
||||||
data.content = section_index.body_html
|
ctx.content = section_index.body_html
|
||||||
data.page_title = section_index.title
|
ctx.page_title = section_index.title
|
||||||
data.title = fmt.tprintf("%s | %s", section_index.title, site.title)
|
ctx.title = fmt.tprintf("%s | %s", section_index.title, site.title)
|
||||||
data.og = og_for_page(site.og, section_index)
|
ctx.og = og_for_page(site.og, section_index)
|
||||||
} else {
|
} else {
|
||||||
data.page_title = capitalize(section)
|
ctx.page_title = capitalize(section)
|
||||||
data.title = fmt.tprintf("%s | %s", capitalize(section), site.title)
|
ctx.title = fmt.tprintf("%s | %s", capitalize(section), site.title)
|
||||||
data.og.title = capitalize(section)
|
ctx.og.title = capitalize(section)
|
||||||
data.og.description = ""
|
ctx.og.description = ""
|
||||||
data.og.url = fmt.tprintf("%s/%s/", site.base_url, section)
|
ctx.og.url = fmt.tprintf("%s/%s/", site.base_url, section)
|
||||||
data.og.type = "website"
|
ctx.og.type = "website"
|
||||||
data.og.is_article = false
|
ctx.og.is_article = false
|
||||||
}
|
}
|
||||||
data.posts = posts
|
ctx.posts = posts
|
||||||
return render_template(content_tpl, data, partials)
|
return render_template(content_tpl, ctx, partials)
|
||||||
}
|
}
|
||||||
|
|
||||||
load_partials :: proc(vfs: ^VFS) -> map[string]mustache.Template {
|
load_partials :: proc(vfs: ^VFS) -> map[string]mustache.Template {
|
||||||
|
|||||||
Reference in New Issue
Block a user