mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
refactor: Page.menu: string is now `Page.menus: map[string]Menu_Entry
This commit is contained in:
@@ -21,15 +21,16 @@ thor/
|
|||||||
├── markdown/ # Content transformation pipeline (imports ../treesitter)
|
├── markdown/ # Content transformation pipeline (imports ../treesitter)
|
||||||
├── mustache/ # Template engine with lambdas + pipe filters + diagnostics
|
├── mustache/ # Template engine with lambdas + pipe filters + diagnostics
|
||||||
├── content.odin # Page struct, Pending_File, scan_content_files, collect_languages, load_page
|
├── content.odin # Page struct, Pending_File, scan_content_files, collect_languages, load_page
|
||||||
├── render.odin # Template rendering, data structs, RSS, sitemap
|
├── render.odin # Template rendering, Template_Context, sort_pages, RSS, sitemap
|
||||||
├── site.odin # Config (Flags, Config_File, Site), init_site
|
├── menus.odin # Menu_Entry, DEFAULT_WEIGHT, build_menus, collect_auto_menus, merge_page_menus, parse_page_menus, parse_config_menus
|
||||||
|
├── site.odin # Config (Flags, Config_File, Site, Site_Context), init_site
|
||||||
├── minify.odin # HTML/CSS minification (imports treesitter)
|
├── minify.odin # HTML/CSS minification (imports treesitter)
|
||||||
├── feed.odin # RSS + sitemap generation
|
├── feed.odin # RSS + sitemap generation
|
||||||
├── vfs.odin # Union file system (defaults → modules → site)
|
├── vfs.odin # Union file system (defaults → modules → site)
|
||||||
├── assets.odin # VFS-based asset copying
|
├── assets.odin # VFS-based asset copying
|
||||||
├── html.odin # HTML helpers: strip_html_tags, unescape_html, generate_summary (word-count truncation), generate_description (scrub to plain text)
|
├── html.odin # HTML helpers: strip_html_tags, unescape_html, generate_summary, generate_description
|
||||||
├── opengraph.odin # Open_Graph struct + og_for_site/og_for_page
|
├── opengraph.odin # Open_Graph struct + og_for_site/og_for_page
|
||||||
├── frontmatter.odin # JSON frontmatter parser (supports nested og + lastmod)
|
├── frontmatter.odin # JSON frontmatter parser (supports nested og + lastmod + weight + menus)
|
||||||
├── defaults.odin # DEFAULTS_PATH constant (#directory)
|
├── defaults.odin # DEFAULTS_PATH constant (#directory)
|
||||||
├── main.odin # Entry point
|
├── main.odin # Entry point
|
||||||
├── bench/ # Template rendering benchmark
|
├── bench/ # Template rendering benchmark
|
||||||
@@ -41,16 +42,17 @@ thor/
|
|||||||
| File | Responsibility |
|
| File | Responsibility |
|
||||||
|---|---|
|
|---|---|
|
||||||
| `main.odin` | Entry point. Sets `context.logger`, calls `init_site`, `build_vfs`, wires `treesitter.grammar_dir`/`query_dir` from config, `site_load_content`, `render_site`. Optional Spall profiling via `SPALL` config flag. |
|
| `main.odin` | Entry point. Sets `context.logger`, calls `init_site`, `build_vfs`, wires `treesitter.grammar_dir`/`query_dir` from config, `site_load_content`, `render_site`. Optional Spall profiling via `SPALL` config flag. |
|
||||||
| `site.odin` | `Flags` (CLI), `Config_File` (thor.json, includes `og: Open_Graph`), `Site` (runtime state + arena + VFS + pages + modules + `og`). `Feature` enum. 5-step `init_site`. Imports `md "markdown"` for `Extension` enum. |
|
| `site.odin` | `Flags` (CLI), `Config_File` (thor.json), `Site_Context` (template-facing: `title`, `description`, `base_url`, `params`, `og`, `menus`), `Site` (runtime state + arena + VFS + pages + `og`). `Feature` enum. 5-step `init_site`. Config menu parsing in `site_apply_config`. |
|
||||||
| `content.odin` | `Page` struct (includes `lastmod`, `og`), `Pending_File` struct, `scan_content_files` (section-aware walk that handles leaf bundles), `collect_languages` (pre-scan for code fence languages), `load_page`, `infer_layout`. Calls `md.process()` for the markdown pipeline. |
|
| `content.odin` | `Page` struct (includes `weight`, `menus: map[string]Menu_Entry`, `og`), `Pending_File` struct, `scan_content_files` (section-aware walk that handles leaf bundles), `collect_languages` (pre-scan for code fence languages), `load_page` (falls back to file mtime when no frontmatter date), `infer_layout`. Calls `md.process()` for the markdown pipeline. |
|
||||||
| `render.odin` | Template rendering: `render_site`, `render_page_html`, `render_home_html`, `render_section`. Data structs (`Base_Data`, `Page_Data`, `Home_Data`, `Section_Data`). VFS-based template loading with fallback chain (`get_template`). |
|
| `render.odin` | Template rendering: `render_site`, `render_page_html`, `render_home_html`, `render_section`. `Template_Context` (unified render struct with `site: Site_Context`, `page: Page`, `menus`, `posts`, `pages`). 3-frame context stack via `[]any{ctx.site, ctx.page, ctx}`. `sort_pages` (weight primary, date secondary). `to_title_case` for section display names. VFS-based template loading with fallback chain (`get_template`). |
|
||||||
|
| `menus.odin` | Menu system: `Menu_Entry {name, url, weight}`, `DEFAULT_WEIGHT = 10`. `build_menus` (priority chain: config → auto + page frontmatter). `collect_auto_menus` (sections + root-level pages). `merge_page_menus` (frontmatter entries with effective weight fallback). `parse_page_menus` (string/array/object forms). `parse_config_menus` (from thor.json). `sort_menu_entries` / `compare_menu_entries` (weight primary, name secondary). |
|
||||||
| `minify.odin` | HTML/CSS minification via tree-sitter. Imports `ts "treesitter"`. |
|
| `minify.odin` | HTML/CSS minification via tree-sitter. Imports `ts "treesitter"`. |
|
||||||
| `feed.odin` | RSS feed + sitemap XML. Uses `page.url` for canonical URLs. |
|
| `feed.odin` | RSS feed + sitemap XML. Uses `page.url` for canonical URLs. |
|
||||||
| `vfs.odin` | Union file system: `VFS`, `build_vfs`, `mount_dir`, `mount_subdir`, `mount_recursive`, `vfs_get`, `vfs_get_entry`, `vfs_entry_data`. Layers defaults → modules → site. |
|
| `vfs.odin` | Union file system: `VFS`, `build_vfs`, `mount_dir`, `mount_subdir`, `mount_recursive`, `vfs_get`, `vfs_get_entry`, `vfs_entry_data`. Layers defaults → modules → site. |
|
||||||
| `assets.odin` | `copy_assets_dir` — iterates VFS entries with `assets/` prefix, minifies CSS, copies verbatim or via `os.copy_file`. |
|
| `assets.odin` | `copy_assets_dir` — iterates VFS entries with `assets/` prefix, minifies CSS, copies verbatim or via `os.copy_file`. |
|
||||||
| `html.odin` | `strip_html_tags`, `unescape_html`, `generate_summary` (word-count truncation, zero-alloc), `generate_description` (HTML→plain text: strip tags, decode entities, collapse whitespace). |
|
| `html.odin` | `strip_html_tags`, `unescape_html`, `generate_summary` (word-count truncation, zero-alloc), `generate_description` (HTML→plain text: strip tags, decode entities, collapse whitespace). |
|
||||||
| `opengraph.odin` | `Open_Graph` struct (fields ordered per OGP spec, `is_article: Maybe(bool)`). `og_for_site(site)` for site defaults (from config + derived), `og_for_page(site_og, page)` for page-specific (overlay page.og + derive from page data). |
|
| `opengraph.odin` | `Open_Graph` struct (fields ordered per OGP spec, `is_article: Maybe(bool)`). `og_for_site(site)` for site defaults (from config + derived), `og_for_page(site_og, page)` for page-specific (overlay page.og + derive from page data). Description falls back to `generate_description(generate_summary(body_html))`. |
|
||||||
| `frontmatter.odin` | JSON frontmatter parser (`{ }` delimited). Supports `layout`, `lastmod`, and nested `og` object (via `json_get_open_graph`). |
|
| `frontmatter.odin` | JSON frontmatter parser (`{ }` delimited). Supports `layout`, `lastmod`, `weight`, `menus`, and nested `og` object (via `json_get_open_graph`). Helpers: `json_get_string`, `json_get_bool`, `json_get_int`. |
|
||||||
| `defaults.odin` | `DEFAULTS_PATH` constant, resolved at compile time via `#directory` so bundled templates ship in the binary. |
|
| `defaults.odin` | `DEFAULTS_PATH` constant, resolved at compile time via `#directory` so bundled templates ship in the binary. |
|
||||||
|
|
||||||
### Subpackages
|
### Subpackages
|
||||||
@@ -64,6 +66,7 @@ thor/
|
|||||||
| | `emoji.odin` | `expand_emoji` — `:shortcode:` → unicode emoji |
|
| | `emoji.odin` | `expand_emoji` — `:shortcode:` → unicode emoji |
|
||||||
| | `sectionate.odin` | `wrap_sections` — splits HTML at `<h2>` into `<section>` wrappers |
|
| | `sectionate.odin` | `wrap_sections` — splits HTML at `<h2>` into `<section>` wrappers |
|
||||||
| | `highlight.odin` | Syntax highlighting via tree-sitter. Imports `../treesitter`. |
|
| | `highlight.odin` | Syntax highlighting via tree-sitter. Imports `../treesitter`. |
|
||||||
|
| | `heading_ids.odin` | `inject_heading_ids` — adds `id` attributes to `<h1>`-`<h6>` from heading text. Slug-based, deduplicated. |
|
||||||
| `mustache/` | See [Mustache engine](#mustache-engine) below | Template engine |
|
| `mustache/` | See [Mustache engine](#mustache-engine) below | Template engine |
|
||||||
| `bench/` | `bench.odin` + `templates/` | Standalone template rendering benchmark. Generates 500 posts + 100 comments, renders with indented partials + inheritance + pipes. `--dump <path>` for output validation, positional arg for iteration count (default 250). |
|
| `bench/` | `bench.odin` + `templates/` | Standalone template rendering benchmark. Generates 500 posts + 100 comments, renders with indented partials + inheritance + pipes. `--dump <path>` for output validation, positional arg for iteration count (default 250). |
|
||||||
|
|
||||||
@@ -74,10 +77,10 @@ Icon SVGs live as HTML partials in `layouts/partials/icons/` (home, github, rss,
|
|||||||
```
|
```
|
||||||
thor.json → find_config → init_site (5-step)
|
thor.json → find_config → init_site (5-step)
|
||||||
→ build_vfs (defaults/layouts → modules → site/layouts, site/assets)
|
→ build_vfs (defaults/layouts → modules → site/layouts, site/assets)
|
||||||
→ site_load_content (scan_content_files + collect_languages + preload_grammars + load_page + url computation)
|
→ site_load_content (scan_content_files + collect_languages + preload_grammars + load_page + url computation + build_menus)
|
||||||
→ render_site
|
→ render_site
|
||||||
→ load_partials + get_template (VFS + fallback chain)
|
→ load_partials + get_template (VFS + fallback chain)
|
||||||
→ render_page_html / render_home_html / render_section
|
→ render_page_html / render_home_html / render_section (3-frame context stack: site, page, ctx)
|
||||||
→ optional minify_html
|
→ optional minify_html
|
||||||
→ public/
|
→ public/
|
||||||
```
|
```
|
||||||
@@ -94,12 +97,14 @@ Page :: struct {
|
|||||||
title: string,
|
title: string,
|
||||||
description: string,
|
description: string,
|
||||||
date: string,
|
date: string,
|
||||||
|
year: string,
|
||||||
|
weight: int, // page ordering (default DEFAULT_WEIGHT = 10)
|
||||||
lastmod: string,
|
lastmod: string,
|
||||||
menu: string,
|
menus: map[string]Menu_Entry, // frontmatter menu assignments
|
||||||
body_html: string,
|
content: string, // rendered HTML body
|
||||||
|
og: Open_Graph,
|
||||||
draft: bool,
|
draft: bool,
|
||||||
is_starred: bool,
|
starred: bool,
|
||||||
og: Open_Graph, // per-page OG overrides from frontmatter
|
|
||||||
_is_index: bool `private`,
|
_is_index: bool `private`,
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
@@ -113,6 +118,33 @@ No `Page_Type` enum — page type is inferred from section + `_is_index`. Layout
|
|||||||
|
|
||||||
**Template fallback chain** (in `get_template`): for content pages, `post → page → base`; for section indexes, `posts_index → section_index → page → base`. Fallbacks logged at debug level. Frontmatter `layout` field overrides the inferred value.
|
**Template fallback chain** (in `get_template`): for content pages, `post → page → base`; for section indexes, `posts_index → section_index → page → base`. Fallbacks logged at debug level. Frontmatter `layout` field overrides the inferred value.
|
||||||
|
|
||||||
|
## Menus
|
||||||
|
|
||||||
|
Menu system in `menus.odin`. `Menu_Entry :: struct {name: string, url: string, weight: int}`. `DEFAULT_WEIGHT = 10`.
|
||||||
|
|
||||||
|
### Sources (priority chain, no mixing)
|
||||||
|
|
||||||
|
1. **Config menus** (`"menus"` key in `thor.json`) — exclusive. `"menus": {}` = explicit opt-out (no menus). Config entries sorted by weight.
|
||||||
|
2. **Auto-menus + page frontmatter** — always run together when no config menus:
|
||||||
|
- Auto: one entry per section directory + one per root-level non-index page. Alphabetical.
|
||||||
|
- Page frontmatter: `"menus": "main"` (string), `["main", "footer"]` (array), or `{"main": {"weight": 30}}` (object with per-menu weight). Merged with auto entries, sorted by weight.
|
||||||
|
|
||||||
|
### Weight
|
||||||
|
|
||||||
|
- `Page.weight` — general page ordering (default `DEFAULT_WEIGHT`). Affects `sort_pages` (weight primary, date secondary).
|
||||||
|
- Per-menu weight — from object frontmatter form. Falls back to `Page.weight` when `DEFAULT_WEIGHT`.
|
||||||
|
- `Menu_Entry.weight` — effective weight after fallback. Sorted ascending, name alphabetical for ties.
|
||||||
|
|
||||||
|
### Templates
|
||||||
|
|
||||||
|
```html
|
||||||
|
{{#menus.main}}
|
||||||
|
<li><a href="{{url}}">{{name}}</a></li>
|
||||||
|
{{/menus.main}}
|
||||||
|
```
|
||||||
|
|
||||||
|
`Template_Context.menus` resolves above `Page.menus` (frontmatter assignments) on the 3-frame context stack. Accessible as `{{#menus.main}}` or `{{#site.menus.main}}`.
|
||||||
|
|
||||||
## Config system
|
## Config system
|
||||||
|
|
||||||
Config is split into three structs with a clear 5-step initialization flow:
|
Config is split into three structs with a clear 5-step initialization flow:
|
||||||
@@ -146,6 +178,12 @@ Config precedence: `CLI flags > thor.json values > hardcoded defaults`.
|
|||||||
"grammars": "~/.config/helix/runtime/grammars/",
|
"grammars": "~/.config/helix/runtime/grammars/",
|
||||||
"queries": "/path/to/tree-sitter/queries",
|
"queries": "/path/to/tree-sitter/queries",
|
||||||
"markdown_extensions": { "emoji": true, "highlight": false },
|
"markdown_extensions": { "emoji": true, "highlight": false },
|
||||||
|
"menus": {
|
||||||
|
"main": [
|
||||||
|
{"name": "Home", "url": "/", "weight": 1},
|
||||||
|
{"name": "About", "url": "/about/"}
|
||||||
|
]
|
||||||
|
},
|
||||||
"params": {
|
"params": {
|
||||||
"social": [
|
"social": [
|
||||||
{ "name": "github", "url": "...", "icon": "icons/github" }
|
{ "name": "github", "url": "...", "icon": "icons/github" }
|
||||||
@@ -225,36 +263,26 @@ Templates use Mustache with template inheritance (`{{<base}}` / `{{$block}}`):
|
|||||||
{{/base}}
|
{{/base}}
|
||||||
```
|
```
|
||||||
|
|
||||||
Data is passed as **typed structs** (not `map[string]any`). Mustache resolves struct fields via Odin reflection, including `using`-embedded fields. Date presence is checked via string truthiness (`{{#date}}`) — no separate `has_date` bool needed. Dates are stored as raw ISO strings; presentation formatting happens in the template via the `format` pipe (see Pipes extension below).
|
Data is passed as a single `Template_Context` struct. `render_template` passes a 3-frame context stack `[]any{ctx.site, ctx.page, ctx}` to `mustache.render`, which auto-detects `[]any` and expands each element into a stack frame. Name resolution walks top-to-bottom: `Template_Context` → `Page` → `Site_Context`. Fields not found on the top frame fall through to lower frames.
|
||||||
|
|
||||||
```odin
|
```odin
|
||||||
Base_Data :: struct {
|
Template_Context :: struct {
|
||||||
now: string, // UTC ISO 8601 build timestamp
|
site: Site_Context, // site-level data (title, description, base_url, params, og)
|
||||||
params: json.Value,
|
menus: map[string][]Menu_Entry, // generated menu data (copied from site, resolves above Page.menus)
|
||||||
content: string,
|
now: string, // UTC ISO 8601 build timestamp
|
||||||
title: string,
|
title: string, // computed browser title ("Page | Site")
|
||||||
description: string,
|
date_format: string, // from site.date.format (thor.json)
|
||||||
og: Open_Graph,
|
timezone: ^datetime.TZ_Region, // for format pipe
|
||||||
date_format: string, // from site.date.format (thor.json)
|
og: Open_Graph, // computed per-page OG
|
||||||
timezone: ^datetime.TZ_Region, // loaded from site.date.timezone or local, owned by Site
|
page: Page, // current page
|
||||||
}
|
pages: [dynamic]Page, // home page list
|
||||||
Page_Data :: struct {
|
posts: [dynamic]Page, // section post list
|
||||||
using base: Base_Data, // fields promoted via reflection fallback
|
|
||||||
page.title: string,
|
|
||||||
date: string, // raw ISO 8601; formatted via `| format` in templates
|
|
||||||
}
|
|
||||||
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, // flat list; year grouping done in template via pipe
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
`render_site` pre-parses all partials and the base layout once (via `mustache.parse`), then per-layout templates are cached in `get_template`. Year-based grouping on section index pages is done in the template via `{{#posts | group_by year}}` (see Pipes extension below) — there is no `Year_Section` Go-side struct.
|
`Site_Context` is embedded in `Site` via `using site_context`. Fields like `site.title`, `site.menus`, `site.params` are accessed directly on `Site` through promotion. `Template_Context.menus` is copied from `site.menus` to resolve above `Page.menus` (frontmatter assignments) on the context stack.
|
||||||
|
|
||||||
|
`render_site` pre-parses all partials and the base layout once (via `mustache.parse`), then per-layout templates are cached in `get_template`. Year-based grouping on section index pages is done in the template via `{{#posts | group_by year}}` (see Pipes extension below).
|
||||||
|
|
||||||
### Pipes extension
|
### Pipes extension
|
||||||
|
|
||||||
@@ -456,6 +484,7 @@ These are things that are easy to get wrong:
|
|||||||
- **Proc arguments are immutable.** You cannot assign to a parameter directly. To get a mutable copy, shadow it: `x := x`. If you need to modify the source, pass a pointer `^x`.
|
- **Proc arguments are immutable.** You cannot assign to a parameter directly. To get a mutable copy, shadow it: `x := x`. If you need to modify the source, pass a pointer `^x`.
|
||||||
- **`for` each loops use `item, idx` order**, not `idx, item`. Correct: `for item, idx in arr`. Wrong: `for idx, item in arr`.
|
- **`for` each loops use `item, idx` order**, not `idx, item`. Correct: `for item, idx in arr`. Wrong: `for idx, item in arr`.
|
||||||
- **`make([dynamic]T, n, allocator)` sets capacity, not length.** To get length=0 with capacity=n, use `make([dynamic]T, 0, n, allocator)`. Using `make([dynamic]T, n, allocator)` creates `len=n` with `n` zero-initialized elements.
|
- **`make([dynamic]T, n, allocator)` sets capacity, not length.** To get length=0 with capacity=n, use `make([dynamic]T, 0, n, allocator)`. Using `make([dynamic]T, n, allocator)` creates `len=n` with `n` zero-initialized elements.
|
||||||
|
- `#partial switch` is usually a code smell. prefer a `case all, extra, types:` branch.
|
||||||
|
|
||||||
## TODO
|
## TODO
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,8 @@
|
|||||||
- want rust style diagnostic and better message, maybe "unknown timezone 'America/New_Yorkskie'"
|
- want rust style diagnostic and better message, maybe "unknown timezone 'America/New_Yorkskie'"
|
||||||
- [ ] Test menu diagnostics
|
- [ ] Test menu diagnostics
|
||||||
- [ ] Honestly, Test **all** diagnostics
|
- [ ] Honestly, Test **all** diagnostics
|
||||||
|
- [ ] Need to be careful about diagnostics across module boundaries.
|
||||||
|
- we don't necessarilly want to warn users about theme designers mistakes. (though perhaps we do)
|
||||||
- [x] Simplify / unify template context stack. Come up with a name for it.
|
- [x] Simplify / unify template context stack. Come up with a name for it.
|
||||||
- [x] `render_template` should accept `Template_Context`, not `any`
|
- [x] `render_template` should accept `Template_Context`, not `any`
|
||||||
- [ ] Load grammars dynamically
|
- [ ] Load grammars dynamically
|
||||||
@@ -37,6 +39,10 @@
|
|||||||
- i.e. force the user to choose one or the other.
|
- i.e. force the user to choose one or the other.
|
||||||
- [ ] Don't show annoying log output in tests.
|
- [ ] Don't show annoying log output in tests.
|
||||||
- [ ] improve home link customization.
|
- [ ] improve home link customization.
|
||||||
|
- [ ] currently an accessability issue.
|
||||||
|
- [ ] support JSON5 in in frontmatter
|
||||||
|
- [ ] Create a json schema file for `thor.json`.
|
||||||
|
- [ ] cleanup `#partial switch`es.
|
||||||
|
|
||||||
## Performance
|
## Performance
|
||||||
|
|
||||||
@@ -128,7 +134,6 @@ main :: proc () {
|
|||||||
- [ ] enabled features / extensions
|
- [ ] enabled features / extensions
|
||||||
- [ ] etc
|
- [ ] etc
|
||||||
- [ ] Avoid `json.Value` / `json.Object` where possible.
|
- [ ] Avoid `json.Value` / `json.Object` where possible.
|
||||||
- [ ] Create a json schema file for `thor.json`.
|
|
||||||
- [ ] make `parse` an overload of `parse_text/parse_inline` and `parse_file`, or something.
|
- [ ] make `parse` an overload of `parse_text/parse_inline` and `parse_file`, or something.
|
||||||
- [ ] Add page params
|
- [ ] Add page params
|
||||||
- [ ] We must remove all mention of `posts` from the odin code.
|
- [ ] We must remove all mention of `posts` from the odin code.
|
||||||
|
|||||||
+11
-10
@@ -21,7 +21,7 @@ Page :: struct {
|
|||||||
date: string,
|
date: string,
|
||||||
year: string,
|
year: string,
|
||||||
lastmod: string,
|
lastmod: string,
|
||||||
menu: string,
|
menus: map[string]Menu_Entry,
|
||||||
content: string,
|
content: string,
|
||||||
og: Open_Graph,
|
og: Open_Graph,
|
||||||
draft: bool,
|
draft: bool,
|
||||||
@@ -256,15 +256,6 @@ load_page :: proc(
|
|||||||
page.lastmod = fm.lastmod
|
page.lastmod = fm.lastmod
|
||||||
page.draft = fm.draft
|
page.draft = fm.draft
|
||||||
page.starred = fm.isStarred
|
page.starred = fm.isStarred
|
||||||
page.menu = fm.menu
|
|
||||||
page.layout = fm.layout if fm.layout != "" else infer_layout(section, is_index)
|
|
||||||
page.og = fm.og
|
|
||||||
|
|
||||||
if strings.has_suffix(file_path, ".html") {
|
|
||||||
page.content = strings.clone(body)
|
|
||||||
} else {
|
|
||||||
page.content = md.process(body, ext, file_path)
|
|
||||||
}
|
|
||||||
|
|
||||||
if section == "" && is_index {
|
if section == "" && is_index {
|
||||||
page.permalink = "/"
|
page.permalink = "/"
|
||||||
@@ -276,6 +267,16 @@ load_page :: proc(
|
|||||||
page.permalink = fmt.aprintf("/%s/%s/", section, slug)
|
page.permalink = fmt.aprintf("/%s/%s/", section, slug)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
page.menus = parse_page_menus(fm.menus, page, context.allocator)
|
||||||
|
page.layout = fm.layout if fm.layout != "" else infer_layout(section, is_index)
|
||||||
|
page.og = fm.og
|
||||||
|
|
||||||
|
if strings.has_suffix(file_path, ".html") {
|
||||||
|
page.content = strings.clone(body)
|
||||||
|
} else {
|
||||||
|
page.content = md.process(body, ext, file_path)
|
||||||
|
}
|
||||||
|
|
||||||
ok = true
|
ok = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|||||||
+19
-2
@@ -10,7 +10,7 @@ Frontmatter :: struct {
|
|||||||
date: string,
|
date: string,
|
||||||
lastmod: string,
|
lastmod: string,
|
||||||
publishDate: string,
|
publishDate: string,
|
||||||
menu: string,
|
menus: json.Value,
|
||||||
layout: string,
|
layout: string,
|
||||||
og: Open_Graph,
|
og: Open_Graph,
|
||||||
draft: bool,
|
draft: bool,
|
||||||
@@ -55,7 +55,9 @@ parse_frontmatter :: proc(content: string) -> (fm: Frontmatter, body: string, ok
|
|||||||
fm.publishDate = json_get_string(obj, "publishDate")
|
fm.publishDate = json_get_string(obj, "publishDate")
|
||||||
fm.draft = json_get_bool(obj, "draft")
|
fm.draft = json_get_bool(obj, "draft")
|
||||||
fm.isStarred = json_get_bool(obj, "isStarred")
|
fm.isStarred = json_get_bool(obj, "isStarred")
|
||||||
fm.menu = json_get_string(obj, "menu")
|
if v, ok := obj["menus"]; ok {
|
||||||
|
fm.menus = v
|
||||||
|
}
|
||||||
fm.layout = json_get_string(obj, "layout")
|
fm.layout = json_get_string(obj, "layout")
|
||||||
fm.og = json_get_open_graph(obj, "og")
|
fm.og = json_get_open_graph(obj, "og")
|
||||||
|
|
||||||
@@ -81,6 +83,20 @@ json_get_bool :: proc(obj: json.Object, key: string) -> bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
json_get_int :: proc(obj: json.Object, key: string) -> int {
|
||||||
|
if v, ok := obj[key]; ok {
|
||||||
|
switch val in v {
|
||||||
|
case json.Integer:
|
||||||
|
return int(val)
|
||||||
|
case json.Float:
|
||||||
|
return int(val)
|
||||||
|
case json.Boolean, json.String, json.Array, json.Object, json.Null:
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
json_get_open_graph :: proc(obj: json.Object, key: string) -> Open_Graph {
|
json_get_open_graph :: proc(obj: json.Object, key: string) -> Open_Graph {
|
||||||
og: Open_Graph
|
og: Open_Graph
|
||||||
if v, ok := obj[key]; ok {
|
if v, ok := obj[key]; ok {
|
||||||
@@ -99,3 +115,4 @@ json_get_open_graph :: proc(obj: json.Object, key: string) -> Open_Graph {
|
|||||||
}
|
}
|
||||||
return og
|
return og
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+110
-11
@@ -3,12 +3,111 @@ package main
|
|||||||
import "core:encoding/json"
|
import "core:encoding/json"
|
||||||
import "core:fmt"
|
import "core:fmt"
|
||||||
import "core:log"
|
import "core:log"
|
||||||
|
import "core:mem"
|
||||||
import "core:os"
|
import "core:os"
|
||||||
import "core:strings"
|
import "core:strings"
|
||||||
|
|
||||||
Menu_Entry :: struct {
|
Menu_Entry :: struct {
|
||||||
name: string,
|
name: string,
|
||||||
url: string,
|
url: string,
|
||||||
|
weight: int,
|
||||||
|
}
|
||||||
|
|
||||||
|
// parse_page_menus converts raw frontmatter JSON into map[string]Menu_Entry.
|
||||||
|
// Supports three forms:
|
||||||
|
// "menus": "main" → {main: {name=title, url=permalink, weight=0}}
|
||||||
|
// "menus": ["main", "footer"] → {main: {...}, footer: {...}}
|
||||||
|
// "menus": {"main": {"weight": 30}} → {main: {name=title, url=permalink, weight=30}}
|
||||||
|
parse_page_menus :: proc(
|
||||||
|
raw: json.Value,
|
||||||
|
page: Page,
|
||||||
|
allocator: mem.Allocator,
|
||||||
|
) -> map[string]Menu_Entry {
|
||||||
|
result: map[string]Menu_Entry
|
||||||
|
|
||||||
|
if raw == nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
switch v in raw {
|
||||||
|
case json.String:
|
||||||
|
result = make(map[string]Menu_Entry, allocator)
|
||||||
|
result[string(v)] = Menu_Entry {
|
||||||
|
name = page.title,
|
||||||
|
url = page.permalink,
|
||||||
|
}
|
||||||
|
|
||||||
|
case json.Array:
|
||||||
|
result = make(map[string]Menu_Entry, allocator)
|
||||||
|
for item in v {
|
||||||
|
if s, ok := item.(json.String); ok {
|
||||||
|
result[string(s)] = Menu_Entry {
|
||||||
|
name = page.title,
|
||||||
|
url = page.permalink,
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.warnf("menus: ignoring non-string item in menus array: %v", item)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case json.Object:
|
||||||
|
result = make(map[string]Menu_Entry, allocator)
|
||||||
|
for menu_name, entry_val in v {
|
||||||
|
weight := 0
|
||||||
|
if entry_obj, ok := entry_val.(json.Object); ok {
|
||||||
|
if w, ok := entry_obj["weight"]; ok {
|
||||||
|
#partial switch wval in w {
|
||||||
|
case json.Integer:
|
||||||
|
weight = int(wval)
|
||||||
|
case json.Float:
|
||||||
|
weight = int(wval)
|
||||||
|
case:
|
||||||
|
log.warnf(
|
||||||
|
"menus: '%s' entry 'weight' must be a number, got %v",
|
||||||
|
menu_name,
|
||||||
|
w,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if _, has_name := entry_obj["name"]; has_name {
|
||||||
|
log.warnf(
|
||||||
|
"menus: '%s' entry 'name' override not yet supported, ignoring",
|
||||||
|
menu_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
if _, has_url := entry_obj["url"]; has_url {
|
||||||
|
log.warnf(
|
||||||
|
"menus: '%s' entry 'url' override not yet supported, ignoring",
|
||||||
|
menu_name,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
log.warnf(
|
||||||
|
"menus: '%s' entry must be an object, got %v, using defaults",
|
||||||
|
menu_name,
|
||||||
|
entry_val,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
result[menu_name] = Menu_Entry {
|
||||||
|
name = page.title,
|
||||||
|
url = page.permalink,
|
||||||
|
weight = weight,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
case json.Null:
|
||||||
|
return nil
|
||||||
|
|
||||||
|
case json.Integer, json.Float, json.Boolean:
|
||||||
|
log.warnf("menus: expected string, array, or object, got %v", raw)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if page.title == "" {
|
||||||
|
log.warnf("menus: page '%s' has no title, menu entry will be blank", page.permalink)
|
||||||
|
}
|
||||||
|
|
||||||
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
// build_menus populates site.menus:
|
// build_menus populates site.menus:
|
||||||
@@ -21,7 +120,7 @@ build_menus :: proc(site: ^Site) {
|
|||||||
if site.menus != nil {
|
if site.menus != nil {
|
||||||
has_menus := false
|
has_menus := false
|
||||||
for page in site.pages {
|
for page in site.pages {
|
||||||
if page.menu != "" {
|
if len(page.menus) > 0 {
|
||||||
has_menus = true
|
has_menus = true
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
@@ -50,22 +149,21 @@ build_menus :: proc(site: ^Site) {
|
|||||||
merge_page_menus(site)
|
merge_page_menus(site)
|
||||||
}
|
}
|
||||||
|
|
||||||
// merge_page_menus collects frontmatter "menu" entries from pages and merges
|
// merge_page_menus collects frontmatter menu entries from pages and merges
|
||||||
// them into site.menus (which may already contain auto-generated entries).
|
// them into site.menus (which may already contain auto-generated entries).
|
||||||
// If no pages have "menu" set, this is a no-op.
|
// If no pages have menus set, this is a no-op.
|
||||||
merge_page_menus :: proc(site: ^Site) {
|
merge_page_menus :: proc(site: ^Site) {
|
||||||
alloc := site_allocator(site)
|
alloc := site_allocator(site)
|
||||||
|
|
||||||
// Collect page entries by menu name
|
// Collect page entries by menu name
|
||||||
page_entries := make(map[string][dynamic]Menu_Entry, 16, alloc)
|
page_entries := make(map[string][dynamic]Menu_Entry, 16, alloc)
|
||||||
for page in site.pages {
|
for page in site.pages {
|
||||||
if page.menu == "" {
|
for menu_name, entry in page.menus {
|
||||||
continue
|
if _, ok := page_entries[menu_name]; !ok {
|
||||||
|
page_entries[menu_name] = make([dynamic]Menu_Entry, 0, 4, alloc)
|
||||||
|
}
|
||||||
|
append(&page_entries[menu_name], entry)
|
||||||
}
|
}
|
||||||
if _, ok := page_entries[page.menu]; !ok {
|
|
||||||
page_entries[page.menu] = make([dynamic]Menu_Entry, 0, 4, alloc)
|
|
||||||
}
|
|
||||||
append(&page_entries[page.menu], Menu_Entry{name = page.title, url = page.permalink})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(page_entries) == 0 {
|
if len(page_entries) == 0 {
|
||||||
@@ -221,3 +319,4 @@ parse_config_menus :: proc(
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+201
@@ -0,0 +1,201 @@
|
|||||||
|
#+test
|
||||||
|
package main
|
||||||
|
|
||||||
|
import "core:encoding/json"
|
||||||
|
import "core:log"
|
||||||
|
import "core:mem"
|
||||||
|
import "core:testing"
|
||||||
|
|
||||||
|
make_page :: proc(title: string, permalink: string) -> Page {
|
||||||
|
return Page{title = title, permalink = permalink}
|
||||||
|
}
|
||||||
|
|
||||||
|
parse_raw :: proc(s: string) -> json.Value {
|
||||||
|
v, _ := json.parse_string(s, spec = .JSON)
|
||||||
|
return v
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_string_form :: proc(t: ^testing.T) {
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("About", "/about/")
|
||||||
|
menus := parse_page_menus(parse_raw(`"main"`), page, context.allocator)
|
||||||
|
testing.expect(t, len(menus) == 1, "expected 1 menu")
|
||||||
|
entry, ok := menus["main"]
|
||||||
|
testing.expect(t, ok, "expected 'main' menu")
|
||||||
|
testing.expect_value(t, entry.name, "About")
|
||||||
|
testing.expect_value(t, entry.url, "/about/")
|
||||||
|
testing.expect_value(t, entry.weight, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_array_form :: proc(t: ^testing.T) {
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("Contact", "/contact/")
|
||||||
|
menus := parse_page_menus(parse_raw(`["main", "footer"]`), page, context.allocator)
|
||||||
|
testing.expect(t, len(menus) == 2, "expected 2 menus")
|
||||||
|
|
||||||
|
main, ok1 := menus["main"]
|
||||||
|
testing.expect(t, ok1)
|
||||||
|
testing.expect_value(t, main.name, "Contact")
|
||||||
|
|
||||||
|
footer, ok2 := menus["footer"]
|
||||||
|
testing.expect(t, ok2)
|
||||||
|
testing.expect_value(t, footer.name, "Contact")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_object_with_weight :: proc(t: ^testing.T) {
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("Posts", "/posts/")
|
||||||
|
menus := parse_page_menus(parse_raw(`{"main": {"weight": 30}}`), page, context.allocator)
|
||||||
|
testing.expect(t, len(menus) == 1)
|
||||||
|
entry, ok := menus["main"]
|
||||||
|
testing.expect(t, ok)
|
||||||
|
testing.expect_value(t, entry.weight, 30)
|
||||||
|
testing.expect_value(t, entry.name, "Posts")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_object_no_weight :: proc(t: ^testing.T) {
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("About", "/about/")
|
||||||
|
menus := parse_page_menus(parse_raw(`{"main": {}}`), page, context.allocator)
|
||||||
|
testing.expect(t, len(menus) == 1)
|
||||||
|
entry, ok := menus["main"]
|
||||||
|
testing.expect(t, ok)
|
||||||
|
testing.expect_value(t, entry.weight, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_nil_input :: proc(t: ^testing.T) {
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("Test", "/test/")
|
||||||
|
menus := parse_page_menus(nil, page, context.allocator)
|
||||||
|
testing.expect(t, menus == nil, "nil input should return nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_invalid_type :: proc(t: ^testing.T) {
|
||||||
|
context.logger = log.nil_logger()
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("Test", "/test/")
|
||||||
|
menus := parse_page_menus(parse_raw(`42`), page, context.allocator)
|
||||||
|
testing.expect(t, menus == nil, "integer should return nil")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_array_non_string :: proc(t: ^testing.T) {
|
||||||
|
context.logger = log.nil_logger()
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("Test", "/test/")
|
||||||
|
menus := parse_page_menus(parse_raw(`["main", 42, "footer"]`), page, context.allocator)
|
||||||
|
testing.expect(t, len(menus) == 2, "42 should be dropped")
|
||||||
|
_, ok1 := menus["main"]
|
||||||
|
testing.expect(t, ok1)
|
||||||
|
_, ok2 := menus["footer"]
|
||||||
|
testing.expect(t, ok2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_object_non_object_value :: proc(t: ^testing.T) {
|
||||||
|
context.logger = log.nil_logger()
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("Test", "/test/")
|
||||||
|
menus := parse_page_menus(parse_raw(`{"main": "oops"}`), page, context.allocator)
|
||||||
|
testing.expect(t, len(menus) == 1, "entry created with defaults")
|
||||||
|
entry, ok := menus["main"]
|
||||||
|
testing.expect(t, ok)
|
||||||
|
testing.expect_value(t, entry.weight, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_non_numeric_weight :: proc(t: ^testing.T) {
|
||||||
|
context.logger = log.nil_logger()
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("Test", "/test/")
|
||||||
|
menus := parse_page_menus(parse_raw(`{"main": {"weight": "30"}}`), page, context.allocator)
|
||||||
|
entry, ok := menus["main"]
|
||||||
|
testing.expect(t, ok)
|
||||||
|
testing.expect_value(t, entry.weight, 0)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_empty_title :: proc(t: ^testing.T) {
|
||||||
|
context.logger = log.nil_logger()
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("", "/test/")
|
||||||
|
menus := parse_page_menus(parse_raw(`"main"`), page, context.allocator)
|
||||||
|
testing.expect(t, len(menus) == 1)
|
||||||
|
entry, ok := menus["main"]
|
||||||
|
testing.expect(t, ok)
|
||||||
|
testing.expect_value(t, entry.name, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_object_with_float_weight :: proc(t: ^testing.T) {
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("Test", "/test/")
|
||||||
|
menus := parse_page_menus(parse_raw(`{"main": {"weight": 15.0}}`), page, context.allocator)
|
||||||
|
entry, ok := menus["main"]
|
||||||
|
testing.expect(t, ok)
|
||||||
|
testing.expect_value(t, entry.weight, 15)
|
||||||
|
}
|
||||||
|
|
||||||
|
@(test)
|
||||||
|
test_menus_null_json :: proc(t: ^testing.T) {
|
||||||
|
arena: mem.Dynamic_Arena
|
||||||
|
mem.dynamic_arena_init(&arena)
|
||||||
|
defer mem.dynamic_arena_destroy(&arena)
|
||||||
|
context.allocator = mem.dynamic_arena_allocator(&arena)
|
||||||
|
|
||||||
|
page := make_page("Test", "/test/")
|
||||||
|
menus := parse_page_menus(parse_raw(`null`), page, context.allocator)
|
||||||
|
testing.expect(t, menus == nil, "null JSON should return nil")
|
||||||
|
}
|
||||||
@@ -16,6 +16,7 @@ Template_Context :: struct {
|
|||||||
timezone: ^datetime.TZ_Region,
|
timezone: ^datetime.TZ_Region,
|
||||||
og: Open_Graph,
|
og: Open_Graph,
|
||||||
site: Site_Context,
|
site: Site_Context,
|
||||||
|
menus: map[string][]Menu_Entry,
|
||||||
page: Page,
|
page: Page,
|
||||||
|
|
||||||
// Home data
|
// Home data
|
||||||
@@ -151,6 +152,7 @@ render_site :: proc(site: ^Site) {
|
|||||||
|
|
||||||
ctx := Template_Context {
|
ctx := Template_Context {
|
||||||
site = site.site_context,
|
site = site.site_context,
|
||||||
|
menus = site.menus,
|
||||||
now = now,
|
now = now,
|
||||||
og = site.og,
|
og = site.og,
|
||||||
date_format = site.date.format,
|
date_format = site.date.format,
|
||||||
|
|||||||
Reference in New Issue
Block a user