mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
refactor: Moved OpenGraph fields to a separate struct.
This commit is contained in:
@@ -1,9 +1,9 @@
|
|||||||
<meta property="og:url" content="{{og_url}}">
|
<meta property="og:url" content="{{og.url}}">
|
||||||
<meta property="og:site_name" content="{{og_site_name}}">
|
<meta property="og:site_name" content="{{og.site_name}}">
|
||||||
<meta property="og:title" content="{{og_title}}">
|
<meta property="og:title" content="{{og.title}}">
|
||||||
<meta property="og:description" content="{{og_description}}">
|
<meta property="og:description" content="{{og.description}}">
|
||||||
<meta property="og:locale" content="en_us">
|
<meta property="og:locale" content="{{og.locale}}">
|
||||||
<meta property="og:type" content="{{og_type}}">
|
<meta property="og:type" content="{{og.type}}">
|
||||||
{{#is_article}}<meta property="article:section" content="{{og_section}}">
|
{{#og.is_article}}<meta property="article:section" content="{{og.section}}">
|
||||||
<meta property="article:published_time" content="{{og_published}}">
|
<meta property="article:published_time" content="{{og.published_time}}">
|
||||||
{{/is_article}}<meta property="og:image" content="{{og_image}}">
|
{{/og.is_article}}<meta property="og:image" content="{{og.image}}">
|
||||||
|
|||||||
@@ -0,0 +1,40 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import "core:fmt"
|
||||||
|
|
||||||
|
Open_Graph :: struct {
|
||||||
|
title: string,
|
||||||
|
type: string,
|
||||||
|
image: string,
|
||||||
|
url: string,
|
||||||
|
|
||||||
|
description: string,
|
||||||
|
locale: string,
|
||||||
|
site_name: string,
|
||||||
|
|
||||||
|
is_article: bool,
|
||||||
|
published_time: string,
|
||||||
|
modified_time: string,
|
||||||
|
section: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
og_init :: proc(site: ^Site) -> Open_Graph {
|
||||||
|
return {
|
||||||
|
site_name = site.title,
|
||||||
|
description = site.description,
|
||||||
|
image = fmt.tprintf("%s/avatar.jpg", site.base_url),
|
||||||
|
locale = "en_US",
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
og_for_page :: proc(site: ^Site, page: Page, base: Open_Graph) -> Open_Graph {
|
||||||
|
og := base
|
||||||
|
is_article := page.section != ""
|
||||||
|
og.url = fmt.tprintf("%s%s", site.base_url, page.permalink)
|
||||||
|
og.title = strip_html_tags(page.title)
|
||||||
|
og.type = "article" if is_article else "website"
|
||||||
|
og.is_article = is_article
|
||||||
|
og.section = page.section
|
||||||
|
og.published_time = page.date
|
||||||
|
return og
|
||||||
|
}
|
||||||
+30
-42
@@ -24,18 +24,12 @@ Year_Section :: struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Base_Data :: struct {
|
Base_Data :: struct {
|
||||||
now: datetime.DateTime,
|
now: datetime.DateTime,
|
||||||
author: string,
|
author: string,
|
||||||
params: json.Value,
|
params: json.Value,
|
||||||
body: string,
|
body: string,
|
||||||
title: string,
|
title: string,
|
||||||
og_site_name: string,
|
og: Open_Graph,
|
||||||
og_description: string,
|
|
||||||
og_image: string,
|
|
||||||
og_url: string,
|
|
||||||
og_title: string,
|
|
||||||
og_type: string,
|
|
||||||
is_article: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Page_Data :: struct {
|
Page_Data :: struct {
|
||||||
@@ -43,8 +37,6 @@ Page_Data :: struct {
|
|||||||
page_title: string,
|
page_title: string,
|
||||||
date_iso: string,
|
date_iso: string,
|
||||||
date_display: string,
|
date_display: string,
|
||||||
og_section: string,
|
|
||||||
og_published: string,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Home_Data :: struct {
|
Home_Data :: struct {
|
||||||
@@ -94,13 +86,6 @@ strip_html_tags :: proc(s: string) -> string {
|
|||||||
return strings.to_string(sb)
|
return strings.to_string(sb)
|
||||||
}
|
}
|
||||||
|
|
||||||
og_type :: proc(is_article: bool) -> string {
|
|
||||||
if is_article {
|
|
||||||
return "article"
|
|
||||||
}
|
|
||||||
return "website"
|
|
||||||
}
|
|
||||||
|
|
||||||
load_template :: proc(vfs: ^VFS, virtual_path: string) -> mustache.Template {
|
load_template :: proc(vfs: ^VFS, virtual_path: string) -> mustache.Template {
|
||||||
data, ok := vfs_get(vfs, virtual_path)
|
data, ok := vfs_get(vfs, virtual_path)
|
||||||
if !ok {
|
if !ok {
|
||||||
@@ -191,12 +176,15 @@ render_site :: proc(site: ^Site) {
|
|||||||
|
|
||||||
// Build base data once
|
// Build base data once
|
||||||
base := Base_Data {
|
base := Base_Data {
|
||||||
now = now,
|
now = now,
|
||||||
author = site.author,
|
author = site.author,
|
||||||
params = site.params,
|
params = site.params,
|
||||||
og_site_name = site.title,
|
og = {
|
||||||
og_description = site.description,
|
site_name = site.title,
|
||||||
og_image = fmt.tprintf("%s/avatar.jpg", site.base_url),
|
description = site.description,
|
||||||
|
image = fmt.tprintf("%s/avatar.jpg", site.base_url),
|
||||||
|
locale = "en_US",
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
// Find home page
|
// Find home page
|
||||||
@@ -309,12 +297,12 @@ render_page_html :: proc(
|
|||||||
data.body = page.body_html
|
data.body = page.body_html
|
||||||
data.date_iso = page.date
|
data.date_iso = page.date
|
||||||
data.date_display = format_date(page.date)
|
data.date_display = format_date(page.date)
|
||||||
data.og_url = fmt.tprintf("%s%s", site.base_url, page.permalink)
|
data.og.url = fmt.tprintf("%s%s", site.base_url, page.permalink)
|
||||||
data.og_title = strip_html_tags(page.title)
|
data.og.title = strip_html_tags(page.title)
|
||||||
data.og_type = og_type(is_article)
|
data.og.type = "article" if is_article else "website"
|
||||||
data.is_article = is_article
|
data.og.is_article = is_article
|
||||||
data.og_section = page.section
|
data.og.section = page.section
|
||||||
data.og_published = page.date
|
data.og.published_time = page.date
|
||||||
return render_template(content_tpl, data, partials)
|
return render_template(content_tpl, data, partials)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -340,10 +328,10 @@ render_home_html :: proc(
|
|||||||
data.title = site.title
|
data.title = site.title
|
||||||
data.body = home.body_html
|
data.body = home.body_html
|
||||||
data.pages = list_pages
|
data.pages = list_pages
|
||||||
data.og_url = fmt.tprintf("%s/", site.base_url)
|
data.og.url = fmt.tprintf("%s/", site.base_url)
|
||||||
data.og_title = site.title
|
data.og.title = site.title
|
||||||
data.og_type = "website"
|
data.og.type = "website"
|
||||||
data.is_article = false
|
data.og.is_article = false
|
||||||
return render_template(content_tpl, data, partials)
|
return render_template(content_tpl, data, partials)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -378,16 +366,16 @@ render_section :: proc(
|
|||||||
data.body = section_index.body_html
|
data.body = section_index.body_html
|
||||||
data.page_title = section_index.title
|
data.page_title = section_index.title
|
||||||
data.title = fmt.tprintf("%s | %s", section_index.title, site.title)
|
data.title = fmt.tprintf("%s | %s", section_index.title, site.title)
|
||||||
data.og_title = section_index.title
|
data.og.title = section_index.title
|
||||||
} else {
|
} else {
|
||||||
data.page_title = capitalize(section)
|
data.page_title = capitalize(section)
|
||||||
data.title = fmt.tprintf("%s | %s", capitalize(section), site.title)
|
data.title = fmt.tprintf("%s | %s", capitalize(section), site.title)
|
||||||
data.og_title = capitalize(section)
|
data.og.title = capitalize(section)
|
||||||
}
|
}
|
||||||
data.by_year = by_year
|
data.by_year = by_year
|
||||||
data.og_url = fmt.tprintf("%s/%s/", site.base_url, section)
|
data.og.url = fmt.tprintf("%s/%s/", site.base_url, section)
|
||||||
data.og_type = "website"
|
data.og.type = "website"
|
||||||
data.is_article = false
|
data.og.is_article = false
|
||||||
return render_template(content_tpl, data, partials)
|
return render_template(content_tpl, data, partials)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user