mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
Compare commits
62 Commits
0307ab647f
...
main
| Author | SHA1 | Date | |
|---|---|---|---|
| 5088f31a58 | |||
| 380c324623 | |||
| 695548bada | |||
| fde54c1f94 | |||
| 876f767548 | |||
| 0e8d770ac8 | |||
| cc33447ace | |||
| 2ac1d767cf | |||
| 2af8c68339 | |||
| b1e40c480b | |||
| 8c1f7647a1 | |||
| e7bb5126cb | |||
| f3ea6ab2a9 | |||
| ed842e4bea | |||
| 8d67108984 | |||
| 58e6da6a2c | |||
| 3ad59c0761 | |||
| 6a4debe35a | |||
| b3c2d4745d | |||
| 8404227c22 | |||
| 55a0d25173 | |||
| 604a80ca42 | |||
| 806c153979 | |||
| 4c41d85385 | |||
| c92d5e5611 | |||
| afb98c3bc4 | |||
| 7eb116c731 | |||
| 616880b3da | |||
| e4f5c63346 | |||
| dd77289f5b | |||
| dd76a8e83c | |||
| 10be1b92c4 | |||
| 49d08c3129 | |||
| 6eebaa0a6c | |||
| e1b2c9d321 | |||
| 86c5428f77 | |||
| 4ed581373b | |||
| 5e2bee4897 | |||
| 54d3bed630 | |||
| 073695b7c7 | |||
| 4d2fdf284d | |||
| 25551a4fb6 | |||
| b06cf6f8b1 | |||
| a420803b3e | |||
| 040c462bba | |||
| fa8179c9b9 | |||
| 8416411c62 | |||
| f54da58698 | |||
| f370da6e55 | |||
| ceae8bfa8b | |||
| e64cd929e6 | |||
| de328b3bcb | |||
| 8cb7d1243b | |||
| 126548cb29 | |||
| c289c3c7f9 | |||
| d1d0af60e7 | |||
| 6d7b88a8d6 | |||
| f1c7d02bc1 | |||
| 6ec8fcf2a1 | |||
| b81aa38c62 | |||
| 9a35fa2051 | |||
| 178b02da74 |
@@ -2,6 +2,18 @@
|
||||
|
||||
Thor is a static site generator written in [Odin](https://odin-lang.org), replacing Hugo for the `sbrow.github.io` blog. It lives at `./thor/` as a git subtree with its own `flake.nix`.
|
||||
|
||||
## 🚫 DO NOT EDIT THE DOCS — BY HUMANS, FOR HUMANS
|
||||
|
||||
> **THE DOCUMENTATION UNDER `thor/site/` IS HANDWRITTEN, BY HUMANS, FOR HUMANS.**
|
||||
>
|
||||
> **NO AI, AGENT, BOT, ASSISTANT, OR OTHER NON-HUMAN MAY EDIT, REWRITE,
|
||||
> REPHRASE, REFORMAT, "IMPROVE," SUMMARIZE, OR GENERATE ANY FILE UNDER
|
||||
> `thor/site/` — EVER.**
|
||||
>
|
||||
> These are not machine artifacts. A human wrote every word. AI may be
|
||||
> consulted as a sanity check, but the prose stays human. If you are not a
|
||||
> human, do not touch these files. See `thor/site/content/ai.md`.
|
||||
|
||||
## Architecture
|
||||
|
||||
```
|
||||
@@ -21,15 +33,16 @@ thor/
|
||||
├── markdown/ # Content transformation pipeline (imports ../treesitter)
|
||||
├── mustache/ # Template engine with lambdas + pipe filters + diagnostics
|
||||
├── content.odin # Page struct, Pending_File, scan_content_files, collect_languages, load_page
|
||||
├── render.odin # Template rendering, data structs, RSS, sitemap
|
||||
├── site.odin # Config (Flags, Config_File, Site), init_site
|
||||
├── render.odin # Template rendering, Template_Context, sort_pages, RSS, sitemap
|
||||
├── 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)
|
||||
├── feed.odin # RSS + sitemap generation
|
||||
├── vfs.odin # Union file system (defaults → modules → site)
|
||||
├── 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
|
||||
├── 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)
|
||||
├── main.odin # Entry point
|
||||
├── bench/ # Template rendering benchmark
|
||||
@@ -40,17 +53,18 @@ thor/
|
||||
|
||||
| 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. |
|
||||
| `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. |
|
||||
| `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. |
|
||||
| `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`). |
|
||||
| `main.odin` | Entry point. Parses CLI flags via `core:flags`, sets logger level from `-verbose`/`-quiet`, 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, includes `-verbose`/`-quiet`), `Config_File` (thor.json), `Site_Context` (template-facing: `title`, `description`, `base_url`, `params`, `og`, `menus`), `Site` (runtime state + arena + VFS + pages + `og`). `Feature` enum. `init_site(site, flags)` — takes pre-parsed `Flags`. Config menu parsing in `site_apply_config`. |
|
||||
| `content.odin` | `Page` struct (includes `weight`, `menus`, `params: json.Value`, `toc: string`, `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, generates TOC via `md.generate_toc` when frontmatter `"toc": true`), `infer_layout`. Calls `md.process()` for the markdown pipeline. |
|
||||
| `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`, `params`, `posts`, `pages`). 3-frame context stack via `[]any{ctx.site, ctx.page, ctx}`. `merge_params(site, page)` — shallow merge of site + page params. Error deduplication via `seen: ^map[string]bool` passed through render chain. `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: Maybe(int)}`, `DEFAULT_WEIGHT = 10`. `build_menus` (priority chain: config → auto + page frontmatter, then `warn_all_duplicate_weights`). `collect_auto_menus` (sections + root-level pages, skips pages with explicit `"menus": "main"` frontmatter). `merge_page_menus` (frontmatter entries with effective weight fallback via nil check). `parse_page_menus` (string/array/object forms). `parse_config_menus` (from thor.json). `sort_menu_entries` / `compare_menu_entries` (weight primary via `.? or_else DEFAULT_WEIGHT`, name secondary). `warn_duplicate_weights` / `warn_all_duplicate_weights` (log when two entries in same menu have same explicitly-set weight). |
|
||||
| `minify.odin` | HTML/CSS minification via tree-sitter. Imports `ts "treesitter"`. |
|
||||
| `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. |
|
||||
| `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). |
|
||||
| `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). |
|
||||
| `frontmatter.odin` | JSON frontmatter parser (`{ }` delimited). Supports `layout`, `lastmod`, and nested `og` object (via `json_get_open_graph`). |
|
||||
| `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`, `weight: Maybe(int)`, `menus`, `params: json.Value`, `toc: bool`, and nested `og` object (via `json_get_open_graph`). Helpers: `json_get_string`, `json_get_bool`, `json_get_int` (returns `Maybe(int)`, nil for absent/invalid). |
|
||||
| `defaults.odin` | `DEFAULTS_PATH` constant, resolved at compile time via `#directory` so bundled templates ship in the binary. |
|
||||
|
||||
### Subpackages
|
||||
@@ -58,12 +72,15 @@ thor/
|
||||
| Package | Files | Responsibility |
|
||||
|---|---|---|
|
||||
| `treesitter/` | `treesitter.odin` | FFI types (`Parser`, `Node`, `Query`, etc.), `@(link_prefix="ts_")` foreign bindings, grammar management (`Grammar_Store` with persistent allocator, `load_language`/`compile_query` building blocks, `ensure_parser`/`load_grammar` lazy loading, `preload_grammar`/`preload_grammars` for parallel loading with `sync.Mutex` cache protection), statically-linked HTML/CSS grammars |
|
||||
| `markdown/` | `markdown.odin` | `Extension` enum, `DEFAULT_EXTENSIONS`, `process(body, ext, file_path)` — full pipeline, `parse_extension_list`, `apply_extension_config` |
|
||||
| | `footnotes.odin` | `strip_definitions` (pre-cmark), `inject_notes` (post-cmark) |
|
||||
| `markdown/` | `markdown.odin` | `Extension` enum, `DEFAULT_EXTENSIONS`, `process(body, ext, file_path, allocator)` — full pipeline (clones cmark output, frees original), `parse_extension_list`, `apply_extension_config` |
|
||||
| | `footnotes.odin` | `strip_definitions` (pre-cmark, shared by `.Sidenotes` + `.Footnotes`), `inject_notes` (post-cmark sidenote rendering), `inject_footnotes` (post-cmark standard footnote rendering — numbered `<sup>` links + `<section class="footnotes"><ol>` at bottom). `.Sidenotes` and `.Footnotes` are mutually exclusive; `resolve_extension_conflicts` in `markdown.odin` picks `.Footnotes` if both are set. |
|
||||
| | `alerts.odin` | `inject_alerts` — GitHub alert blocks (`> [!NOTE]`) → styled blockquotes with semantic class names (`alert-note` etc.) |
|
||||
| | `emoji.odin` | `expand_emoji` — `:shortcode:` → unicode emoji |
|
||||
| | `sectionate.odin` | `wrap_sections` — splits HTML at `<h2>` into `<section>` wrappers |
|
||||
| | `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. |
|
||||
| | `deflists.odin` | `convert_deflists` — pre-cmark pass. Scans for definition list patterns (`term\n\n: definition`) and converts to `<dl><dt><dd>` HTML blocks. Terms and definitions rendered through cmark individually for inline markdown. Consecutive pairs grouped into single `<dl>`. |
|
||||
| | `toc.odin` | `generate_toc(html, allocator)` — page-level feature (not a pipeline extension). Scans `<h1>`-`<h6>` for IDs (after `inject_heading_ids`), builds nested `<ul>` with `<a href="#id">` links. Called from `load_page` when frontmatter `"toc": true`. Depends on `.HeadingIDs` being enabled. |
|
||||
| `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). |
|
||||
|
||||
@@ -74,10 +91,10 @@ Icon SVGs live as HTML partials in `layouts/partials/icons/` (home, github, rss,
|
||||
```
|
||||
thor.json → find_config → init_site (5-step)
|
||||
→ 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 + warn_all_duplicate_weights)
|
||||
→ render_site
|
||||
→ 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
|
||||
→ public/
|
||||
```
|
||||
@@ -94,15 +111,19 @@ Page :: struct {
|
||||
title: string,
|
||||
description: string,
|
||||
date: string,
|
||||
year: string,
|
||||
weight: Maybe(int), // page ordering (nil = unset, defaults to DEFAULT_WEIGHT at comparison time)
|
||||
lastmod: string,
|
||||
menu: string,
|
||||
body_html: string,
|
||||
menus: map[string]Menu_Entry, // frontmatter menu assignments
|
||||
params: json.Value, // per-page params (merged with site params at render time)
|
||||
content: string, // rendered HTML body
|
||||
og: Open_Graph,
|
||||
draft: bool,
|
||||
is_starred: bool,
|
||||
og: Open_Graph, // per-page OG overrides from frontmatter
|
||||
toc: string, // generated table of contents HTML (empty if not requested)
|
||||
_is_index: bool `private`,
|
||||
}
|
||||
```
|
||||
```
|
||||
|
||||
No `Page_Type` enum — page type is inferred from section + `_is_index`. Layout is inferred via `infer_layout(section, is_index)`:
|
||||
|
||||
@@ -113,17 +134,51 @@ 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.
|
||||
|
||||
## 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
|
||||
|
||||
All weight fields use `Maybe(int)` — nil means "unset," `some(v)` means explicitly set. This distinguishes `"weight": 10` (explicit) from no weight key (defaults to `DEFAULT_WEIGHT` at comparison time via `.? or_else DEFAULT_WEIGHT`). Eliminates the old `0`-as-sentinel pattern from `json_get_int`.
|
||||
|
||||
- `Page.weight: Maybe(int)` — page-level ordering. nil = unset. Affects `sort_pages` (weight primary, date secondary).
|
||||
- `Menu_Entry.weight: Maybe(int)` — per-menu ordering. nil for auto-generated entries and string/array frontmatter forms. Explicit value from object frontmatter form `{"weight": N}`.
|
||||
- Effective weight in `merge_page_menus`: per-menu weight if set, else falls back to `page.weight`. Both `Maybe(int)`, so nil propagates naturally — no value-based sentinel check.
|
||||
- Sorted ascending via `.? or_else DEFAULT_WEIGHT`, 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}}`.
|
||||
|
||||
### Duplicate weight warnings
|
||||
|
||||
`warn_duplicate_weights` (called from `build_menus` after all menus are sorted) logs a warning when two entries in the same menu have the same explicitly-set weight. Only non-nil weights are checked — nil (unset/default) entries are never flagged, so auto-generated entries don't produce noise. The warning includes the menu name, weight value, and both entry names.
|
||||
|
||||
## Config system
|
||||
|
||||
Config is split into three structs with a clear 5-step initialization flow:
|
||||
|
||||
- **`Flags`** — CLI args only. Parsed by `core:flags`. Includes path overrides (`--content`, `--assets`, `--output`, `--layouts`), build-mode toggles (`-drafts`, `-watch`, `-minify`), and `-ext`/`-no-ext` for markdown extension overrides.
|
||||
- **`Flags`** — CLI args only. Parsed once in `main.odin` via `core:flags`, passed to `init_site`. Includes path overrides (`--content`, `--assets`, `--output`, `--layouts`), build-mode toggles (`-drafts`, `-watch`, `-minify`), log level (`-verbose` → Debug, `-quiet` → Warning), and `-ext`/`-no-ext` for markdown extension overrides.
|
||||
- **`Config_File`** — parsed from `thor.json` via `json.unmarshal_string`. Holds title, paths, `markdown_extensions` (JSON), `params` (JSON), `modules` (JSON array of relative paths), `og` (`Open_Graph` struct for site-level OG defaults).
|
||||
- **`Site`** — runtime state: arena, pages, modules, VFS, `features: bit_set[Feature]`, `markdown_extensions: bit_set[md.Extension]`, `og: Open_Graph` (resolved site-level OG).
|
||||
|
||||
**`Feature` enum** — `Drafts`, `Minify`, `Watch`. Checked with `.Minify in site.features`.
|
||||
|
||||
**`markdown.Extension` enum** (in the `markdown` package, not main) — `Emoji`, `Sidenotes`, `Alerts`, `Highlight`, `Sections`, `HeadingIDs`. Default is `md.DEFAULT_EXTENSIONS` (currently `.Emoji, .Sidenotes, .Alerts, .HeadingIDs`). Configurable via:
|
||||
**`markdown.Extension` enum** (in the `markdown` package, not main) — `Emoji`, `Sidenotes`, `Alerts`, `Highlight`, `Sections`, `HeadingIDs`, `DefLists`, `Footnotes`. Default is `md.DEFAULT_EXTENSIONS` (currently `.Emoji, .Sidenotes, .Alerts, .HeadingIDs, .DefLists`). Configurable via:
|
||||
- `thor.json`: `"markdown_extensions": { "emoji": true, "highlight": false, ... }`
|
||||
- CLI: `-ext:highlight,sections` (enable) / `-no-ext:emoji` (disable). Comma-separated, case-insensitive.
|
||||
|
||||
@@ -146,6 +201,12 @@ Config precedence: `CLI flags > thor.json values > hardcoded defaults`.
|
||||
"grammars": "~/.config/helix/runtime/grammars/",
|
||||
"queries": "/path/to/tree-sitter/queries",
|
||||
"markdown_extensions": { "emoji": true, "highlight": false },
|
||||
"menus": {
|
||||
"main": [
|
||||
{"name": "Home", "url": "/", "weight": 1},
|
||||
{"name": "About", "url": "/about/"}
|
||||
]
|
||||
},
|
||||
"params": {
|
||||
"social": [
|
||||
{ "name": "github", "url": "...", "icon": "icons/github" }
|
||||
@@ -197,10 +258,12 @@ Lives in the `markdown` package. Entry point: `md.process(body, ext, file_path)`
|
||||
|
||||
```
|
||||
raw markdown
|
||||
→ md.strip_definitions (if .Sidenotes — pre-cmark)
|
||||
→ md.strip_definitions (if .Sidenotes || .Footnotes — pre-cmark)
|
||||
→ md.convert_deflists (if .DefLists — pre-cmark)
|
||||
→ cmark markdown_to_html (Unsafe mode for HTML passthrough)
|
||||
→ md.expand_emoji (if .Emoji — post-cmark)
|
||||
→ md.inject_notes (if .Sidenotes — post-cmark)
|
||||
→ md.inject_notes (if .Sidenotes — post-cmark, sidenote rendering)
|
||||
→ md.inject_footnotes (if .Footnotes — post-cmark, standard footnote rendering)
|
||||
→ md.inject_alerts (if .Alerts — post-cmark)
|
||||
→ md.highlight_code (if .Highlight — post-cmark)
|
||||
→ md.inject_heading_ids (if .HeadingIDs — post-cmark, pre-sections)
|
||||
@@ -220,41 +283,31 @@ Templates use Mustache with template inheritance (`{{<base}}` / `{{$block}}`):
|
||||
<!-- page.html (content layout) -->
|
||||
{{<base}}
|
||||
{{$main}}
|
||||
<main><article><h1>{{page_title}}</h1>{{&content}}</article></main>
|
||||
<main><article><h1>{{page.title}}</h1>{{&content}}</article></main>
|
||||
{{/main}}
|
||||
{{/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
|
||||
Base_Data :: struct {
|
||||
now: string, // UTC ISO 8601 build timestamp
|
||||
params: json.Value,
|
||||
content: string,
|
||||
title: string,
|
||||
description: string,
|
||||
og: Open_Graph,
|
||||
date_format: string, // from site.date.format (thor.json)
|
||||
timezone: ^datetime.TZ_Region, // loaded from site.date.timezone or local, owned by Site
|
||||
}
|
||||
Page_Data :: struct {
|
||||
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
|
||||
Template_Context :: struct {
|
||||
site: Site_Context, // site-level data (title, description, base_url, params, og)
|
||||
menus: map[string][]Menu_Entry, // generated menu data (copied from site, resolves above Page.menus)
|
||||
now: string, // UTC ISO 8601 build timestamp
|
||||
date_format: string, // from site.date.format (thor.json)
|
||||
timezone: ^datetime.TZ_Region, // for format pipe
|
||||
og: Open_Graph, // computed per-page OG
|
||||
params: json.Value, // merged site + page params (resolves above Page.params)
|
||||
page: Page, // current page
|
||||
pages: [dynamic]Page, // home page list
|
||||
posts: [dynamic]Page, // section post list
|
||||
}
|
||||
```
|
||||
|
||||
`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. `Template_Context.params` is set per-page via `merge_params(site.params, page.params)` — site params overlaid with page params. Browser title is handled by the `{{> title}}` partial (not a computed field).
|
||||
|
||||
`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
|
||||
|
||||
@@ -364,12 +417,11 @@ Spec-compliant implementation at `mustache/`. See `mustache/SPEC.md` for the imp
|
||||
|---|---|
|
||||
| `mustache.odin` | Public API (`parse`, `render`, `Template`), parser (`parse_section`), renderer (`render_nodes` with `Indent_State` for partial indentation), template inheritance (`merge_block_overrides`), `delete_template`/`delete_partials`. Pipe support in Variable/Unescaped/Section/Inverted tags. |
|
||||
| `tokenizer.odin` | Tokenizer (template string → `[]Token`), standalone whitespace detection |
|
||||
| `data.odin` | Reflection-based data model: `base_value` (peels union/any/nested-any layers), `lookup_in` (structs + maps, handles `Type_Info_Any` value kind in maps), `resolve_name`, `is_truthy`, `any_to_string`, `list_info`, `extract_list_element`, `call_interp_lambda`/`call_section_lambda` |
|
||||
| `pipes.odin` | Pipes extension: `Pipe_Filter` AST, `parse_pipeline` (takes `pos`), `apply_pipeline`, `apply_filter` (switch dispatch: `group_by` + `format`), `apply_group_by`, `apply_format`. Stored on `Node.filters`; render-scoped results in temp allocator. |
|
||||
| `data.odin` | Reflection-based data model: `base_value` (peels union/any/nested-any layers), `lookup_in` (structs + maps, handles `Type_Info_Any` value kind in maps), `resolve_name`, `is_truthy`, `any_to_string`, `write_value`, `list_info`, `extract_list_element`, `collect_map_keys` |
|
||||
| `pipes.odin` | Pipes extension: `Pipe_Op` enum (`.Format`, `.Group_By`), `pipe_op_from_string`/`pipe_op_candidates` (reflection-based enum name lookup), `Pipe_Filter` AST (with `op_pos` for diagnostics), `parse_pipeline` (tracks byte offsets via `strings.index`), `apply_pipeline`, `apply_filter` (exhaustive enum switch), `apply_group_by`, `apply_format`. Misspelled pipe op suggestions via `suggest_correction`. Stored on `Node.filters`; render-scoped results in temp allocator. |
|
||||
| `diagnostic.odin` | Rust-style error formatter: `format_error` (multi-line context, ANSI colors via `core:terminal/ansi`, `colorize` param), `format_render_error` (formats `Error`), `line_col`, `line_text`, `context_extent`, `count_lines`, `digit_count`, `should_colorize`. |
|
||||
| `suggest.odin` | Strict-warning helpers: `validate_key_path` (walks dotted path, crosses maps silently), `suggest_correction` (Levenshtein via `core:strings/levenshtein_distance`), `collect_struct_keys` (via reflection, recurses into `using`), `struct_has_field` (distinguishes missing field from nil value — needed for `Maybe(bool)`), `collect_partial_names`, `collect_block_names`. |
|
||||
| `spec_test.odin` | JSON spec test runner — loads `spec/specs/*.json`, runs each test case. Uses `log.nil_logger()` to suppress expected warnings. |
|
||||
| `lambda_test.odin` | Spec lambda tests |
|
||||
| `pipes_test.odin` | Pipe filter tests (`group_by` + `format`) |
|
||||
| `diagnostic_test.odin` | Golden-output tests for `format_error` (multi-line context, edge cases, alignment, caret position, hint) + parser error message brace-escaping |
|
||||
| `suggest_test.odin` | Tests for `validate_key_path`, `suggest_correction`, `struct_has_field` with `Maybe(bool)` and `using`-promoted fields |
|
||||
@@ -397,7 +449,11 @@ render(tmpl, data, partials) → render_nodes (walks flat node array against con
|
||||
|
||||
Rust-style error messages with multi-line source context, caret underlines, and Levenshtein suggestions. ANSI colors via `core:terminal/ansi`, gated on `should_colorize()` (TTY detection on stderr).
|
||||
|
||||
**Error types**: `Error_Body{msg, pos, kind}` where `kind` is `Error_Kind.Syntax` (parse-time) or `Error_Kind.Data` (render-time). `Error` is a single-variant union wrapping `Error_Body` (nilable for `!= nil` / `or_return`).
|
||||
**Error types**: `Error_Body{msg, pos, kind, source, path, span, hint}` where `kind` is `Error_Kind.Syntax` (parse-time) or `Error_Kind.Data` (render-time). `source`/`path` carry the template the error originated in (set by `tag_error` — enables correct file/line for errors inside partials). `span` controls caret underline width (used by pipe op diagnostics). `hint` carries "did you mean?" suggestions. `Error` is a single-variant union wrapping `Error_Body` (nilable for `!= nil` / `or_return`).
|
||||
|
||||
**Error deduplication**: `render_template` takes `seen: ^map[string]bool`. Duplicate errors (same formatted diagnostic) are suppressed across page renders within a single build.
|
||||
|
||||
**Partial source tracking**: `tag_error(err, current)` stamps render-time errors with `current.source`/`current.path` at the 4 `apply_pipeline` call sites in `render_nodes`. Ensures errors inside partials point at the partial file, not the top-level template.
|
||||
|
||||
**Strict-by-default warnings** — `render_nodes` emits `log.warnf` diagnostics for:
|
||||
- Unknown keys in `{{k}}`, `{{{k}}}`, `{{#k}}`, `{{^k}}` (via `validate_key_path` + `suggest_correction`)
|
||||
@@ -407,18 +463,12 @@ Rust-style error messages with multi-line source context, caret underlines, and
|
||||
|
||||
**Exceptions** (no warning):
|
||||
- `{{.}}` and dot-prefixed names (current context)
|
||||
- Paths that cross a map (e.g., `params.*` — user-defined namespace)
|
||||
- Paths that cross a map (e.g., `params.*`) — validated for typos via Levenshtein: close matches warn with a suggestion, genuinely absent keys are suppressed silently
|
||||
- `Maybe(bool)` fields with nil value (field exists, value is nil — distinguished via `struct_has_field`)
|
||||
- Found fields whose value is nil/empty (e.g., nil `json.Value` union) — the field exists, looking up sub-keys is valid "not found" behavior
|
||||
|
||||
**Block override source tracking**: `Block_Override.source: Template` ensures warnings inside block overrides point at the override's source file (e.g., `page.html`), not the parent template (`base.html`).
|
||||
|
||||
### Lambdas
|
||||
|
||||
Spec-compliant. Stored as `any` values in the data context.
|
||||
|
||||
- **Interpolation lambdas**: `proc() -> string`, `proc() -> int`, `proc() -> bool` — called via `call_interp_lambda`, result stringified and escaped.
|
||||
- **Section lambdas**: `proc(string) -> string`, `proc(string) -> int`, `proc(string) -> bool` — called via `call_section_lambda` with the raw section text (`node.content`). String result is re-parsed as mustache and rendered against the current context stack.
|
||||
|
||||
### Pipes
|
||||
|
||||
`{{key | op args…}}` for interpolation, `{{#key | op args…}}…{{/key}}` for sections. Stored as `[dynamic; MAX_PIPES]Pipe_Filter` on each `Node`. Applied in the renderer via `apply_pipeline` before truthiness/interpolation. Implemented filters:
|
||||
@@ -435,12 +485,13 @@ See `mustache/EXTENSIONS.md`.
|
||||
|
||||
## Known limitations
|
||||
|
||||
- cmark allocates via C malloc, not the arena. HTML output leaks until process exit (problematic in watch mode — see `TODOS.md`).
|
||||
- CSS/JS cache busting uses manual `?v=N` query params instead of content hashing.
|
||||
- Tree-sitter grammar/query paths must be configured manually via `thor.json` (`grammars`, `queries`) — no auto-discovery. HTML/CSS are statically linked.
|
||||
- `map[string]any` only works through `lookup_in`'s special-case handling; thor otherwise uses structs.
|
||||
- `format_f64` in mustache brute-forces shortest float representation.
|
||||
- Content directory not mounted in VFS (modules can ship templates/assets but not content packs yet).
|
||||
- Per-page params rendering is incomplete — `merge_params` produces correct data, but `base_value` may return nil for `json.Value` fields accessed through the `[]any` context stack via reflection in some cases. Warning suppression masks this; actual rendering may not work for all param values.
|
||||
- Lambda support removed. Mustache lambdas (`proc() -> string` in data context) are not supported. Pipes cover data transformation.
|
||||
|
||||
## Design decisions
|
||||
|
||||
@@ -448,6 +499,23 @@ You may never, *ever* remove `TODO:` or `FIXME:` comments. Those are for humans,
|
||||
See `HUGO.md` for analysis of why thor doesn't need Hugo's shortcode context isolation.
|
||||
See `mustache/SPEC.md` for the original implementation specification.
|
||||
See `mustache/EXTENSIONS.md` for non-standard extensions (pipes).
|
||||
See `DIAGNOSTICS.md` for the two-tier diagnostic system design.
|
||||
See `mustache/TOKENIZERS.md` for tokenizer architecture comparison (Go, Liquid, Thor).
|
||||
|
||||
## Odin language facts
|
||||
|
||||
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`.
|
||||
- **`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.
|
||||
- `#partial switch` is usually a code smell. prefer a `case all, extra, types:` branch.
|
||||
- you don't usually need to create arena allocators in tests, instead use context.temp_allocator if you want to simplify cleanup.
|
||||
- you don't need to manually set up a tracking allocator in tests. the context.allocator will warn you about leaks.
|
||||
- **`Maybe(T)` unwrap syntax:** `value.? or_else default`. Not `value or_else default` — `or_else` works on the `?T` returned by `.?`, not on `Maybe(T)` directly.
|
||||
- **`Maybe(T)` equality:** `a == b` works directly between two `Maybe(T)` values (nil == nil → true, some(5) == some(5) → true, nil == some(5) → false). Also `a == 5` works (int coerces to `Maybe(int)`).
|
||||
- **File logger in tests:** `log.create_file_logger(&f)` + `context.logger = logger` captures log output. Must be set inline in the test proc (not via a helper proc) for context propagation. Clean up with `log.destroy_file_logger(logger)` then `os.read_entire_file_from_path` to verify output.
|
||||
- **`fmt.sbprintf` writes directly to a `strings.Builder`.** Prefer `fmt.sbprintf(&sb, fmt, args...)` over `fmt.aprintf(fmt, args...)` + `defer delete` + `strings.write_string`. The `aprintf` pattern allocates an intermediate string, requires manual cleanup, and queues a `defer delete` per loop iteration. `sbprintf` avoids all of this.
|
||||
|
||||
## TODO
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
# Diagnostics
|
||||
|
||||
Thor has two diagnostic tiers. This document explains why, and when to use each.
|
||||
|
||||
## Tier 1: Rust-style rich diagnostics (mustache engine)
|
||||
|
||||
`mustache/diagnostic.odin` implements multi-line source context, caret underlines, ANSI colors (gated on TTY detection), and Levenshtein suggestions. Used exclusively by the mustache engine for template errors:
|
||||
|
||||
- Unknown keys in `{{k}}`, `{{{k}}}`, `{{#k}}`, `{{^k}}`
|
||||
- Missing partials (`{{> name}}`)
|
||||
- Missing parent templates (`{{<name}}`)
|
||||
- Unmatched block overrides (`{{$name}}`)
|
||||
- Parse-time syntax errors
|
||||
|
||||
These benefit from rich diagnostics because **exact source location matters** — templates have complex syntax, and the user often doesn't know *where* the problem is. The diagnostic system operates on `Template.source` with byte offsets, producing output like:
|
||||
|
||||
```
|
||||
error: unknown key 'titel' in {{page.titel}}
|
||||
--> layouts/page.html:12:22
|
||||
|
|
||||
12 | <h1>{{page.titel}}</h1>
|
||||
| ^^^^^
|
||||
|
|
||||
= hint: did you mean 'title'?
|
||||
```
|
||||
|
||||
## Tier 2: Simple log warnings (content and runtime)
|
||||
|
||||
Everything outside the mustache engine uses `log.warnf` — flat one-line messages via `core:log`:
|
||||
|
||||
- Missing frontmatter dates (fallback to file mtime)
|
||||
- Duplicate menu weights
|
||||
- Non-numeric weight values in frontmatter
|
||||
- Tree-sitter highlight errors
|
||||
- Menu system issues (mixing config/frontmatter menus, etc.)
|
||||
|
||||
These are simple, actionable, and cross-file. The problem isn't *location* — it's that two files disagree, or a value is missing. A caret pointing at one file doesn't help; the message already communicates what to fix:
|
||||
|
||||
```
|
||||
[WARN] --- [menus.odin:290:warn_duplicate_weights()] menus('main'):'Ideas' and 'Stuff' share the same weight (11).
|
||||
```
|
||||
|
||||
## Why not use rich diagnostics everywhere?
|
||||
|
||||
Rust's diagnostic model is built for a single compilation unit with full AST/IR data. Three obstacles prevent reusing it for content warnings:
|
||||
|
||||
1. **Source tracking**: The mustache diagnostic system operates on `Template.source` (byte offsets into template strings). Content warnings come from frontmatter in markdown files — different source, different parser, no position tracking. Reusing the system would require building a parallel position-tracking infrastructure for frontmatter.
|
||||
|
||||
2. **Cross-file context**: Rust diagnostics point at one location. Weight duplicates are a relationship between two files. Rich diagnostics would need to show *both* file locations, which is more infrastructure for marginal value.
|
||||
|
||||
3. **Diminishing returns**: Rust diagnostics shine for syntax/type errors where the user doesn't understand the failure. Content warnings are already self-explanatory — "these two pages share weight 11" doesn't need a caret to be actionable.
|
||||
|
||||
## When to upgrade a Tier 2 warning to Tier 1
|
||||
|
||||
If a warning's usefulness would significantly improve from showing exact source location (e.g., a frontmatter syntax error where the user needs to see *which line* is malformed), consider extending the diagnostic system to frontmatter. This would require:
|
||||
|
||||
1. Position tracking in `frontmatter.odin` (store byte offsets for each parsed field)
|
||||
2. A `format_frontmatter_error` proc modeled on `format_render_error`
|
||||
3. File path propagation through the page loading pipeline
|
||||
|
||||
This is not currently planned — see `TODOS.md`.
|
||||
@@ -0,0 +1,231 @@
|
||||
name: Missing 2nd closing brace
|
||||
input: |
|
||||
<menu>
|
||||
<a class="goto-top opacity-0" href="#">{{> icons/chevron_up}</a>
|
||||
{{#params.social}}
|
||||
<a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
{{/params.social}}
|
||||
</menu>
|
||||
<p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
|
||||
expected: Somthing that points to the opening and lack of close.
|
||||
actual: |
|
||||
[ERROR] --- [383:load_partials()] unexpected {{/params.social}}
|
||||
--> /home/spencer/github.com/sbrow.github.io/layouts/partials/footer.html:6:5
|
||||
|
|
||||
4 | {{#params.social}}
|
||||
5 | <a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
6 | {{/params.social}}
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
7 | </menu>
|
||||
8 | <p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
---
|
||||
name: Missing 2nd opening brace
|
||||
input: |
|
||||
<menu>
|
||||
<a class="goto-top opacity-0" href="#">{> icons/chevron_up}}</a>
|
||||
{{#params.social}}
|
||||
<a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
{{/params.social}}
|
||||
</menu>
|
||||
<p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
expected: Somthing that shows the whole block, and points out the missing opening brace
|
||||
actual: Nothing. The page compiles with no warnings or errors
|
||||
---
|
||||
name: Invalid timezone
|
||||
input: |
|
||||
{
|
||||
"date": {
|
||||
"timezone": "America/New_Yorkside"
|
||||
}
|
||||
}
|
||||
expected: "Did you mean 'America/New_York'? pointing to line+col no in the config"
|
||||
actual: |
|
||||
[WARN ] --- [148:init_site()] unable to load timezone 'America/New_Yorkside'
|
||||
---
|
||||
name: Mismatched section tags
|
||||
input: |
|
||||
<footer>
|
||||
<menu>
|
||||
<a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
{{#params.ocial}}
|
||||
<a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
{{/params.social}}
|
||||
</menu>
|
||||
<p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
href="https://edwardtufte.github.io/tufte-css/">Tufte CSS</a>
|
||||
</p>
|
||||
<p><small>©</small> {{now | format "2006" }} {{params.author.name}}</p>
|
||||
</footer>
|
||||
expected: Mostly the same, but with rust style markers at the opening and close
|
||||
actual: |
|
||||
[ERROR] --- [383:load_partials()] expected {{/params.ocial}}, got {{/params.social}}
|
||||
--> /home/spencer/github.com/sbrow.github.io/layouts/partials/footer.html:6:5
|
||||
|
|
||||
4 | {{#params.ocial}}
|
||||
5 | <a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
6 | {{/params.social}}
|
||||
| ^^^^^^^^^^^^^^^^^^
|
||||
7 | </menu>
|
||||
8 | <p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
---
|
||||
name: Missing closing section
|
||||
input: |
|
||||
<footer>
|
||||
<menu>
|
||||
<a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
{{#params.social}}
|
||||
<a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
<!-- {{/params.social}} -->
|
||||
</menu>
|
||||
<p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
href="https://edwardtufte.github.io/tufte-css/">Tufte CSS</a>
|
||||
</p>
|
||||
<p><small>©</small> {{now | format "2006" }} {{params.author.name}}</p>
|
||||
</footer>
|
||||
expected: "Missing closing tag '{{/params.social}}' ...opened <here> ... expected close <here>"
|
||||
actual: Nothing. The page compiles with no warnings or errors
|
||||
---
|
||||
name: empty tag
|
||||
input: |
|
||||
<footer>
|
||||
<menu>
|
||||
<a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
{{#params.social}}
|
||||
<a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
{{/params.social}}
|
||||
</menu>
|
||||
<p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
href="https://edwardtufte.github.io/tufte-css/">Tufte CSS</a>
|
||||
</p>
|
||||
<p><small>©</small> {{}} {{params.author.name}}</p>
|
||||
</footer>
|
||||
expected: "Found an empty tag at <line:col>"
|
||||
actual: |
|
||||
[WARN ] --- [920:warn_unknown_key()] unknown key ''
|
||||
--> /home/spencer/github.com/sbrow.github.io/layouts/partials/footer.html:1:1
|
||||
|
|
||||
1 | <footer>
|
||||
| ^
|
||||
2 | <menu>
|
||||
3 | <a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
|
|
||||
---
|
||||
name: Missing pipe arguement
|
||||
input: |
|
||||
<footer>
|
||||
<menu>
|
||||
<a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
{{#params.social}}
|
||||
<a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
{{/params.social}}
|
||||
</menu>
|
||||
<p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
href="https://edwardtufte.github.io/tufte-css/">Tufte CSS</a>
|
||||
</p>
|
||||
<p><small>©</small> {{now | group_by }} {{params.author.name}}</p>
|
||||
</footer>
|
||||
expected: "Missing filter argument <pointing to exact location>"
|
||||
actual: |
|
||||
[ERROR] --- [152:render_template()] group_by expects 1 argument, got 0
|
||||
--> /home/spencer/github.com/sbrow.github.io/layouts/partials/footer.html:1:1
|
||||
|
|
||||
1 | <footer>
|
||||
| ^
|
||||
2 | <menu>
|
||||
3 | <a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
|
|
||||
---
|
||||
name: Double dot access
|
||||
input: |
|
||||
<footer>
|
||||
<menu>
|
||||
<a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
{{#params.social}}
|
||||
<a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
{{/params.social}}
|
||||
</menu>
|
||||
<p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
href="https://edwardtufte.github.io/tufte-css/">Tufte CSS</a>
|
||||
</p>
|
||||
<p><small>©</small> {{now | format "2006" }} {{params..name}}</p>
|
||||
</footer>
|
||||
expected: Error "Invalid access key"
|
||||
actual: Nothing. The page compiles with no warnings or errors
|
||||
---
|
||||
name: Too many opening braces
|
||||
input: |
|
||||
<footer>
|
||||
<menu>
|
||||
<a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
{{#params.social}}
|
||||
<a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
{{/params.social}}
|
||||
</menu>
|
||||
<p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
href="https://edwardtufte.github.io/tufte-css/">Tufte CSS</a>
|
||||
</p>
|
||||
<p><small>©</small> {{{{now | format "2006" }}} {{params.author.name}}</p>
|
||||
</footer>
|
||||
expected: "Too many opening braces <point to location>"
|
||||
actual: |
|
||||
[WARN ] --- [920:warn_unknown_key()] unknown key '{now'
|
||||
--> /home/spencer/github.com/sbrow.github.io/layouts/partials/footer.html:1:1
|
||||
|
|
||||
1 | <footer>
|
||||
| ^ did you mean 'now'?
|
||||
2 | <menu>
|
||||
3 | <a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
|
|
||||
---
|
||||
name: Too many closing braces
|
||||
input: |
|
||||
<footer>
|
||||
<menu>
|
||||
<a class="goto-top opacity-0" href="#">{{> icons/chevron_up}}</a>
|
||||
{{#params.social}}
|
||||
<a href="{{url}}" target="_blank" rel="noopener noreferrer me" title="{{name}}">{{>* icon}}</a>
|
||||
{{/params.social}}
|
||||
</menu>
|
||||
<p>Proudly built with <a href="https://github.com/sbrow/thor/">Thor</a> and <a
|
||||
href="https://edwardtufte.github.io/tufte-css/">Tufte CSS</a>
|
||||
</p>
|
||||
<p><small>©</small> {{now | format "2006" }}}} {{params.author.name}}</p>
|
||||
</footer>
|
||||
expected: "Too many closing braces <Point to location>"
|
||||
actual: Nothing. The page compiles with no warnings or errors
|
||||
---
|
||||
name: Frontmatter missing closing quote on key
|
||||
input: |
|
||||
{
|
||||
"title: Docs"
|
||||
}
|
||||
|
||||
[TOC]
|
||||
|
||||
## Introduction
|
||||
expected: 'TODO:'
|
||||
actual: |
|
||||
[ERROR] --- [42:parse_frontmatter()] failed to parse frontmatter JSON: Expected_Colon_After_Key
|
||||
---
|
||||
name: Fronmatter missing opening quote on value
|
||||
input: |
|
||||
{
|
||||
"title": Docs"
|
||||
}
|
||||
|
||||
[TOC]
|
||||
|
||||
## Introduction
|
||||
expected: 'detailed diagnostic pointing to the point of failure and suggesting the addition of a quote'
|
||||
actual: Nothing. The page compiles with no warnings or errors
|
||||
---
|
||||
name: No error when missing index page.
|
||||
expected: 'Your site has no index page! Please create an index.md or index.html file in ./content/'
|
||||
actual: Nothing. The site compiles with no warnings or errors
|
||||
---
|
||||
name: format pipe with no date format set
|
||||
expected: |
|
||||
Show where the error was triggered. Should also show the format that will be used
|
||||
actual: |
|
||||
[ERROR] --- [333:apply_format()] format pipe used but no date format configured (set date.format in thor.json) Default will be used
|
||||
@@ -1,87 +0,0 @@
|
||||
# Timezone Support
|
||||
|
||||
## Goal
|
||||
|
||||
Add timezone conversion to the `format` pipe so dates display in the configured site timezone, with correct abbreviations for the `MST` token.
|
||||
|
||||
## Context
|
||||
|
||||
Dates are stored as raw ISO 8601 strings. The `format` pipe formats them for display using Go-style layout tokens. Currently `parse_iso_date` ignores the timezone offset suffix, and `MST` is hardcoded to `"UTC"`.
|
||||
|
||||
Pipes now take `ctx: []any`, so timezone config can flow through the data context identically to `date_format` — no new parameters to thread.
|
||||
|
||||
## Decisions
|
||||
|
||||
- **MST fallback** (no target tz, date has offset): `UTC-04:00` format
|
||||
- **`now` field**: intentionally UTC (offset=0). The `format` pipe handles timezone display.
|
||||
- **TZ_Region cache**: lives in the mustache package (`format.odin`)
|
||||
|
||||
## Files changed
|
||||
|
||||
| File | Changes |
|
||||
|---|---|
|
||||
| `mustache/format.odin` | Extend `Date_Components` (+`offset_seconds`, `has_offset`, `tz_abbr`). Extend `parse_iso_date` to parse trailing offset. Add `tz_cache`, `get_cached_tz`, `convert_to_tz`, `format_offset`, `destroy_tz_cache`. Fix MST token. Import `core:time/timezone`. |
|
||||
| `mustache/pipes.odin` | Resolve `date_timezone` from ctx in `"format"` case (optional — nil is fine). Add `timezone_name` param to `apply_format`. Orchestrates parse → convert → format. |
|
||||
| `render.odin` | Add `date_timezone: string` to `Base_Data`, populate from `site.date.timezone`. Remove `// TODO: CAlculate offset` (offset=0 is intentional — UTC). |
|
||||
| `site.odin` | Call `mustache.destroy_tz_cache()` from `destroy_site`. No structural changes — `Date_Preferences.timezone` already exists and flows through config. |
|
||||
| `mustache/pipes_test.odin` | Add `date_timezone: string` to test data structs. Add timezone conversion tests. |
|
||||
|
||||
## Conversion logic (in `apply_format`)
|
||||
|
||||
```
|
||||
1. parse_iso_date(iso) → components (now includes offset_seconds, has_offset)
|
||||
2. tz_name := resolve "date_timezone" from ctx (optional)
|
||||
3. target_tz := get_cached_tz(tz_name) // nil if empty/UTC/not configured
|
||||
4. if target_tz != nil:
|
||||
components = convert_to_tz(components, target_tz)
|
||||
// tz_abbr filled by convert_to_tz via timezone.shortname()
|
||||
5. else if components.has_offset:
|
||||
components.tz_abbr = format_offset(components.offset_seconds)
|
||||
// e.g. "UTC-04:00"
|
||||
6. else:
|
||||
components.tz_abbr = "UTC"
|
||||
7. format_date(components, fmt)
|
||||
```
|
||||
|
||||
## `convert_to_tz` flow
|
||||
|
||||
```
|
||||
1. Build DateTime from components (tz=nil=UTC)
|
||||
2. If has_offset: add offset_seconds to get true UTC
|
||||
3. timezone.datetime_to_tz(utc_dt, target_tz) → converted DateTime
|
||||
4. Extract components from converted DateTime
|
||||
5. tz_abbr = timezone.shortname(converted_dt) // "EST", "EDT", etc.
|
||||
```
|
||||
|
||||
## MST fallback: `format_offset`
|
||||
|
||||
```
|
||||
0 → "UTC"
|
||||
-14400 → "UTC-04:00"
|
||||
+19800 → "UTC+05:30"
|
||||
```
|
||||
|
||||
## `now` field
|
||||
|
||||
`now` stays UTC (offset=0). Remove the `// TODO: CAlculate offset` comment — it's correct as-is. Templates format it with `{{now | format}}` and the pipe handles timezone display.
|
||||
|
||||
## Behavior matrix
|
||||
|
||||
| Config TZ | ISO has offset | Conversion | `MST` output |
|
||||
|---|---|---|---|
|
||||
| `"America/New_York"` | yes (`-04:00`) | UTC → NY (DST-aware) | `"EST"`/`"EDT"` |
|
||||
| `"America/New_York"` | no | assume already in NY | `"EST"`/`"EDT"` |
|
||||
| not set | yes (`-04:00`) | none — display as-is | `"UTC-04:00"` |
|
||||
| not set | no | none | `"UTC"` |
|
||||
|
||||
## Imports added
|
||||
|
||||
- `mustache/format.odin`: `import "core:time/timezone"` (for `region_load`, `datetime_to_tz`, `shortname`, `region_destroy`)
|
||||
|
||||
## Odin timezone API reference
|
||||
|
||||
- `timezone.region_load(name: string) -> (^datetime.TZ_Region, bool)` — `"local"` reads `$TZ` env, falls back to `/etc/localtime`
|
||||
- `timezone.region_destroy(region: ^datetime.TZ_Region)`
|
||||
- `timezone.datetime_to_tz(dt: DateTime, tz: ^TZ_Region) -> (DateTime, bool)` — DST-aware. If `dt.tz == tz`, no-op. If `dt.tz == nil`, treats as UTC.
|
||||
- `timezone.shortname(dt: DateTime) -> (string, bool)` — abbreviation from TZ_Region records (e.g. `"EST"`, `"EDT"`)
|
||||
- `datetime.DateTime :: struct { using date: Date, using time: Time, tz: ^TZ_Region }` — `tz == nil` means UTC
|
||||
+317
@@ -0,0 +1,317 @@
|
||||
# Thor — UX Problems Catalog
|
||||
|
||||
Adversarial review of error messages, behavioral inconsistencies, and user
|
||||
frustration points. Established as a baseline on commit `314cab2`.
|
||||
|
||||
Each entry cites the source location so it can be tracked to a fix.
|
||||
|
||||
---
|
||||
|
||||
## Severity legend
|
||||
|
||||
- **Critical** — user mistake produces silent wrong output or an unhelpful
|
||||
fatal error with no path forward.
|
||||
- **High** — error or warning is emitted but missing "where" or "how to fix."
|
||||
- **Medium** — inconsistency or gotcha that causes confusion or rework.
|
||||
- **Low** — polish / minor frustration.
|
||||
|
||||
---
|
||||
|
||||
## A. Silent wrong output (no error, wrong result)
|
||||
|
||||
These are the most dangerous — the user gets *no signal* that something is wrong.
|
||||
|
||||
### A1. Non-JSON frontmatter silently treated as body content — Critical
|
||||
`frontmatter.odin:26`
|
||||
|
||||
Thor expects JSON frontmatter delimited by bare `{` / `}` lines. A user
|
||||
coming from Hugo/Jekyll writes YAML (`---`) or TOML (`+++`) frontmatter. It is
|
||||
silently swallowed into the markdown body. No title, no date, no draft flag —
|
||||
and no error. Likely the #1 onboarding trap.
|
||||
|
||||
### A2. Unknown `thor.json` keys silently ignored — Critical
|
||||
`site.odin:162`
|
||||
|
||||
`json.unmarshal_string` skips unknown fields. A typo like `"tittle"` instead
|
||||
of `"title"` produces a silently-empty title. No warning. (`TODOS.md` already
|
||||
wants a JSON schema.)
|
||||
|
||||
### A3. Draft pages silently excluded — High
|
||||
`content.odin:64`
|
||||
|
||||
When `-drafts` isn't passed, draft pages vanish with no log. User adds a
|
||||
page, forgets the flag, page doesn't appear — zero feedback.
|
||||
|
||||
### A4. Naive singularization for layout inference — High
|
||||
`content.odin:202`
|
||||
|
||||
`posts` → `post` (correct), but `series` → `serie`, `news` → `new`. The
|
||||
layout silently falls through the fallback chain to `page`/`base`. No
|
||||
"layout 'serie' not found for section 'series'" message — only a debug log
|
||||
that's off by default.
|
||||
|
||||
### A5. `base_url` defaults to `localhost:8080` — Critical
|
||||
`site.odin:100`
|
||||
|
||||
Forgetting to set it means every canonical URL, OG tag, and RSS link points
|
||||
to localhost. No warning. Devastating in production builds.
|
||||
|
||||
### A6. Missing `content/` produces empty build — High
|
||||
`content.odin:82`
|
||||
|
||||
`scan_content_files` logs a `warnf`, the build proceeds with zero pages, then
|
||||
`log.infof("Rendered 0 pages")`. No fatal error, no "did you create
|
||||
content/?" guidance.
|
||||
|
||||
### A7. RSS emits sentinel epoch date silently — Medium
|
||||
`feed.odin:33`
|
||||
|
||||
Pages without a date get `"Mon, 01 Jan 0001 00:00:00 +0000"` in `<pubDate>`.
|
||||
No warning that a page is dateless in the feed.
|
||||
|
||||
### A8. `format_rfc822` returns raw ISO on parse failure — Medium
|
||||
`feed.odin:122-125`
|
||||
|
||||
`// TODO: should indicate error somehow` — short/malformed dates get embedded
|
||||
verbatim in `<pubDate>`, producing invalid RSS with no warning.
|
||||
|
||||
---
|
||||
|
||||
## B. Error messages missing "where" or "how to fix"
|
||||
|
||||
### B1. `render_template` blanks the entire page on error — Critical
|
||||
`render.odin:119-133`
|
||||
|
||||
A single bad tag/pipe anywhere produces `log.errorf` + `return ""`. The
|
||||
output file is silently written empty. In a `nix build` (no visible
|
||||
terminal), the user sees a blank page with zero clue why. Already noted in
|
||||
`TODOS.md`.
|
||||
|
||||
### B2. Malformed `thor.json` degrades to defaults — Critical
|
||||
`site.odin:162-168`
|
||||
|
||||
A JSON syntax error is a `warnf`, then `site_apply_path_defaults` kicks in.
|
||||
The site builds with wrong paths and produces a confusing empty result — the
|
||||
cause is two hops removed from the symptom.
|
||||
|
||||
### B3. Frontmatter parse error has no file location — Critical
|
||||
`frontmatter.odin:41`
|
||||
|
||||
`"failed to parse frontmatter JSON: %v"` — no filename. On a 100-post site
|
||||
the user can't find the bad file. Worse: `ok=false` silently drops the page
|
||||
entirely.
|
||||
|
||||
### B4. `get_template` returns empty `Template{}` on missing base — High
|
||||
`render.odin:88-89`
|
||||
|
||||
`"base.html not found in VFS"` — no guidance on how to fix (create the file,
|
||||
check modules, etc.).
|
||||
|
||||
### B5. `dlopen` failures lack the OS reason and fix guidance — High
|
||||
`treesitter/treesitter.odin:200-219`
|
||||
|
||||
"cannot load grammar %s (%s)" shows the path but not *why* (no `dlerror()`).
|
||||
No guidance: "set the 'grammars' key in thor.json" or "this .so may be for a
|
||||
different tree-sitter ABI."
|
||||
|
||||
### B6. Menu-mix fatal lacks location — High
|
||||
`menus.odin:42`
|
||||
|
||||
`"cannot mix config menus with frontmatter menus"` — doesn't name which pages
|
||||
have frontmatter menus.
|
||||
|
||||
### B7. Minify error doesn't name the page — Medium
|
||||
`minify.odin:33`
|
||||
|
||||
"minify: HTML parse errors, skipping minification" — across 50 pages, which
|
||||
one?
|
||||
|
||||
### B8. Timezone load failure is a warning with no guidance — Medium
|
||||
`site.odin:148`
|
||||
|
||||
Doesn't state impact (dates render in UTC) or suggest valid names. Already
|
||||
in `TODOS.md`.
|
||||
|
||||
### B9. No "config not found" message — Medium
|
||||
`site.odin:113`
|
||||
|
||||
Silently falls back to `./thor.json`. Wrong-directory runs produce a
|
||||
confusing default build.
|
||||
|
||||
---
|
||||
|
||||
## C. Silent skip of invalid user input
|
||||
|
||||
### C1. Unknown markdown extensions silently ignored (CLI) — High
|
||||
`markdown/markdown.odin:49-68`
|
||||
|
||||
`parse_extension_list` has a switch with no default case. `-ext:higlight`
|
||||
(typo for `highlight`) is silently a no-op.
|
||||
|
||||
### C2. Unknown markdown extensions silently ignored (config) — High
|
||||
`markdown/markdown.odin:71-90`
|
||||
|
||||
`apply_extension_config` has a `// TODO: Silently discards invalid values.`
|
||||
Unknown keys in `thor.json`'s `markdown_extensions` are silently dropped.
|
||||
Non-boolean values are `or_continue`d.
|
||||
|
||||
---
|
||||
|
||||
## D. Naming inconsistencies
|
||||
|
||||
### D1. Markdown extensions have 3+ names — Medium
|
||||
|
||||
| Context | Name |
|
||||
|---|---|
|
||||
| `thor.json` key | `markdown_extensions` |
|
||||
| CLI flag | `-ext` / `-no-ext` |
|
||||
| Struct fields | `md_enable` / `md_disable` |
|
||||
| JSON/CLI values | `emoji`, `sidenotes` (lowercase) |
|
||||
| Enum members | `.Emoji`, `.Sidenotes` (PascalCase) |
|
||||
|
||||
### D2. `-ext` usage string omits `heading_ids` — Medium
|
||||
`site.odin:91`
|
||||
|
||||
The help text lists `emoji,sidenotes,alerts,highlight,sections` but the enum
|
||||
also has `HeadingIDs`. Users can't discover it from `--help`.
|
||||
|
||||
### D3. Starred field has three names — Low
|
||||
- `Page.starred` (`content.odin:28`)
|
||||
- `Frontmatter.isStarred` (`frontmatter.odin:17`) — so the JSON key is `isStarred`
|
||||
- `AGENTS.md:101` says `is_starred` (stale)
|
||||
|
||||
### D4. Inconsistent error severity for similar failures — Medium
|
||||
- Template **parse** error → `log.errorf` + `os.exit(1)` (fatal) — `render.odin:37-49`
|
||||
- Template **render** error → `log.errorf` + return `""` (non-fatal, blank page) — `render.odin:126-131`
|
||||
- Config parse error → `warnf` + fallback to defaults — `site.odin:163-165`
|
||||
|
||||
Same category of failure (user wrote something wrong) with wildly different
|
||||
consequences.
|
||||
|
||||
### D5. Dead `os.exit(1)` after `log.fatalf` — Low
|
||||
`render.odin:33`, `menus.odin:43`
|
||||
|
||||
`fatalf` already exits; the following line is dead code.
|
||||
|
||||
---
|
||||
|
||||
## E. Configuration gotchas
|
||||
|
||||
### E1. `"menus": {}` is a stealth opt-out — Medium
|
||||
`menus.odin:31-38`
|
||||
|
||||
An empty object silently disables *all* auto-menus. A user who adds the key
|
||||
intending to configure later quietly loses their nav. The semantics (absent
|
||||
≠ empty) are undocumented outside code comments.
|
||||
|
||||
### E2. Config precedence is invisible — Medium
|
||||
`site.odin`
|
||||
|
||||
CLI > JSON > defaults, but there's no "resolved config" log. Debugging "why
|
||||
is my base_url wrong?" requires reading source.
|
||||
|
||||
### E3. `format` pipe logs ERROR but still renders — Medium
|
||||
`pipes.odin:261-265`
|
||||
|
||||
Missing `date.format` produces `log.errorf` but falls back to
|
||||
`DEFAULT_DATE_FORMAT`. The severity says "error" but the behavior says
|
||||
"warning."
|
||||
|
||||
---
|
||||
|
||||
## F. File/directory behavior surprises
|
||||
|
||||
### F1. Root dirs = sections, nested dirs = leaf bundles — Medium
|
||||
`content.odin:108-127`
|
||||
|
||||
This meaningful semantic distinction is entirely implicit.
|
||||
`content/about/team.md` is a leaf bundle (page "about" with body from
|
||||
team.md), not a section "about" with page "team". No error or guidance when
|
||||
the user's mental model differs.
|
||||
|
||||
### F2. Missing `layouts/` silently uses defaults — High
|
||||
`vfs.odin:38-40`
|
||||
|
||||
`mount_dir` returns silently if the directory doesn't exist. Wrong path →
|
||||
all user templates missing → defaults used. No "layouts directory X not
|
||||
found" message.
|
||||
|
||||
### F3. Missing section index silently synthesized — Medium
|
||||
`render.odin:316-326`
|
||||
|
||||
A section with pages but no `index.md` gets a synthetic `Page` with only a
|
||||
title. No warning. User expecting an error gets a mostly-blank page.
|
||||
|
||||
### F4. Windows line endings silently break frontmatter — Medium
|
||||
`frontmatter.odin:26`
|
||||
|
||||
`has_prefix(content, "{\n")` fails on `\r\n`; the JSON is treated as body.
|
||||
Zero feedback.
|
||||
|
||||
---
|
||||
|
||||
## G. Template authoring frustrations
|
||||
|
||||
### G1. Template fallback chain is silent at Info level — Medium
|
||||
`render.odin:84`
|
||||
|
||||
Only `log.debugf`, which is off by default (`main.odin:48` sets `.Info`).
|
||||
User's custom layout silently ignored, defaults used.
|
||||
|
||||
### G2. Render error blanks entire page — Critical
|
||||
`render.odin:126-131`
|
||||
|
||||
(Same as B1 — restated here for the template-authoring perspective.) One bad
|
||||
tag → whole page `""`. The most impactful silent failure in the system.
|
||||
|
||||
### G3. Pipe errors lack "did you mean?" suggestions — High
|
||||
`mustache/pipes.odin:241`
|
||||
|
||||
Unknown key errors (`{{tittle}}`), missing partials, and unmatched block
|
||||
overrides all get Levenshtein "did you mean?" hints via `suggest_correction`.
|
||||
But unknown pipe operations (`{{date | formats}}`) get only `"unknown pipe op
|
||||
'formats'"` with no suggestion. The known filter names (`"format"`,
|
||||
`"group_by"`) are a small fixed set — perfect for suggestions.
|
||||
|
||||
Structural gap: `Error_Body` has no `hint` field, and
|
||||
`format_render_error` doesn't pass `hint` to `format_error` (defaults to
|
||||
`""`). So even if a suggestion were computed, there's nowhere to put it
|
||||
without either appending to `msg` or adding `hint` to `Error_Body`.
|
||||
|
||||
### G4. Triple-mustache `{{{` mishandled by `tag_content_base` — Medium
|
||||
`mustache/mustache.odin:230`
|
||||
|
||||
`tag_content_base` skips `{{` and sigils (`#^/&><$!`) to find where tag
|
||||
content begins. But triple-mustache `{{{key}}}` is common — after `{{`,
|
||||
the next char is `{`, which is not in the sigil list, so `base` points at
|
||||
`{` instead of the actual key content. Any pipe position calculation for
|
||||
`{{{key | format}}}` will be off by one byte.
|
||||
|
||||
---
|
||||
|
||||
## Cross-cutting themes
|
||||
|
||||
1. **Debug-level logging masks important fallbacks.** Layout fallbacks,
|
||||
template misses, and grammar skips are all `debugf` — invisible at the
|
||||
default Info level. Users never learn their customizations were ignored.
|
||||
|
||||
2. **The system fails open, not closed.** Missing files, missing
|
||||
directories, missing config — all silently fall back to defaults rather
|
||||
than surfacing the problem. Friendly until it isn't.
|
||||
|
||||
3. **No "resolved state" visibility.** There's no way for a user to see what
|
||||
thor actually loaded: which layouts, which config values, which pages
|
||||
were skipped as drafts. The build is a black box.
|
||||
|
||||
---
|
||||
|
||||
## Gold-standard examples to emulate
|
||||
|
||||
These are the parts of the codebase that already do it right:
|
||||
|
||||
- **Mustache diagnostics** (`mustache/diagnostic.odin` + `suggest.odin`):
|
||||
rust-style multi-line context, caret underlines, Levenshtein "did you
|
||||
mean?" hints, file:line:col.
|
||||
- **Treesitter query version-mismatch** (`treesitter/treesitter.odin:299-315`):
|
||||
explains the likely cause, shows both grammar/query versions, flags
|
||||
mismatches explicitly.
|
||||
@@ -1,8 +1,6 @@
|
||||
# Thor
|
||||
|
||||
[TOC]
|
||||
|
||||
Thor is a simple Static Sire Generator designed for personal blogs and other small websites.
|
||||
Thor is a simple Static Site Generator designed for personal blogs and other small websites.
|
||||
|
||||
Its core principals are simplicity and minimal configuration, so you can get started as quickly as possible.
|
||||
|
||||
@@ -16,14 +14,14 @@ It is based on Hugo, and gingerbill's SSG. Templating is done with (extended?) M
|
||||
- Menus (WIP)
|
||||
- Extended Markdown ([See below](#extended-markdown))
|
||||
- Basic (whitespace) minification.
|
||||
- Union File System (Modules)
|
||||
|
||||
## What it doesn't do
|
||||
- Internationalization
|
||||
- Pagination (Yet)
|
||||
- Themes
|
||||
- Union File System (Yet)
|
||||
- Image Manipulation
|
||||
- TailwindCSS integration
|
||||
- Pagination (Yet)
|
||||
- Themes
|
||||
- Image Manipulation
|
||||
- TailwindCSS integration
|
||||
|
||||
## Getting Started
|
||||
|
||||
@@ -34,7 +32,8 @@ Then follow [The Guide]()
|
||||
For a more complete setup, run `thor new site`.
|
||||
|
||||
## Extended Markdown
|
||||
|
||||
- Emoji expansion
|
||||
- margin style footnotes
|
||||
- Guthub style alerts
|
||||
- Github style alerts
|
||||
- [and more]
|
||||
|
||||
@@ -1,13 +1,64 @@
|
||||
## High priority
|
||||
|
||||
- Polish existing features before moving on to new ones.
|
||||
- [ ] Do mustache's whitespace rules actually suit us, or should we make our own?
|
||||
- [x] `{{>title}}` default partial? `{{site.title}} | {{ page.title }}`
|
||||
- [ ] implicit titles (Set when missing?)
|
||||
- [ ] create a default `head.html`.
|
||||
- [ ] html comments are rendered, except in minify mode.
|
||||
- [ ] does it make sense for partials to be inside layouts?
|
||||
- current: `layouts/partials`
|
||||
- alt1: `layouts`, `partials`
|
||||
- alt2: `templates/layouts`, `templates/partials`
|
||||
- [ ] [aliases](https://gohugo.io/methods/page/aliases/#redirects)?
|
||||
- [ ] Improve diagnostics
|
||||
- [ ] Simplify / unify template context stack. Come up with a name for it.
|
||||
- [ ] `render_template` should accept `Template_Context`, not `any`
|
||||
- [x] keep track of every error and don't report them more than once.
|
||||
- [ ] show parsed arg when -extension is unrecognized
|
||||
- [ ] also do typo detection?
|
||||
- [ ] `*` make sure the frontmatter parser has good diagnostics.
|
||||
- [ ] fix the diagnostics in [DIAGNOSTIC TODOS](./DIAGNOSTIC_TODOS.yaml)
|
||||
- [ ] only report format errors once.
|
||||
- [ ] only report missing partiall errors once.
|
||||
- [ ] All Diagnostics should show:
|
||||
- [ ] *What* went wrong
|
||||
- [ ] *where* (in the file)
|
||||
- [ ] *where* (in the stack trace)
|
||||
- [ ] *how* you can fix it (if applicable)
|
||||
- [ ] Create a Location struct that somewhat matches Odin's [Source_Code_Location](https://pkg.odin-lang.org/base/runtime/#Source_Code_Location)?
|
||||
- Note that odin's version doesn't contain the stack trace.
|
||||
- [ ] show "stack traces" in template error diagnostics
|
||||
- [ ] better diagnostics for syntax errors in treesitter.
|
||||
- [ ] Ensure diagnostics for MAX_CONTEXT_DEPTH are good.
|
||||
- [ ] improve matching weights message.
|
||||
- [ ] Test menu diagnostics
|
||||
- [ ] Honestly, Test **all** diagnostics
|
||||
- [ ] Need to be careful about diagnostics across module boundaries.
|
||||
- we don't necessarily want to warn users about theme designers mistakes. (though perhaps we do)
|
||||
- [ ] consider reporting duplicate weights outside of menus
|
||||
- [ ] Extend `tag_error` to all render-time errors, not just pipe errors.
|
||||
Currently only pipe errors (4 sites in `render_nodes`) get stamped with
|
||||
the correct template source/path. Other render errors still use the
|
||||
content template's source/path, which can point at the wrong file.
|
||||
- [ ] try to make file paths clickable links.
|
||||
- [ ] centralize diagnostics to one place.
|
||||
- [ ] consider logging the number of times an error occurred.
|
||||
- [ ] Load grammars dynamically
|
||||
- [ ] consider adding a limit to the context stack in mustache.
|
||||
- [ ] better diagnostics for syntax errors in treesitter.
|
||||
- [x] Add heading ids as a default on extension.
|
||||
- [x] starred must be a param.
|
||||
- [ ] Documentation
|
||||
- [ ] talk about the context stack (and its limit).
|
||||
- [ ] highlight the differences in the way menus are handled.
|
||||
- [ ] consider sites with date based urls.
|
||||
- [x] Don't show annoying log output in tests.
|
||||
- [ ] improve home link customization.
|
||||
- [ ] currently an accessibility issue.
|
||||
- [x] support JSON5 in in frontmatter
|
||||
- [ ] Create a json schema file for `thor.json`.
|
||||
- [x] cleanup `#partial switch`es.
|
||||
- [ ] improve json diagnostics.
|
||||
- i.e. "Missing quotes around string", etc.
|
||||
- [ ] don't use bullshit "sub-tokens", add filters and pipes as proper tokens.
|
||||
- [ ] Ideas is now in the wrong spot. Date is wrong, and it is showing date
|
||||
when it shouldn't be.
|
||||
|
||||
## Performance
|
||||
|
||||
@@ -31,6 +82,13 @@
|
||||
- `await` the highlighted code.
|
||||
- [ ] can markdown extensions run in parallel?
|
||||
- [ ] enforce MAX_SLUG_LENGTH
|
||||
- [ ] enforce MAX_CONTEXT_DEPTH
|
||||
- [ ] ensure struct fields are ordered correctly
|
||||
|
||||
## Remove Privileged content
|
||||
|
||||
- [ ] `group_by` currently requires a computed `year` field on the page.
|
||||
- We should replace this with `{{ pages | group_by (date | "2006") }}` or similar
|
||||
|
||||
|
||||
## Memory Management
|
||||
@@ -50,20 +108,33 @@
|
||||
|
||||
## Markdown
|
||||
- [ ] Add overloads for every extension - accept ^strings.Builder.
|
||||
- [ ] Add conventional (Hugo style) footnotes option.
|
||||
- [ ] Add opt-in deflist support.
|
||||
- [ ] Decide if lambdas actually provide any value.
|
||||
- [x] Add conventional (Hugo style) footnotes option.
|
||||
- [x] Add opt-in deflist support.
|
||||
- [x] Decide if lambdas actually provide any value.
|
||||
- [ ] add tables extension
|
||||
- [x] Table of contents support.
|
||||
- [ ] enable template level rendering of TOCs
|
||||
- [ ] Write css for toc sidebar and figure out where to put it.
|
||||
- [ ] Add [hugo style configuration](https://gohugo.io/configuration/markup/#table-of-contents)
|
||||
- [ ] Link checker?
|
||||
- Checks all links on each page to make sure they are valid.
|
||||
- [ ] Peruse [GitHub's](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts)
|
||||
docs for any juicy nuggets we may have missed.
|
||||
- [ ] Avoid using `render_inline_md` if possible.
|
||||
|
||||
## Dates
|
||||
- [ ] display an error when no part of the date appears in the output.
|
||||
- [ ] Handle 0 and whitespace padding i.e. "_2" -> " 2"
|
||||
- [ ] Do we *need* mustache.Date_Components, or can we use core:time/datetime.DateTime?
|
||||
- [ ] show a proper diagnostic for timezones
|
||||
- currently "unable to load timezone 'America/New_Yorkskie'"
|
||||
- want rust style diagnostic and better message, maybe "unknown timezone 'America/New_Yorkskie'"
|
||||
|
||||
## General
|
||||
- [ ] Menus
|
||||
- [ ] configure opt-out of automatic sections being added to menu.
|
||||
- [ ] nested menus (i.e. `parent` support)
|
||||
- [ ] get rid of the global variables in the `treesitter` package.
|
||||
- [ ] enforce heading structure.
|
||||
- [ ] Either frontmatter.title set, or 1 h1 tag at top, not both
|
||||
- [ ] No skipping.
|
||||
- [ ] Consider using `or_else` when applying default values to structs. i.e.
|
||||
```odin
|
||||
package main
|
||||
@@ -81,26 +152,38 @@ main :: proc () {
|
||||
- [ ] Integrity hash
|
||||
- Allows users to verify their output didn't change after upgrading to a new version
|
||||
- [ ] Content-hash fingerprinting for CSS and JS cache busting
|
||||
- [ ] come up with scrapers / scrape sources to harvest site data
|
||||
- we'll use this to help us sculpt defaults.
|
||||
- [ ] merge `render_{section,home_html,page_html}` procs.
|
||||
- [ ] try to combine render_page_html and render_home_html?
|
||||
- [ ] Debug log stats. (analytics)
|
||||
- [ ] final Context_Stack cap
|
||||
- [ ] highest PIPE args used
|
||||
- [ ] longest slug length + name that generated it
|
||||
- [ ] number of pages
|
||||
- [ ] number of blocks
|
||||
- [ ] enabled features / extensions
|
||||
- [ ] etc
|
||||
- [ ] 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.
|
||||
- [ ] Add page params
|
||||
- [x] Add page params
|
||||
- [ ] We must remove all mention of `posts` from the odin code.
|
||||
At present, "posts" are a user-level construct defined as pages in a
|
||||
particular collection.
|
||||
- [ ] running ./thor/thor still logs the debug message: using config /home/spencer/github.com/sbrow.github.io/thor.json
|
||||
- wrong cwd?
|
||||
- [ ] Clean up the default layouts
|
||||
- [ ] Menus
|
||||
- [x] Detailed frontmatter menu form ("menu": {"main": {"weight": 5}})
|
||||
- [ ] Menu active state (pre-compute is_active based on page.permalink prefix match)
|
||||
- [x] Page.weight field for general-purpose page ordering (menus, lists, related posts)
|
||||
- [ ] if no `html` tag detected in output, re-render output with base template
|
||||
(or whatever template is next in the chain)
|
||||
- [ ] Add `-production` flag
|
||||
- sets `-minify`
|
||||
- [ ] Mustache diagnostics
|
||||
- [ ] Partial invocation stack in diagnostics: when an error fires inside a partial, show "invoked from" chain through `{{> name}}` calls. Currently warnings inside partials point at the partial (correct file) but don't show the invocation site.
|
||||
- [ ] Could be better error message when missing a closing (or opening) brace
|
||||
- [ ] Error message doesn't show position of faulty pipe name correctly.
|
||||
- [x] Error message doesn't show position of faulty pipe name correctly.
|
||||
- [ ] `render_template` (`render.odin`) blanks the *entire page* to `""` on any
|
||||
mustache render error and only `log.errorf`s it — a single bad tag/pipe
|
||||
anywhere on the page silently kills the whole output with no visible
|
||||
@@ -120,7 +203,6 @@ main :: proc () {
|
||||
- [ ] include-code shortcode (`{{< include-code ... >}}`) — i-ported-fd-to-odin
|
||||
- [ ] follow symlinks in `scan_content`?
|
||||
- [ ] ensure sidenote numbers render in display order and not in declaration order.
|
||||
- [ ] Table of contents support.
|
||||
- [ ] Nav items should be active when the current page is selected.
|
||||
- [ ] Theme selector for syntax highlighting.
|
||||
- use http://github.com/helix-editor/helix/tree/master/runtime/themes) as a
|
||||
@@ -138,13 +220,15 @@ main :: proc () {
|
||||
- [x] basic poll loop
|
||||
- [ ] filesystem poll loop
|
||||
- [ ] event based
|
||||
- [ ] Free cmark HTML output (`body_html`) — cmark allocates via C malloc, not the arena, so it leaks per iteration in watch mode
|
||||
- [x] Free cmark HTML output (`body_html`) — cmark allocates via C malloc, not the arena, so it leaks per iteration in watch mode
|
||||
- [ ] manually pass `site_allocator` to `load_page`
|
||||
- [ ] Mount content in VFS
|
||||
- [ ] commands
|
||||
- [ ] `build` alias of default
|
||||
- [ ] `new site` set up new project
|
||||
- [ ] warn/error when unknown key used in mustache.
|
||||
- [ ] Import/export packages. Hugo, jekyll, WordPress, etc.
|
||||
- [ ] opt-in "strict_keys" mode. in this mode, key lookups may not view parent objects.
|
||||
|
||||
## Notes
|
||||
|
||||
|
||||
+47
-26
@@ -1,11 +1,11 @@
|
||||
package bench
|
||||
|
||||
import "../mustache"
|
||||
import "core:fmt"
|
||||
import "core:mem"
|
||||
import "core:os"
|
||||
import "core:strconv"
|
||||
import "core:time"
|
||||
import "../mustache"
|
||||
|
||||
Tag :: struct {
|
||||
name: string,
|
||||
@@ -101,13 +101,13 @@ main :: proc() {
|
||||
return
|
||||
}
|
||||
|
||||
for _ in 0..<3 {
|
||||
for _ in 0 ..< 3 {
|
||||
_, _ = mustache.render(page, data, partials, allocator = context.temp_allocator)
|
||||
mem.dynamic_arena_free_all(&temp_arena)
|
||||
}
|
||||
|
||||
start := time.now()
|
||||
for _ in 0..<iterations {
|
||||
for _ in 0 ..< iterations {
|
||||
_, _ = mustache.render(page, data, partials, allocator = context.temp_allocator)
|
||||
mem.dynamic_arena_free_all(&temp_arena)
|
||||
}
|
||||
@@ -116,8 +116,12 @@ main :: proc() {
|
||||
seconds := time.duration_seconds(elapsed)
|
||||
per_render_ms := seconds * 1000 / f64(iterations)
|
||||
|
||||
fmt.printfln("iterations=%d total=%.3fs per_render=%.3fms",
|
||||
iterations, seconds, per_render_ms)
|
||||
fmt.printfln(
|
||||
"iterations=%d total=%.3fs per_render=%.3fms",
|
||||
iterations,
|
||||
seconds,
|
||||
per_render_ms,
|
||||
)
|
||||
}
|
||||
|
||||
parse_file :: proc(name: string) -> mustache.Template {
|
||||
@@ -138,16 +142,27 @@ parse_file :: proc(name: string) -> mustache.Template {
|
||||
}
|
||||
|
||||
generate_data :: proc() -> Page_Data {
|
||||
years := []string{
|
||||
"2025", "2024", "2023", "2022", "2021",
|
||||
"2020", "2019", "2018", "2017", "2016",
|
||||
years := []string {
|
||||
"2025",
|
||||
"2024",
|
||||
"2023",
|
||||
"2022",
|
||||
"2021",
|
||||
"2020",
|
||||
"2019",
|
||||
"2018",
|
||||
"2017",
|
||||
"2016",
|
||||
}
|
||||
|
||||
posts := make([dynamic]Post, 0, 500)
|
||||
for year in years {
|
||||
for i in 0..<50 {
|
||||
for i in 0 ..< 50 {
|
||||
tags := make([dynamic]Tag, 0, 3)
|
||||
append(&tags, Tag{name = fmt.aprintf("%s-notes", year), slug = fmt.aprintf("%s-notes", year)})
|
||||
append(
|
||||
&tags,
|
||||
Tag{name = fmt.aprintf("%s-notes", year), slug = fmt.aprintf("%s-notes", year)},
|
||||
)
|
||||
append(&tags, Tag{name = "writing", slug = "writing"})
|
||||
append(&tags, Tag{name = "archive", slug = "archive"})
|
||||
|
||||
@@ -159,28 +174,34 @@ generate_data :: proc() -> Page_Data {
|
||||
author = fmt.aprintf("Author %d", i % 5)
|
||||
}
|
||||
|
||||
append(&posts, Post{
|
||||
title = fmt.aprintf("Post %d from %s", i, year),
|
||||
url = fmt.aprintf("/%s/post-%d", year, i),
|
||||
date = fmt.aprintf("%s-%02d-%02dT10:00:00Z", year, month, day),
|
||||
year = year,
|
||||
excerpt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
|
||||
author = author,
|
||||
tags = tags,
|
||||
})
|
||||
append(
|
||||
&posts,
|
||||
Post {
|
||||
title = fmt.aprintf("Post %d from %s", i, year),
|
||||
url = fmt.aprintf("/%s/post-%d", year, i),
|
||||
date = fmt.aprintf("%s-%02d-%02dT10:00:00Z", year, month, day),
|
||||
year = year,
|
||||
excerpt = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
|
||||
author = author,
|
||||
tags = tags,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
comments := make([dynamic]Comment, 0, 100)
|
||||
for i in 0..<100 {
|
||||
for i in 0 ..< 100 {
|
||||
year := years[i % len(years)]
|
||||
month := (i % 12) + 1
|
||||
day := (i % 28) + 1
|
||||
append(&comments, Comment{
|
||||
author = fmt.aprintf("Commenter %d", i),
|
||||
date = fmt.aprintf("%s-%02d-%02dT12:00:00Z", year, month, day),
|
||||
body = fmt.aprintf("Great post! This is comment number %d.", i),
|
||||
})
|
||||
append(
|
||||
&comments,
|
||||
Comment {
|
||||
author = fmt.aprintf("Commenter %d", i),
|
||||
date = fmt.aprintf("%s-%02d-%02dT12:00:00Z", year, month, day),
|
||||
body = fmt.aprintf("Great post! This is comment number %d.", i),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
nav_items := make([dynamic]Nav_Item, 0, 8)
|
||||
@@ -193,7 +214,7 @@ generate_data :: proc() -> Page_Data {
|
||||
append(&nav_items, Nav_Item{url = "https://twitter.com/example", label = "Twitter"})
|
||||
append(&nav_items, Nav_Item{url = "mailto:nobody@example.com", label = "Email"})
|
||||
|
||||
return Page_Data{
|
||||
return Page_Data {
|
||||
title = "Post Archive",
|
||||
now = "2025-07-21T12:00:00Z",
|
||||
posts = posts,
|
||||
|
||||
@@ -127,4 +127,3 @@ to_benchmark :: proc($f: formatter) -> benchmark {
|
||||
} \
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -41,4 +41,3 @@ parse_2_digits :: proc(s: string, offset: int) -> int {
|
||||
}
|
||||
return (int(s[offset]) - 0x30) * 10 + (int(s[offset + 1]) - 0x30)
|
||||
}
|
||||
|
||||
|
||||
@@ -123,4 +123,3 @@ emit_hour_12 :: proc(b: ^strings.Builder, dt: common.Date_Components, pad: bool)
|
||||
|
||||
fmt.sbprintf(b, format, h12)
|
||||
}
|
||||
|
||||
|
||||
@@ -130,4 +130,3 @@ emit_hour_12 :: proc(b: ^strings.Builder, dt: common.Date_Components, pad: bool)
|
||||
|
||||
fmt.sbprintf(b, format, h12)
|
||||
}
|
||||
|
||||
|
||||
@@ -125,4 +125,3 @@ emit_am_pm :: proc(b: ^strings.Builder, dt: common.Date_Components) {
|
||||
emit_am_pm_lower :: proc(b: ^strings.Builder, dt: common.Date_Components) {
|
||||
strings.write_string(b, "pm" if dt.hour >= 12 else "am")
|
||||
}
|
||||
|
||||
|
||||
@@ -41,4 +41,3 @@ parse_2_digits :: proc(s: string, offset: int) -> int {
|
||||
}
|
||||
return (int(s[offset]) - 0x30) * 10 + (int(s[offset + 1]) - 0x30)
|
||||
}
|
||||
|
||||
|
||||
@@ -124,4 +124,3 @@ emit_hour_12 :: proc(b: ^strings.Builder, dt: common.Date_Components, pad: bool)
|
||||
|
||||
fmt.sbprintf(b, format, h12)
|
||||
}
|
||||
|
||||
|
||||
+43
-12
@@ -3,10 +3,12 @@ package main
|
||||
import md "markdown"
|
||||
import ts "treesitter"
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:fmt"
|
||||
import "core:log"
|
||||
import "core:os"
|
||||
import "core:strings"
|
||||
import "core:time"
|
||||
|
||||
// Fields with underscores should never be set by the user.
|
||||
Page :: struct {
|
||||
@@ -18,12 +20,15 @@ Page :: struct {
|
||||
title: string,
|
||||
description: string,
|
||||
date: string,
|
||||
year: string,
|
||||
weight: Maybe(int),
|
||||
lastmod: string,
|
||||
menu: string,
|
||||
menus: map[string]Menu_Entry,
|
||||
params: json.Value,
|
||||
content: string,
|
||||
og: Open_Graph,
|
||||
draft: bool,
|
||||
is_starred: bool,
|
||||
toc: string,
|
||||
_is_index: bool `private`,
|
||||
}
|
||||
|
||||
@@ -67,6 +72,8 @@ site_load_content :: proc(site: ^Site) {
|
||||
for &page in site.pages {
|
||||
page.url = fmt.tprintf("%s%s", site.base_url, page.permalink)
|
||||
}
|
||||
|
||||
build_menus(site)
|
||||
}
|
||||
|
||||
// scan_content_files walks the content directory and collects Pending_File
|
||||
@@ -231,18 +238,28 @@ load_page :: proc(
|
||||
page.title = fm.title
|
||||
page.description = fm.description
|
||||
page.date = fm.date
|
||||
if page.date == "" {
|
||||
info, stat_err := os.stat(file_path, context.allocator)
|
||||
if stat_err == nil {
|
||||
page.date, _ = time.time_to_rfc3339(
|
||||
info.modification_time,
|
||||
0,
|
||||
false,
|
||||
context.allocator,
|
||||
)
|
||||
os.file_info_delete(info, context.allocator)
|
||||
log.warnf(
|
||||
"no date in frontmatter for %s, using file modification time: %s",
|
||||
file_path,
|
||||
page.date,
|
||||
)
|
||||
}
|
||||
}
|
||||
page.year = get_year(page.date)
|
||||
page.weight = fm.weight
|
||||
page.lastmod = fm.lastmod
|
||||
page.draft = fm.draft
|
||||
page.is_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)
|
||||
}
|
||||
page.params = fm.params
|
||||
|
||||
if section == "" && is_index {
|
||||
page.permalink = "/"
|
||||
@@ -254,6 +271,20 @@ load_page :: proc(
|
||||
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, context.allocator)
|
||||
} else {
|
||||
page.content = md.process(body, ext, file_path, context.allocator)
|
||||
}
|
||||
|
||||
if fm.toc {
|
||||
page.toc = md.generate_toc(page.content, context.allocator)
|
||||
}
|
||||
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
|
||||
@@ -3,4 +3,3 @@ package main
|
||||
import "core:os"
|
||||
|
||||
DEFAULTS_PATH :: #directory + os.Path_Separator_String + "defaults"
|
||||
|
||||
|
||||
@@ -4,9 +4,10 @@
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<title>{{title}}</title>
|
||||
{{> head}}
|
||||
<link rel="stylesheet" href="/css/main.css">
|
||||
<title>{{> title}}</title>
|
||||
{{> opengraph}}
|
||||
{{> styles}}
|
||||
{{> scripts}}
|
||||
</head>
|
||||
|
||||
<body>
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
{{&content}}
|
||||
</header>
|
||||
<ul>
|
||||
{{#pages}} <li><a href="{{permalink}}">{{&title}}</a><span>{{#date_iso}}<time
|
||||
datetime="{{date_iso}}">{{date_display}}</time>{{/date_iso}}</span>
|
||||
{{#pages}} <li><a href="{{permalink}}">{{&title}}</a><span>{{#date}}<time
|
||||
datetime="{{date}}">{{date | format}}</time>{{/date}}</span>
|
||||
</li>
|
||||
{{/pages}}
|
||||
</ul>
|
||||
|
||||
@@ -2,9 +2,14 @@
|
||||
{{$main}}
|
||||
<main>
|
||||
<article>
|
||||
<h1>{{page_title}}</h1>
|
||||
{{#date_iso}} <time class="subtitle" datetime="{{date_iso}}">{{date_display}}</time>
|
||||
{{/date_iso}} {{&content}}
|
||||
<h1>{{page.title}}</h1>
|
||||
{{#date}}<time class="subtitle" datetime="{{date}}">{{ date | format}}</time>{{/date}}
|
||||
{{#page.toc}}
|
||||
<nav class="toc">
|
||||
{{&page.toc}}
|
||||
</nav>
|
||||
{{/page.toc}}
|
||||
{{&content}}
|
||||
</article>
|
||||
</main>
|
||||
{{/main}}
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
<footer>
|
||||
<p>© {{now.year}} {{params.author.name}}</p>
|
||||
</footer>
|
||||
<p>© {{now | format "2006"}} {{params.author.name}}</p>
|
||||
</footer>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{{site.title}}{{^site.title}}Home{{/site.title}}
|
||||
@@ -1,7 +1,10 @@
|
||||
<header>
|
||||
<nav>
|
||||
<ul>
|
||||
<li><a href="/">{{title}}</a></li>
|
||||
<li><a href="/">{{> home-link}}</a></li>
|
||||
{{#menus.main}}
|
||||
<li><a href="{{url}}">{{name}}</a></li>
|
||||
{{/menus.main}}
|
||||
</ul>
|
||||
</nav>
|
||||
</header>
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
{{params.scripts}}
|
||||
@@ -0,0 +1,223 @@
|
||||
<style>
|
||||
/* Base layout */
|
||||
body {
|
||||
margin-left: auto;
|
||||
margin-right: auto;
|
||||
}
|
||||
|
||||
main {
|
||||
max-width: 760px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
/* Sidenote layout — activates only when sidenote elements are present */
|
||||
body:has(.sidenote, .marginnote) {
|
||||
counter-reset: sidenote-counter;
|
||||
padding-left: 12.5%;
|
||||
width: 87.5%;
|
||||
}
|
||||
|
||||
body:has(.sidenote, .marginnote) main {
|
||||
max-width: none;
|
||||
width: 60%;
|
||||
margin: 0;
|
||||
}
|
||||
|
||||
/* Images */
|
||||
img {
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
/* Superscript (footnote refs) */
|
||||
sup {
|
||||
line-height: 0;
|
||||
}
|
||||
|
||||
/* Code blocks */
|
||||
code,
|
||||
pre>code {
|
||||
font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace;
|
||||
font-size: 1.0rem;
|
||||
line-height: 1.42;
|
||||
-webkit-text-size-adjust: 100%;
|
||||
}
|
||||
|
||||
pre>code {
|
||||
font-size: 0.9rem;
|
||||
overflow-x: auto;
|
||||
display: block;
|
||||
}
|
||||
|
||||
/* Alerts (GitHub-style) */
|
||||
.alert {
|
||||
border-left: 4px solid;
|
||||
border-radius: 0 4px 4px 0;
|
||||
padding: 0.5rem 1rem;
|
||||
margin-inline-start: 0;
|
||||
}
|
||||
|
||||
.alert-title {
|
||||
font-weight: bold;
|
||||
margin-bottom: 0.25rem;
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
.alert-note {
|
||||
border-left-color: #3b82f6;
|
||||
}
|
||||
|
||||
.alert-tip {
|
||||
border-left-color: #22c55e;
|
||||
}
|
||||
|
||||
.alert-important {
|
||||
border-left-color: #a855f7;
|
||||
}
|
||||
|
||||
.alert-warning {
|
||||
border-left-color: #eab308;
|
||||
}
|
||||
|
||||
.alert-caution {
|
||||
border-left-color: #ef4444;
|
||||
}
|
||||
|
||||
/* Definition lists */
|
||||
dl,
|
||||
ol,
|
||||
ul {
|
||||
font-size: 1.4rem;
|
||||
line-height: 2rem;
|
||||
}
|
||||
|
||||
dt:not(:first-child) {
|
||||
margin-top: 0.25rem;
|
||||
}
|
||||
|
||||
dd {
|
||||
margin-left: 0;
|
||||
}
|
||||
|
||||
/* Sidenotes, margin notes */
|
||||
.sidenote,
|
||||
.marginnote {
|
||||
float: right;
|
||||
clear: right;
|
||||
margin-right: -45%;
|
||||
width: 40%;
|
||||
margin-top: 0.3rem;
|
||||
margin-bottom: 0;
|
||||
font-size: 0.85em;
|
||||
line-height: 1.5;
|
||||
vertical-align: baseline;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
.sidenote-number {
|
||||
counter-increment: sidenote-counter;
|
||||
}
|
||||
|
||||
.sidenote-number:after,
|
||||
.sidenote:before {
|
||||
position: relative;
|
||||
vertical-align: baseline;
|
||||
color: var(--color-accent, currentColor);
|
||||
}
|
||||
|
||||
.sidenote-number:after {
|
||||
content: counter(sidenote-counter);
|
||||
font-size: 0.8rem;
|
||||
top: -0.5rem;
|
||||
left: 0.1rem;
|
||||
}
|
||||
|
||||
.sidenote:before {
|
||||
content: counter(sidenote-counter) " ";
|
||||
font-size: 0.8rem;
|
||||
top: -0.5rem;
|
||||
}
|
||||
|
||||
blockquote .sidenote,
|
||||
blockquote .marginnote {
|
||||
margin-right: -82%;
|
||||
min-width: 59%;
|
||||
text-align: left;
|
||||
}
|
||||
|
||||
.marginnote>code,
|
||||
.sidenote>code {
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
input.margin-toggle {
|
||||
display: none;
|
||||
}
|
||||
|
||||
label.sidenote-number {
|
||||
display: inline-block;
|
||||
max-height: 2rem;
|
||||
}
|
||||
|
||||
label.margin-toggle:not(.sidenote-number) {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Responsive */
|
||||
@media (max-width: 1200px) {
|
||||
body {
|
||||
padding-left: 1rem;
|
||||
padding-right: 1rem;
|
||||
}
|
||||
|
||||
body:has(.sidenote, .marginnote) {
|
||||
padding-left: 8%;
|
||||
padding-right: 8%;
|
||||
width: 84%;
|
||||
}
|
||||
|
||||
body:has(.sidenote, .marginnote) main {
|
||||
width: 100%;
|
||||
max-width: 760px;
|
||||
margin: 0 auto;
|
||||
}
|
||||
|
||||
pre>code {
|
||||
width: 97%;
|
||||
}
|
||||
|
||||
img {
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
label.margin-toggle:not(.sidenote-number) {
|
||||
display: inline;
|
||||
}
|
||||
|
||||
label.margin-toggle:not(.sidenote-number)::after {
|
||||
content: "\2295";
|
||||
}
|
||||
|
||||
.sidenote,
|
||||
.marginnote {
|
||||
display: none;
|
||||
}
|
||||
|
||||
.margin-toggle:checked+.sidenote,
|
||||
.margin-toggle:checked+.marginnote {
|
||||
display: block;
|
||||
float: left;
|
||||
left: 1rem;
|
||||
clear: both;
|
||||
width: 95%;
|
||||
margin: 1rem 2.5%;
|
||||
vertical-align: baseline;
|
||||
position: relative;
|
||||
}
|
||||
|
||||
label {
|
||||
cursor: pointer;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
{{#params.stylesheets}}<link rel="stylesheet" href="{{.}}">{{/params.stylesheets}}
|
||||
@@ -0,0 +1 @@
|
||||
{{#site.title}}{{.}}{{#page.title}} | {{/page.title}}{{/site.title}}{{page.title}}
|
||||
@@ -1,14 +1,14 @@
|
||||
{{<base}}
|
||||
{{$main}}
|
||||
<main>
|
||||
<h1>{{page_title}}</h1>
|
||||
<h1>{{page.title}}</h1>
|
||||
{{&content}}
|
||||
{{#posts | group_by year}}
|
||||
<section>
|
||||
<h2>{{key}}</h2>
|
||||
<ul>
|
||||
{{#items}} <li><a href="{{permalink}}">{{&title}}</a><span>{{#date_iso}}<time
|
||||
datetime="{{date_iso}}">{{date_display}}</time>{{/date_iso}}</span>
|
||||
{{#items}} <li><a href="{{permalink}}">{{&title}}</a><span>{{#date}}<time
|
||||
datetime="{{date}}">{{date | format}}</time>{{/date}}</span>
|
||||
</li>
|
||||
{{/items}}
|
||||
</ul>
|
||||
|
||||
@@ -7,10 +7,9 @@ import "core:time"
|
||||
generate_rss :: proc(site: ^Site) -> string {
|
||||
sb := strings.builder_make()
|
||||
|
||||
strings.write_string(
|
||||
fmt.sbprintf(
|
||||
&sb,
|
||||
fmt.aprintf(
|
||||
`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
||||
`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||
<channel>
|
||||
<title>%s</title>
|
||||
@@ -18,11 +17,10 @@ generate_rss :: proc(site: ^Site) -> string {
|
||||
<description>%s</description>
|
||||
<language>en-us</language>
|
||||
<atom:link href="%s/index.xml" rel="self" type="application/rss+xml"/>`,
|
||||
xml_escape(site.title),
|
||||
site.base_url,
|
||||
xml_escape(site.description),
|
||||
site.base_url,
|
||||
),
|
||||
xml_escape(site.title),
|
||||
site.base_url,
|
||||
xml_escape(site.description),
|
||||
site.base_url,
|
||||
)
|
||||
|
||||
for page in site.pages {
|
||||
@@ -35,10 +33,9 @@ generate_rss :: proc(site: ^Site) -> string {
|
||||
pub_date = format_rfc822(page.date)
|
||||
}
|
||||
|
||||
strings.write_string(
|
||||
fmt.sbprintf(
|
||||
&sb,
|
||||
fmt.aprintf(
|
||||
`<item>
|
||||
`<item>
|
||||
<title>%s</title>
|
||||
<link>%s</link>
|
||||
<pubDate>%s</pubDate>
|
||||
@@ -46,12 +43,11 @@ generate_rss :: proc(site: ^Site) -> string {
|
||||
<description>%s</description>
|
||||
</item>
|
||||
`,
|
||||
xml_escape(page.title),
|
||||
page.url,
|
||||
pub_date,
|
||||
page.url,
|
||||
xml_escape(page.content),
|
||||
),
|
||||
xml_escape(page.title),
|
||||
page.url,
|
||||
pub_date,
|
||||
page.url,
|
||||
xml_escape(page.content),
|
||||
)
|
||||
}
|
||||
|
||||
@@ -70,11 +66,11 @@ generate_sitemap :: proc(site: ^Site) -> string {
|
||||
)
|
||||
|
||||
for page in site.pages {
|
||||
lastmod := ""
|
||||
fmt.sbprintf(&sb, "<url><loc>%s</loc>", page.url)
|
||||
if page.date != "" {
|
||||
lastmod = fmt.aprintf("<lastmod>%s</lastmod>", page.date)
|
||||
fmt.sbprintf(&sb, "<lastmod>%s</lastmod>", page.date)
|
||||
}
|
||||
strings.write_string(&sb, fmt.aprintf("<url><loc>%s</loc>%s</url>\n", page.url, lastmod))
|
||||
fmt.sbprintf(&sb, "</url>\n")
|
||||
}
|
||||
|
||||
// Section index pages (for sections without an index in content)
|
||||
@@ -103,14 +99,11 @@ generate_sitemap :: proc(site: ^Site) -> string {
|
||||
section_lastmod = page.date
|
||||
}
|
||||
}
|
||||
lm := ""
|
||||
fmt.sbprintf(&sb, "<url><loc>%s/%s/</loc>", site.base_url, section)
|
||||
if section_lastmod != "" {
|
||||
lm = fmt.aprintf("<lastmod>%s</lastmod>", section_lastmod)
|
||||
fmt.sbprintf(&sb, "<lastmod>%s</lastmod>", section_lastmod)
|
||||
}
|
||||
strings.write_string(
|
||||
&sb,
|
||||
fmt.aprintf("<url><loc>%s/%s/</loc>%s</url>\n", site.base_url, section, lm),
|
||||
)
|
||||
fmt.sbprintf(&sb, "</url>\n")
|
||||
}
|
||||
|
||||
strings.write_string(&sb, "</urlset>")
|
||||
@@ -149,4 +142,3 @@ xml_escape :: proc(s: string) -> string {
|
||||
r, _ = strings.replace_all(r, ">", ">")
|
||||
return r
|
||||
}
|
||||
|
||||
|
||||
@@ -12,13 +12,10 @@
|
||||
};
|
||||
|
||||
outputs =
|
||||
inputs@{
|
||||
self,
|
||||
flake-parts,
|
||||
nixpkgs,
|
||||
nixpkgs-unstable,
|
||||
inputs@{ flake-parts
|
||||
, nixpkgs
|
||||
, ...
|
||||
# , process-compose-flake
|
||||
treefmt-nix,
|
||||
}:
|
||||
flake-parts.lib.mkFlake { inherit inputs; } {
|
||||
imports = [
|
||||
@@ -28,11 +25,10 @@
|
||||
systems = [ "x86_64-linux" ];
|
||||
|
||||
perSystem =
|
||||
{
|
||||
pkgs,
|
||||
system,
|
||||
inputs',
|
||||
...
|
||||
{ pkgs
|
||||
, system
|
||||
, inputs'
|
||||
, ...
|
||||
}:
|
||||
let
|
||||
mkGrammarStaticLib = name: src: pkgs.stdenv.mkDerivation {
|
||||
@@ -71,7 +67,7 @@
|
||||
config.allowUnfree = true;
|
||||
|
||||
overlays = [
|
||||
(final: prev: { unstable = inputs'.nixpkgs-unstable.legacyPackages; })
|
||||
(_final: _prev: { unstable = inputs'.nixpkgs-unstable.legacyPackages; })
|
||||
];
|
||||
};
|
||||
|
||||
@@ -95,11 +91,22 @@
|
||||
settings.formatter.prettier = {
|
||||
excludes = [
|
||||
"public/**"
|
||||
"resources/js/modernizr.js"
|
||||
"storage/app/caniuse.json"
|
||||
"mustache/spec/specs/**"
|
||||
"*.html"
|
||||
"*.md"
|
||||
];
|
||||
};
|
||||
|
||||
settings.formatter.ols = {
|
||||
command = "${pkgs.bash}/bin/bash";
|
||||
options = [
|
||||
"-euc"
|
||||
''
|
||||
${pkgs.ols}/bin/odinfmt -w .
|
||||
''
|
||||
];
|
||||
includes = [ "*.odin" ];
|
||||
};
|
||||
};
|
||||
|
||||
#process-compose.default.settings.processes = { };
|
||||
@@ -118,6 +125,7 @@
|
||||
pkgs.git
|
||||
pkgs.cmark
|
||||
pkgs.tree-sitter
|
||||
pkgs.tzdata
|
||||
html-grammar
|
||||
css-grammar
|
||||
];
|
||||
@@ -145,11 +153,12 @@
|
||||
};
|
||||
|
||||
devShells.default = pkgs.mkShell {
|
||||
buildInputs = [odin ols ] ++ (with pkgs; [
|
||||
buildInputs = [ odin ols ] ++ (with pkgs; [
|
||||
cmark
|
||||
tree-sitter
|
||||
|
||||
gdb
|
||||
perf
|
||||
|
||||
# IDE
|
||||
unstable.helix
|
||||
|
||||
+37
-17
@@ -10,11 +10,13 @@ Frontmatter :: struct {
|
||||
date: string,
|
||||
lastmod: string,
|
||||
publishDate: string,
|
||||
menu: string,
|
||||
weight: Maybe(int),
|
||||
menus: json.Value,
|
||||
params: json.Value,
|
||||
layout: string,
|
||||
og: Open_Graph,
|
||||
draft: bool,
|
||||
isStarred: bool,
|
||||
toc: bool,
|
||||
}
|
||||
|
||||
// parse_frontmatter splits raw file content into a Frontmatter struct and the
|
||||
@@ -36,7 +38,7 @@ parse_frontmatter :: proc(content: string) -> (fm: Frontmatter, body: string, ok
|
||||
json_str := content[:end + 1]
|
||||
body = strings.trim_left(content[end + 1:], " \t\r\n")
|
||||
|
||||
value, err := json.parse_string(json_str, spec = .JSON)
|
||||
value, err := json.parse_string(json_str, spec = .JSON5)
|
||||
if err != nil {
|
||||
log.errorf("failed to parse frontmatter JSON: %v", err)
|
||||
return
|
||||
@@ -48,16 +50,20 @@ parse_frontmatter :: proc(content: string) -> (fm: Frontmatter, body: string, ok
|
||||
return
|
||||
}
|
||||
|
||||
fm.title = json_get_string(obj, "title")
|
||||
fm.title = json_get_string(obj, "title")
|
||||
fm.description = json_get_string(obj, "description")
|
||||
fm.date = json_get_string(obj, "date")
|
||||
fm.lastmod = json_get_string(obj, "lastmod")
|
||||
fm.date = json_get_string(obj, "date")
|
||||
fm.lastmod = json_get_string(obj, "lastmod")
|
||||
fm.publishDate = json_get_string(obj, "publishDate")
|
||||
fm.weight = json_get_int(obj, "weight")
|
||||
fm.draft = json_get_bool(obj, "draft")
|
||||
fm.isStarred = json_get_bool(obj, "isStarred")
|
||||
fm.menu = json_get_string(obj, "menu")
|
||||
fm.toc = json_get_bool(obj, "toc")
|
||||
if v, ok := obj["menus"]; ok {
|
||||
fm.menus = v
|
||||
}
|
||||
fm.layout = json_get_string(obj, "layout")
|
||||
fm.og = json_get_open_graph(obj, "og")
|
||||
if v, ok := obj["params"]; ok {fm.params = v}
|
||||
|
||||
ok = true
|
||||
return
|
||||
@@ -81,20 +87,34 @@ json_get_bool :: proc(obj: json.Object, key: string) -> bool {
|
||||
return false
|
||||
}
|
||||
|
||||
json_get_int :: proc(obj: json.Object, key: string) -> Maybe(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 nil
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
json_get_open_graph :: proc(obj: json.Object, key: string) -> Open_Graph {
|
||||
og: Open_Graph
|
||||
if v, ok := obj[key]; ok {
|
||||
if inner, ok2 := v.(json.Object); ok2 {
|
||||
og.title = json_get_string(inner, "title")
|
||||
og.type = json_get_string(inner, "type")
|
||||
og.image = json_get_string(inner, "image")
|
||||
og.url = json_get_string(inner, "url")
|
||||
og.description = json_get_string(inner, "description")
|
||||
og.locale = json_get_string(inner, "locale")
|
||||
og.site_name = json_get_string(inner, "site_name")
|
||||
og.title = json_get_string(inner, "title")
|
||||
og.type = json_get_string(inner, "type")
|
||||
og.image = json_get_string(inner, "image")
|
||||
og.url = json_get_string(inner, "url")
|
||||
og.description = json_get_string(inner, "description")
|
||||
og.locale = json_get_string(inner, "locale")
|
||||
og.site_name = json_get_string(inner, "site_name")
|
||||
og.published_time = json_get_string(inner, "published_time")
|
||||
og.modified_time = json_get_string(inner, "modified_time")
|
||||
og.section = json_get_string(inner, "section")
|
||||
og.modified_time = json_get_string(inner, "modified_time")
|
||||
og.section = json_get_string(inner, "section")
|
||||
}
|
||||
}
|
||||
return og
|
||||
|
||||
+1
-3
@@ -82,9 +82,7 @@ test_description_plain_text :: proc(t: ^testing.T) {
|
||||
|
||||
@(test)
|
||||
test_description_highlighted_code :: proc(t: ^testing.T) {
|
||||
result := generate_description(
|
||||
`<pre><code><span class="hl-keyword">if</span> x</code></pre>`,
|
||||
)
|
||||
result := generate_description(`<pre><code><span class="hl-keyword">if</span> x</code></pre>`)
|
||||
testing.expect_value(t, result, "if x")
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
package main
|
||||
|
||||
import "base:runtime"
|
||||
import "core:flags"
|
||||
import "core:log"
|
||||
import "core:mem"
|
||||
import "core:os"
|
||||
import "core:prof/spall"
|
||||
import "core:sync"
|
||||
@@ -43,9 +45,21 @@ main :: proc() {
|
||||
defer spall.buffer_destroy(&spall_ctx, &spall_buffer)
|
||||
}
|
||||
|
||||
cli_flags: Flags
|
||||
flags.parse_or_exit(&cli_flags, os.args, .Odin)
|
||||
|
||||
level: log.Level
|
||||
switch {
|
||||
case cli_flags.quiet:
|
||||
level = .Warning
|
||||
case cli_flags.verbose:
|
||||
level = .Debug
|
||||
case:
|
||||
level = .Info
|
||||
}
|
||||
logger_opts: log.Options =
|
||||
(log.Default_Console_Logger_Opts - log.Full_Timestamp_Opts - {.Short_File_Path})
|
||||
console_logger := log.create_console_logger(.Info, logger_opts)
|
||||
console_logger := log.create_console_logger(level, logger_opts)
|
||||
context.logger = console_logger
|
||||
defer log.destroy_console_logger(console_logger)
|
||||
|
||||
@@ -57,12 +71,21 @@ main :: proc() {
|
||||
|
||||
for {
|
||||
defer free_all(context.temp_allocator)
|
||||
defer log.debugf(
|
||||
"max_temp_allocator_size=%M",
|
||||
(cast(^runtime.Default_Temp_Allocator)context.temp_allocator.data)^.arena.total_used,
|
||||
)
|
||||
tick := time.tick_now()
|
||||
site: Site
|
||||
init_site(&site, os.args)
|
||||
init_site(&site, cli_flags)
|
||||
defer destroy_site(&site)
|
||||
// TODO: Make it so this isn't necessary
|
||||
context.allocator = site_allocator(&site)
|
||||
defer log.debugf(
|
||||
"current_site_allocator_size=%M",
|
||||
site.arena.block_size * (len(site.arena.used_blocks) + len(site.arena.unused_blocks)) -
|
||||
site.arena.bytes_left,
|
||||
)
|
||||
build_vfs(&site)
|
||||
|
||||
treesitter.grammar_dir = site.grammars
|
||||
@@ -70,7 +93,7 @@ main :: proc() {
|
||||
|
||||
site_load_content(&site)
|
||||
render_site(&site)
|
||||
log.infof("Built site in %s", time.tick_since(tick))
|
||||
log.infof("Built site in %M", time.tick_since(tick))
|
||||
|
||||
(.Watch in site.features) or_break
|
||||
time.sleep(5 * time.Second)
|
||||
|
||||
@@ -7,4 +7,3 @@ import "core:testing"
|
||||
test_true :: proc(t: ^testing.T) {
|
||||
testing.expect(t, true)
|
||||
}
|
||||
|
||||
|
||||
@@ -95,4 +95,3 @@ transform_alert :: proc(sb: ^strings.Builder, bq: string) {
|
||||
strings.write_string(sb, " ")
|
||||
strings.write_string(sb, rest)
|
||||
}
|
||||
|
||||
|
||||
@@ -101,4 +101,3 @@ test_multiple_alerts_render_together :: proc(t: ^testing.T) {
|
||||
</blockquote>`,
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,197 @@
|
||||
package markdown
|
||||
|
||||
import cm "vendor:commonmark"
|
||||
|
||||
import "core:strings"
|
||||
|
||||
// DefList_Entry represents a single term-definition pair in a definition list.
|
||||
DefList_Entry :: struct {
|
||||
term: string,
|
||||
definition: string,
|
||||
}
|
||||
|
||||
// convert_deflists scans markdown text for definition list patterns and
|
||||
// converts them to <dl><dt><dd> HTML blocks before cmark processing.
|
||||
//
|
||||
// A definition line starts with optional whitespace followed by a colon and
|
||||
// a space. The term is the nearest preceding non-blank line (immediately or
|
||||
// within one blank line). Consecutive term+definition pairs are grouped into
|
||||
// a single <dl> block.
|
||||
//
|
||||
// Terms and definitions are rendered through cmark individually so that
|
||||
// inline markdown (code, links, emphasis) is processed.
|
||||
convert_deflists :: proc(body: string, allocator := context.allocator) -> string {
|
||||
lines := strings.split(body, "\n", allocator = context.temp_allocator)
|
||||
|
||||
sb := strings.builder_make(context.temp_allocator)
|
||||
|
||||
first := true
|
||||
need_blank := false
|
||||
|
||||
i := 0
|
||||
for i < len(lines) {
|
||||
entries, matched, next := try_match_deflist(lines, i)
|
||||
if matched {
|
||||
html := render_deflist(entries)
|
||||
if !first {
|
||||
strings.write_string(&sb, "\n\n")
|
||||
}
|
||||
strings.write_string(&sb, html)
|
||||
first = false
|
||||
need_blank = true
|
||||
i = next
|
||||
continue
|
||||
}
|
||||
|
||||
if need_blank {
|
||||
strings.write_string(&sb, "\n\n")
|
||||
need_blank = false
|
||||
} else if !first {
|
||||
strings.write_string(&sb, "\n")
|
||||
}
|
||||
strings.write_string(&sb, lines[i])
|
||||
first = false
|
||||
i += 1
|
||||
}
|
||||
|
||||
return strings.clone(strings.to_string(sb), allocator)
|
||||
}
|
||||
|
||||
// try_match_deflist attempts to match a definition list group starting at
|
||||
// lines[start]. A group is one or more term+definition pairs. Returns the
|
||||
// matched entries, whether a match was found, and the index past the group.
|
||||
try_match_deflist :: proc(
|
||||
lines: []string,
|
||||
start: int,
|
||||
) -> (
|
||||
entries: [dynamic]DefList_Entry,
|
||||
ok: bool,
|
||||
end: int,
|
||||
) {
|
||||
entries = make([dynamic]DefList_Entry, 0, allocator = context.temp_allocator)
|
||||
end = start
|
||||
|
||||
i := start
|
||||
for i < len(lines) {
|
||||
// A term must be non-blank and not itself a def line
|
||||
if is_blank_line(lines[i]) || is_def_line(lines[i]) {
|
||||
break
|
||||
}
|
||||
|
||||
// Look for a def line: immediately after or with one blank line
|
||||
def_idx := i + 1
|
||||
if def_idx < len(lines) && is_blank_line(lines[def_idx]) {
|
||||
def_idx += 1
|
||||
}
|
||||
|
||||
if def_idx >= len(lines) || !is_def_line(lines[def_idx]) {
|
||||
break
|
||||
}
|
||||
|
||||
// Found a term + def pair
|
||||
append(
|
||||
&entries,
|
||||
DefList_Entry {
|
||||
term = strings.trim_space(lines[i]),
|
||||
definition = def_content(lines[def_idx]),
|
||||
},
|
||||
)
|
||||
i = def_idx + 1
|
||||
|
||||
// After a pair, check if another pair follows (optionally
|
||||
// separated by one blank line). If so, continue the group.
|
||||
// If not, break without consuming the blank line.
|
||||
if i < len(lines) && is_blank_line(lines[i]) {
|
||||
after_blank := i + 1
|
||||
if after_blank < len(lines) &&
|
||||
!is_blank_line(lines[after_blank]) &&
|
||||
!is_def_line(lines[after_blank]) {
|
||||
// Check whether a def follows this potential term
|
||||
check_def := after_blank + 1
|
||||
if check_def < len(lines) && is_blank_line(lines[check_def]) {
|
||||
check_def += 1
|
||||
}
|
||||
if check_def < len(lines) && is_def_line(lines[check_def]) {
|
||||
i = after_blank
|
||||
continue
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
if len(entries) > 0 {
|
||||
ok = true
|
||||
end = i
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// is_def_line returns true if the line is a definition line:
|
||||
// optional leading whitespace, a colon, then whitespace or end-of-line.
|
||||
is_def_line :: proc(line: string) -> bool {
|
||||
trimmed := strings.trim_left(line, " \t")
|
||||
if len(trimmed) < 1 || trimmed[0] != ':' {
|
||||
return false
|
||||
}
|
||||
if len(trimmed) == 1 {
|
||||
return true
|
||||
}
|
||||
return trimmed[1] == ' ' || trimmed[1] == '\t'
|
||||
}
|
||||
|
||||
// def_content extracts the definition text from a definition line,
|
||||
// stripping the leading colon and surrounding whitespace.
|
||||
def_content :: proc(line: string) -> string {
|
||||
trimmed := strings.trim_left(line, " \t")
|
||||
content := trimmed[1:]
|
||||
content = strings.trim_left(content, " \t")
|
||||
return content
|
||||
}
|
||||
|
||||
// is_blank_line returns true for empty or whitespace-only lines.
|
||||
is_blank_line :: proc(line: string) -> bool {
|
||||
return strings.trim_space(line) == ""
|
||||
}
|
||||
|
||||
// render_deflist builds the <dl> HTML block from a list of entries.
|
||||
// Each term and definition is rendered through cmark to process inline
|
||||
// markdown. Result lives in context.temp_allocator.
|
||||
render_deflist :: proc(entries: [dynamic]DefList_Entry) -> string {
|
||||
sb := strings.builder_make(context.temp_allocator)
|
||||
|
||||
strings.write_string(&sb, "<dl>")
|
||||
for entry in entries {
|
||||
term_html := render_inline_md(entry.term)
|
||||
def_html := render_inline_md(entry.definition)
|
||||
strings.write_string(&sb, "<dt>")
|
||||
strings.write_string(&sb, term_html)
|
||||
strings.write_string(&sb, "</dt><dd>")
|
||||
strings.write_string(&sb, def_html)
|
||||
strings.write_string(&sb, "</dd>")
|
||||
}
|
||||
strings.write_string(&sb, "</dl>")
|
||||
|
||||
return strings.to_string(sb)
|
||||
}
|
||||
|
||||
// render_inline_md renders a snippet of markdown through cmark and strips
|
||||
// the surrounding <p> tags. Result lives in context.temp_allocator.
|
||||
render_inline_md :: proc(text: string) -> string {
|
||||
raw := cm.markdown_to_html_from_string(text, {.Unsafe})
|
||||
defer cm.free_string(raw)
|
||||
return strings.clone(strip_p_tags(raw), context.temp_allocator)
|
||||
}
|
||||
|
||||
// strip_p_tags removes surrounding <p></p> if the HTML is a single paragraph.
|
||||
strip_p_tags :: proc(html: string) -> string {
|
||||
s := html
|
||||
if len(s) > 0 && s[len(s) - 1] == '\n' {
|
||||
s = s[:len(s) - 1]
|
||||
}
|
||||
if strings.has_prefix(s, "<p>") && strings.has_suffix(s, "</p>") {
|
||||
return s[3:len(s) - 4]
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
#+test
|
||||
package markdown
|
||||
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
|
||||
@(test)
|
||||
test_single_entry :: proc(t: ^testing.T) {
|
||||
input := "term\n\n: definition"
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
expected := "<dl><dt>term</dt><dd>definition</dd></dl>"
|
||||
testing.expect_value(t, result, expected)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_single_entry_no_blank :: proc(t: ^testing.T) {
|
||||
input := "term\n: definition"
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
expected := "<dl><dt>term</dt><dd>definition</dd></dl>"
|
||||
testing.expect_value(t, result, expected)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_indented_variant :: proc(t: ^testing.T) {
|
||||
input := " term\n : definition"
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
expected := "<dl><dt>term</dt><dd>definition</dd></dl>"
|
||||
testing.expect_value(t, result, expected)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_multiple_entries :: proc(t: ^testing.T) {
|
||||
input := "t1\n\n: d1\n\nt2\n\n: d2"
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
expected := "<dl><dt>t1</dt><dd>d1</dd><dt>t2</dt><dd>d2</dd></dl>"
|
||||
testing.expect_value(t, result, expected)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_mixed_indented_and_non_indented :: proc(t: ^testing.T) {
|
||||
input := "t1\n\n: d1\n\n t2\n : d2\n\nt3\n\n: d3"
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
expected := "<dl><dt>t1</dt><dd>d1</dd><dt>t2</dt><dd>d2</dd><dt>t3</dt><dd>d3</dd></dl>"
|
||||
testing.expect_value(t, result, expected)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_inline_markdown_in_term :: proc(t: ^testing.T) {
|
||||
input := "`code`\n\n: def"
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
expected := "<dl><dt><code>code</code></dt><dd>def</dd></dl>"
|
||||
testing.expect_value(t, result, expected)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_inline_markdown_in_definition :: proc(t: ^testing.T) {
|
||||
input := "term\n\n: see [Content](#content) here"
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
testing.expect(t, strings.contains(result, `<a href="#content">Content</a>`))
|
||||
testing.expect(t, strings.contains(result, "<dd>see "))
|
||||
testing.expect(t, strings.contains(result, "</dd>"))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_regular_text_passes_through :: proc(t: ^testing.T) {
|
||||
input := "This is: not a deflist"
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
testing.expect_value(t, result, input)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_colon_inside_paragraph_no_false_positive :: proc(t: ^testing.T) {
|
||||
input := "First paragraph.\n\nSecond paragraph."
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
testing.expect_value(t, result, input)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_deflist_between_paragraphs :: proc(t: ^testing.T) {
|
||||
input := "Before.\n\nterm\n\n: def\n\nAfter."
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
testing.expect(t, strings.has_prefix(result, "Before."))
|
||||
testing.expect(t, strings.contains(result, "<dl><dt>term</dt><dd>def</dd></dl>"))
|
||||
testing.expect(t, strings.has_suffix(result, "After."))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_empty_body :: proc(t: ^testing.T) {
|
||||
result := convert_deflists("", context.temp_allocator)
|
||||
testing.expect_value(t, result, "")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_docs_md_pattern :: proc(t: ^testing.T) {
|
||||
input := "content\n\n: `content` holds your pages.\n\n assets\n : `assets` contains files."
|
||||
result := convert_deflists(input, context.temp_allocator)
|
||||
testing.expect(t, strings.contains(result, "<dl>"))
|
||||
testing.expect(t, strings.contains(result, "<dt>content</dt>"))
|
||||
testing.expect(t, strings.contains(result, "<dt>assets</dt>"))
|
||||
testing.expect(t, strings.contains(result, "<code>content</code>"))
|
||||
testing.expect(t, strings.contains(result, "<code>assets</code>"))
|
||||
testing.expect(t, strings.contains(result, "</dl>"))
|
||||
}
|
||||
|
||||
@@ -434,4 +434,3 @@ expand_emoji :: proc(text: string) -> string {
|
||||
|
||||
return strings.to_string(sb)
|
||||
}
|
||||
|
||||
|
||||
+96
-15
@@ -1,7 +1,5 @@
|
||||
package markdown
|
||||
|
||||
import cm "vendor:commonmark"
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strings"
|
||||
|
||||
@@ -171,27 +169,27 @@ inject_notes :: proc(html: string, sn_defs, mn_defs: map[string]string) -> strin
|
||||
}
|
||||
|
||||
// Render definition through cmark for markdown support
|
||||
def_html := cm.markdown_to_html_from_string(def_text, {.Unsafe})
|
||||
def_html = strip_p_tags(def_html)
|
||||
def_html := render_inline_md(def_text)
|
||||
|
||||
note: string
|
||||
defer delete(note)
|
||||
if is_margin {
|
||||
note = fmt.aprintf(
|
||||
fmt.sbprintf(
|
||||
&parts,
|
||||
`<label for="mn-%s" class="margin-toggle"></label><input type="checkbox" id="mn-%s" class="margin-toggle"><span class="marginnote">%s</span>`,
|
||||
id,
|
||||
id,
|
||||
def_html,
|
||||
)
|
||||
} else {
|
||||
note = fmt.aprintf(
|
||||
fmt.sbprintf(
|
||||
&parts,
|
||||
`<label for="fn-%s" class="margin-toggle sidenote-number"></label><input type="checkbox" id="fn-%s" class="margin-toggle"><span class="sidenote">%s</span>`,
|
||||
id,
|
||||
id,
|
||||
def_html,
|
||||
)
|
||||
}
|
||||
strings.write_string(&parts, note)
|
||||
|
||||
remaining = remaining[ref_end:]
|
||||
}
|
||||
@@ -199,15 +197,98 @@ inject_notes :: proc(html: string, sn_defs, mn_defs: map[string]string) -> strin
|
||||
return strings.to_string(parts)
|
||||
}
|
||||
|
||||
// strip_p_tags removes surrounding <p></p> if the HTML is a single paragraph.
|
||||
strip_p_tags :: proc(html: string) -> string {
|
||||
s := html
|
||||
if len(s) > 0 && s[len(s) - 1] == '\n' {
|
||||
s = s[:len(s) - 1]
|
||||
// inject_footnotes finds [^id] and [*id] references in rendered HTML, numbers
|
||||
// them sequentially by order of appearance, and replaces them with <sup> links.
|
||||
// Appends a <section class="footnotes"><ol> at the end with definitions.
|
||||
// Both sidenote ([^id]) and marginnote ([*id]) references are treated equally.
|
||||
inject_footnotes :: proc(html: string, sn_defs, mn_defs: map[string]string) -> string {
|
||||
if len(sn_defs) == 0 && len(mn_defs) == 0 {
|
||||
return html
|
||||
}
|
||||
if strings.has_prefix(s, "<p>") && strings.has_suffix(s, "</p>") {
|
||||
return s[3:len(s) - 4]
|
||||
|
||||
parts: strings.Builder
|
||||
strings.builder_init_len(&parts, 0)
|
||||
defer strings.builder_destroy(&parts)
|
||||
|
||||
number_of: map[string]int = make(map[string]int, allocator = context.temp_allocator)
|
||||
ordered_ids: [dynamic]string = make([dynamic]string, 0, allocator = context.temp_allocator)
|
||||
next_num := 1
|
||||
|
||||
remaining := html
|
||||
|
||||
for {
|
||||
sn_pos := strings.index(remaining, "[^")
|
||||
mn_pos := strings.index(remaining, "[*")
|
||||
|
||||
is_margin := mn_pos >= 0 && (sn_pos < 0 || mn_pos < sn_pos)
|
||||
pos := sn_pos
|
||||
if is_margin {
|
||||
pos = mn_pos
|
||||
}
|
||||
if pos < 0 {
|
||||
strings.write_string(&parts, remaining)
|
||||
break
|
||||
}
|
||||
|
||||
strings.write_string(&parts, remaining[:pos])
|
||||
|
||||
close := strings.index(remaining[pos + 2:], "]")
|
||||
if close < 0 {
|
||||
strings.write_string(&parts, remaining[pos:])
|
||||
break
|
||||
}
|
||||
|
||||
id := remaining[pos + 2:pos + 2 + close]
|
||||
ref_end := pos + 2 + close + 1
|
||||
|
||||
defs := sn_defs
|
||||
if is_margin {
|
||||
defs = mn_defs
|
||||
}
|
||||
def_text, found := defs[id]
|
||||
if !found {
|
||||
strings.write_string(&parts, remaining[pos:ref_end])
|
||||
remaining = remaining[ref_end:]
|
||||
continue
|
||||
}
|
||||
|
||||
num, seen := number_of[id]
|
||||
if !seen {
|
||||
num = next_num
|
||||
number_of[id] = num
|
||||
next_num += 1
|
||||
append(&ordered_ids, id)
|
||||
}
|
||||
|
||||
fmt.sbprintf(&parts, `<sup><a href="#fn-%d" id="fnref-%d">%d</a></sup>`, num, num, num)
|
||||
|
||||
remaining = remaining[ref_end:]
|
||||
}
|
||||
return s
|
||||
|
||||
if len(ordered_ids) > 0 {
|
||||
strings.write_string(&parts, "\n<section class=\"footnotes\">\n<hr>\n<ol>\n")
|
||||
for id in ordered_ids {
|
||||
def_text, ok := sn_defs[id]
|
||||
if !ok {
|
||||
def_text, ok = mn_defs[id]
|
||||
}
|
||||
if !ok do continue
|
||||
|
||||
def_html := render_inline_md(def_text)
|
||||
num := number_of[id]
|
||||
|
||||
fmt.sbprintf(
|
||||
&parts,
|
||||
`<li id="fn-%d">%s <a href="#fnref-%d" class="footnote-backref">↩︎</a></li>` +
|
||||
"\n",
|
||||
num,
|
||||
def_html,
|
||||
num,
|
||||
)
|
||||
}
|
||||
strings.write_string(&parts, "</ol>\n</section>")
|
||||
}
|
||||
|
||||
return strings.to_string(parts)
|
||||
}
|
||||
|
||||
|
||||
@@ -120,3 +120,101 @@ test_inject_notes_missing_ref :: proc(t: ^testing.T) {
|
||||
testing.expect(t, strings.contains(out, "[*missing]"))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_inject_footnotes_basic :: proc(t: ^testing.T) {
|
||||
html := "Text[^a] end."
|
||||
sn := map[string]string {
|
||||
"a" = "a footnote",
|
||||
}
|
||||
defer delete_map(sn)
|
||||
mn := make(map[string]string)
|
||||
defer delete_map(mn)
|
||||
|
||||
out := inject_footnotes(html, sn, mn)
|
||||
|
||||
testing.expect(t, strings.contains(out, `<sup><a href="#fn-1" id="fnref-1">1</a></sup>`))
|
||||
testing.expect(t, strings.contains(out, `<li id="fn-1">a footnote`))
|
||||
testing.expect(t, strings.contains(out, `class="footnote-backref"`))
|
||||
testing.expect(t, strings.contains(out, `<section class="footnotes">`))
|
||||
testing.expect(t, strings.contains(out, "</section>"))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_inject_footnotes_numbered_by_appearance :: proc(t: ^testing.T) {
|
||||
html := "Second[^b] then first[^a]."
|
||||
sn := map[string]string {
|
||||
"a" = "def a",
|
||||
"b" = "def b",
|
||||
}
|
||||
defer delete_map(sn)
|
||||
mn := make(map[string]string)
|
||||
defer delete_map(mn)
|
||||
|
||||
out := inject_footnotes(html, sn, mn)
|
||||
|
||||
// b appears first in the text → 1, a → 2
|
||||
testing.expect(t, strings.contains(out, `id="fnref-1">1</a></sup>`))
|
||||
testing.expect(t, strings.contains(out, `id="fnref-2">2</a></sup>`))
|
||||
testing.expect(t, strings.contains(out, `<li id="fn-1">def b`))
|
||||
testing.expect(t, strings.contains(out, `<li id="fn-2">def a`))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_inject_footnotes_marginnote_treated_same :: proc(t: ^testing.T) {
|
||||
html := "Sidenote[^a] and marginnote[*b]."
|
||||
sn := map[string]string {
|
||||
"a" = "sn def",
|
||||
}
|
||||
defer delete_map(sn)
|
||||
mn := map[string]string {
|
||||
"b" = "mn def",
|
||||
}
|
||||
defer delete_map(mn)
|
||||
|
||||
out := inject_footnotes(html, sn, mn)
|
||||
|
||||
// Both get numbered as regular footnotes
|
||||
testing.expect(t, strings.contains(out, `id="fnref-1">1</a></sup>`))
|
||||
testing.expect(t, strings.contains(out, `id="fnref-2">2</a></sup>`))
|
||||
testing.expect(t, strings.contains(out, `<li id="fn-1">sn def`))
|
||||
testing.expect(t, strings.contains(out, `<li id="fn-2">mn def`))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_inject_footnotes_no_defs :: proc(t: ^testing.T) {
|
||||
html := "No notes here."
|
||||
sn := make(map[string]string)
|
||||
mn := make(map[string]string)
|
||||
testing.expect(t, inject_footnotes(html, sn, mn) == html)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_inject_footnotes_missing_def :: proc(t: ^testing.T) {
|
||||
html := "Ref[^missing] end."
|
||||
sn := map[string]string {
|
||||
"other" = "x",
|
||||
}
|
||||
defer delete_map(sn)
|
||||
mn := make(map[string]string)
|
||||
defer delete_map(mn)
|
||||
|
||||
out := inject_footnotes(html, sn, mn)
|
||||
|
||||
testing.expect(t, strings.contains(out, "[^missing]"))
|
||||
testing.expect(t, !strings.contains(out, "<section"))
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_inject_footnotes_inline_markdown :: proc(t: ^testing.T) {
|
||||
html := "Text[^a] end."
|
||||
sn := map[string]string {
|
||||
"a" = "see [link](http://example.com) here",
|
||||
}
|
||||
defer delete_map(sn)
|
||||
mn := make(map[string]string)
|
||||
defer delete_map(mn)
|
||||
|
||||
out := inject_footnotes(html, sn, mn)
|
||||
|
||||
testing.expect(t, strings.contains(out, `<a href="http://example.com">link</a>`))
|
||||
}
|
||||
|
||||
@@ -193,4 +193,3 @@ make_unique :: proc(slug: string, seen: ^map[string]bool) -> string {
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
|
||||
@@ -12,7 +12,11 @@ test_heading_simple :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_heading_dedup :: proc(t: ^testing.T) {
|
||||
result := inject_heading_ids("<h2>Intro</h2><p>text</p><h2>Intro</h2>")
|
||||
testing.expect_value(t, result, `<h2 id="intro">Intro</h2><p>text</p><h2 id="intro-1">Intro</h2>`)
|
||||
testing.expect_value(
|
||||
t,
|
||||
result,
|
||||
`<h2 id="intro">Intro</h2><p>text</p><h2 id="intro-1">Intro</h2>`,
|
||||
)
|
||||
}
|
||||
|
||||
@(test)
|
||||
@@ -36,7 +40,9 @@ test_heading_punctuation :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_heading_all_levels :: proc(t: ^testing.T) {
|
||||
result := inject_heading_ids("<h1>A</h1><h2>B</h2><h3>C</h3><h4>D</h4><h5>E</h5><h6>F</h6>")
|
||||
testing.expect_value(t, result,
|
||||
testing.expect_value(
|
||||
t,
|
||||
result,
|
||||
`<h1 id="a">A</h1>` +
|
||||
`<h2 id="b">B</h2>` +
|
||||
`<h3 id="c">C</h3>` +
|
||||
@@ -88,9 +94,9 @@ test_heading_numbers :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_heading_triple_dedup :: proc(t: ^testing.T) {
|
||||
result := inject_heading_ids("<h2>Foo</h2><h2>Foo</h2><h2>Foo</h2>")
|
||||
testing.expect_value(t, result,
|
||||
`<h2 id="foo">Foo</h2>` +
|
||||
`<h2 id="foo-1">Foo</h2>` +
|
||||
`<h2 id="foo-2">Foo</h2>`,
|
||||
testing.expect_value(
|
||||
t,
|
||||
result,
|
||||
`<h2 id="foo">Foo</h2>` + `<h2 id="foo-1">Foo</h2>` + `<h2 id="foo-2">Foo</h2>`,
|
||||
)
|
||||
}
|
||||
|
||||
@@ -305,4 +305,3 @@ highlight_code :: proc(html: string, file_path: string) -> string {
|
||||
}
|
||||
return strings.to_string(sb)
|
||||
}
|
||||
|
||||
|
||||
+70
-31
@@ -3,6 +3,7 @@ package markdown
|
||||
import cm "vendor:commonmark"
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:log"
|
||||
import "core:strings"
|
||||
|
||||
Extension :: enum {
|
||||
@@ -12,22 +13,38 @@ Extension :: enum {
|
||||
Highlight,
|
||||
Sections,
|
||||
HeadingIDs,
|
||||
DefLists,
|
||||
Footnotes,
|
||||
}
|
||||
|
||||
DEFAULT_EXTENSIONS :: bit_set[Extension]{.Emoji, .Sidenotes, .Alerts, .HeadingIDs}
|
||||
DEFAULT_EXTENSIONS :: bit_set[Extension]{.Emoji, .Sidenotes, .Alerts, .HeadingIDs, .DefLists}
|
||||
|
||||
process :: proc(body: string, ext: bit_set[Extension], file_path: string) -> string {
|
||||
// Caller is responsible for freeing string
|
||||
process :: proc(
|
||||
body: string,
|
||||
ext: bit_set[Extension],
|
||||
file_path: string,
|
||||
allocator := context.allocator,
|
||||
) -> string {
|
||||
side_notes := make(map[string]string)
|
||||
margin_notes := make(map[string]string)
|
||||
clean_body := body
|
||||
if .Sidenotes in ext {
|
||||
if .Sidenotes in ext || .Footnotes in ext {
|
||||
clean_body, side_notes, margin_notes = strip_definitions(body)
|
||||
}
|
||||
html := cm.markdown_to_html_from_string(clean_body, {.Unsafe})
|
||||
if .DefLists in ext {
|
||||
clean_body = convert_deflists(clean_body, allocator)
|
||||
}
|
||||
original_html := cm.markdown_to_html_from_string(clean_body, {.Unsafe})
|
||||
html := strings.clone(original_html, allocator)
|
||||
cm.free_string(original_html)
|
||||
|
||||
if .Emoji in ext {
|
||||
html = expand_emoji(html)
|
||||
}
|
||||
if .Sidenotes in ext {
|
||||
if .Footnotes in ext {
|
||||
html = inject_footnotes(html, side_notes, margin_notes)
|
||||
} else if .Sidenotes in ext {
|
||||
html = inject_notes(html, side_notes, margin_notes)
|
||||
}
|
||||
if .Alerts in ext {
|
||||
@@ -49,20 +66,14 @@ process :: proc(body: string, ext: bit_set[Extension], file_path: string) -> str
|
||||
parse_extension_list :: proc(s: string) -> (result: bit_set[Extension]) {
|
||||
for part in strings.split(s, ",", allocator = context.temp_allocator) {
|
||||
name := strings.to_lower(strings.trim_space(part), allocator = context.temp_allocator)
|
||||
switch name {
|
||||
case "emoji":
|
||||
result += {.Emoji}
|
||||
case "sidenotes":
|
||||
result += {.Sidenotes}
|
||||
case "alerts":
|
||||
result += {.Alerts}
|
||||
case "highlight":
|
||||
result += {.Highlight}
|
||||
case "sections":
|
||||
result += {.Sections}
|
||||
case "heading_ids":
|
||||
result += {.HeadingIDs}
|
||||
e, ok := extension_from_name(name)
|
||||
if !ok {
|
||||
if name != "" {
|
||||
log.warnf("unknown extension '%s'", name)
|
||||
}
|
||||
continue
|
||||
}
|
||||
result += {e}
|
||||
}
|
||||
return result
|
||||
}
|
||||
@@ -72,20 +83,48 @@ apply_extension_config :: proc(ext: ^bit_set[Extension], config: json.Object) {
|
||||
for name, val in config {
|
||||
// TODO: Silently discards invalid values.
|
||||
enabled := val.(json.Boolean) or_continue
|
||||
switch name {
|
||||
case "emoji":
|
||||
if enabled {ext^ += {.Emoji}} else {ext^ -= {.Emoji}}
|
||||
case "sidenotes":
|
||||
if enabled {ext^ += {.Sidenotes}} else {ext^ -= {.Sidenotes}}
|
||||
case "alerts":
|
||||
if enabled {ext^ += {.Alerts}} else {ext^ -= {.Alerts}}
|
||||
case "highlight":
|
||||
if enabled {ext^ += {.Highlight}} else {ext^ -= {.Highlight}}
|
||||
case "sections":
|
||||
if enabled {ext^ += {.Sections}} else {ext^ -= {.Sections}}
|
||||
case "heading_ids":
|
||||
if enabled {ext^ += {.HeadingIDs}} else {ext^ -= {.HeadingIDs}}
|
||||
e := extension_from_name(name) or_continue
|
||||
|
||||
if enabled {
|
||||
ext^ += {e}
|
||||
} else {
|
||||
ext^ -= {e}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
extension_from_name :: proc(name: string) -> (e: Extension, ok: bool) {
|
||||
switch name {
|
||||
case "emoji":
|
||||
e = .Emoji
|
||||
ok = true
|
||||
case "sidenotes":
|
||||
e = .Sidenotes
|
||||
case "alerts":
|
||||
e = .Alerts
|
||||
case "highlight":
|
||||
e = .Highlight
|
||||
case "sections":
|
||||
e = .Sections
|
||||
case "heading_ids":
|
||||
e = .HeadingIDs
|
||||
case "deflists":
|
||||
e = .DefLists
|
||||
case "footnotes":
|
||||
e = .Footnotes
|
||||
case:
|
||||
// Do nothing
|
||||
}
|
||||
|
||||
return e, ok || e != .Emoji
|
||||
}
|
||||
|
||||
// resolve_extension_conflicts resolves mutually exclusive extensions.
|
||||
// Footnotes and Sidenotes share the same [^id] syntax but render differently;
|
||||
// if both are enabled (e.g. from defaults + CLI), Footnotes wins.
|
||||
resolve_extension_conflicts :: proc(ext: ^bit_set[Extension]) {
|
||||
if .Footnotes in ext^ && .Sidenotes in ext^ {
|
||||
ext^ -= {.Sidenotes}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -43,4 +43,3 @@ wrap_sections :: proc(html: string) -> string {
|
||||
return html
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -46,4 +46,3 @@ test_wrap_sections_doesnt_split_content :: proc(t: ^testing.T) {
|
||||
"<section><h1>Big</h1><h3>Small</h3></section>",
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,163 @@
|
||||
package markdown
|
||||
|
||||
import "core:strings"
|
||||
|
||||
// generate_toc scans rendered HTML for <h1>-<h6> tags with id attributes and
|
||||
// builds a nested <ul> table of contents. Returns "" if no headings with IDs
|
||||
// are found. Must be called after inject_heading_ids.
|
||||
generate_toc :: proc(html: string, allocator := context.allocator) -> string {
|
||||
b: strings.Builder
|
||||
strings.builder_init(&b, allocator)
|
||||
|
||||
current_level := 0
|
||||
min_level := 7
|
||||
pos := 0
|
||||
|
||||
for {
|
||||
idx, level, id, text, next_pos := next_heading(html, pos)
|
||||
if level == 0 {
|
||||
break
|
||||
}
|
||||
pos = next_pos
|
||||
|
||||
if level < min_level {
|
||||
min_level = level
|
||||
}
|
||||
|
||||
if current_level == 0 {
|
||||
current_level = level
|
||||
strings.write_string(&b, "<ul>\n")
|
||||
} else if level > current_level {
|
||||
for current_level < level {
|
||||
strings.write_string(&b, "<ul>\n")
|
||||
current_level += 1
|
||||
}
|
||||
} else if level < current_level {
|
||||
strings.write_string(&b, "</li>\n")
|
||||
for current_level > level {
|
||||
strings.write_string(&b, "</ul>\n</li>\n")
|
||||
current_level -= 1
|
||||
}
|
||||
} else {
|
||||
strings.write_string(&b, "</li>\n")
|
||||
}
|
||||
|
||||
strings.write_string(&b, `<li><a href="#`)
|
||||
strings.write_string(&b, id)
|
||||
strings.write_string(&b, `">`)
|
||||
strings.write_string(&b, text)
|
||||
strings.write_string(&b, `</a>`)
|
||||
}
|
||||
|
||||
if current_level == 0 {
|
||||
return ""
|
||||
}
|
||||
|
||||
strings.write_string(&b, "</li>\n")
|
||||
for current_level > min_level {
|
||||
strings.write_string(&b, "</ul>\n</li>\n")
|
||||
current_level -= 1
|
||||
}
|
||||
strings.write_string(&b, "</ul>\n")
|
||||
|
||||
return strings.to_string(b)
|
||||
}
|
||||
|
||||
// next_heading finds the next <hN> tag with an id attribute starting from pos.
|
||||
// Returns level=0 if none found.
|
||||
next_heading :: proc(
|
||||
html: string,
|
||||
start: int,
|
||||
) -> (
|
||||
idx: int,
|
||||
level: int,
|
||||
id: string,
|
||||
text: string,
|
||||
next_pos: int,
|
||||
) {
|
||||
i := start
|
||||
for i + 3 < len(html) {
|
||||
if html[i] == '<' && html[i + 1] == 'h' {
|
||||
d := html[i + 2]
|
||||
if d >= '1' && d <= '6' {
|
||||
level = int(d - '0')
|
||||
idx = i
|
||||
break
|
||||
}
|
||||
}
|
||||
i += 1
|
||||
}
|
||||
|
||||
if level == 0 {
|
||||
return 0, 0, "", "", len(html)
|
||||
}
|
||||
|
||||
// Find end of opening tag
|
||||
tag_end := strings.index_byte(html[idx:], '>')
|
||||
if tag_end < 0 {
|
||||
return 0, 0, "", "", len(html)
|
||||
}
|
||||
tag_end += idx
|
||||
|
||||
// Find id="..." within the tag
|
||||
tag := html[idx:tag_end + 1]
|
||||
id_pos := strings.index(tag, `id="`)
|
||||
if id_pos < 0 {
|
||||
// No id — skip this heading, continue searching
|
||||
return next_heading(html, tag_end + 1)
|
||||
}
|
||||
|
||||
id_start := idx + id_pos + 4
|
||||
id_end_rel := strings.index_byte(html[id_start:], '"')
|
||||
if id_end_rel < 0 {
|
||||
return 0, 0, "", "", len(html)
|
||||
}
|
||||
id = html[id_start:id_start + id_end_rel]
|
||||
|
||||
// Text between > and </hN>
|
||||
text_start := tag_end + 1
|
||||
close_idx := strings.index(html[text_start:], "</h")
|
||||
if close_idx < 0 {
|
||||
return 0, 0, "", "", len(html)
|
||||
}
|
||||
text_end := text_start + close_idx
|
||||
text = strip_tags(html[text_start:text_end])
|
||||
|
||||
// Find end of closing tag
|
||||
next_pos = text_end + close_tag_len(html, text_end)
|
||||
|
||||
return idx, level, id, text, next_pos
|
||||
}
|
||||
|
||||
// close_tag_len returns the length of the </hN> tag at pos.
|
||||
close_tag_len :: proc(html: string, pos: int) -> int {
|
||||
if pos + 4 > len(html) {
|
||||
return 4
|
||||
}
|
||||
end := strings.index_byte(html[pos:], '>')
|
||||
if end < 0 {
|
||||
return 4
|
||||
}
|
||||
return end + 1
|
||||
}
|
||||
|
||||
// strip_tags removes HTML tags from a string, leaving only text content.
|
||||
strip_tags :: proc(s: string) -> string {
|
||||
b: strings.Builder
|
||||
strings.builder_init(&b, context.temp_allocator)
|
||||
|
||||
i := 0
|
||||
for i < len(s) {
|
||||
if s[i] == '<' {
|
||||
end := strings.index_byte(s[i:], '>')
|
||||
if end >= 0 {
|
||||
i += end + 1
|
||||
continue
|
||||
}
|
||||
}
|
||||
strings.write_byte(&b, s[i])
|
||||
i += 1
|
||||
}
|
||||
return strings.to_string(b)
|
||||
}
|
||||
|
||||
+395
@@ -0,0 +1,395 @@
|
||||
package main
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:fmt"
|
||||
import "core:log"
|
||||
import "core:mem"
|
||||
import "core:os"
|
||||
import "core:strings"
|
||||
|
||||
DEFAULT_WEIGHT :: 10
|
||||
|
||||
Menu_Entry :: struct {
|
||||
name: string,
|
||||
url: string,
|
||||
weight: Maybe(int),
|
||||
}
|
||||
|
||||
// parse_page_menus converts raw frontmatter JSON into map[string]Menu_Entry.
|
||||
// Supports three forms:
|
||||
// "menus": "main" → {main: {name=title, url=permalink, weight=nil}}
|
||||
// "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: Maybe(int) = nil
|
||||
if entry_obj, ok := entry_val.(json.Object); ok {
|
||||
if w, ok := entry_obj["weight"]; ok {
|
||||
switch wval in w {
|
||||
case json.Integer:
|
||||
weight = int(wval)
|
||||
case json.Float:
|
||||
weight = int(wval)
|
||||
case json.Boolean, json.String, json.Array, json.Object, json.Null:
|
||||
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:
|
||||
// 1. Config menus (thor.json "menus" key present) — exclusive, preserves array order
|
||||
// 2. Auto-menus (sections + root-level pages) + page frontmatter menus — merged, sorted
|
||||
//
|
||||
// If "menus" is present but empty ({}) it means explicit opt-out: no menus.
|
||||
// Config menus cannot be mixed with page frontmatter menus (error).
|
||||
build_menus :: proc(site: ^Site) {
|
||||
if site.menus != nil {
|
||||
has_menus := false
|
||||
for page in site.pages {
|
||||
if len(page.menus) > 0 {
|
||||
has_menus = true
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
// Already populated from config in site_apply_config
|
||||
if len(site.menus) == 0 {
|
||||
// Explicit opt-out ("menus": {})
|
||||
if has_menus {
|
||||
log.warnf(
|
||||
"menus: config has empty menus but pages have frontmatter menu entries; ignoring page menus",
|
||||
)
|
||||
}
|
||||
return
|
||||
}
|
||||
// Config menus active
|
||||
if has_menus {
|
||||
log.fatalf("menus: cannot mix config menus with frontmatter menus")
|
||||
os.exit(1)
|
||||
}
|
||||
warn_all_duplicate_weights(site)
|
||||
return
|
||||
}
|
||||
|
||||
// No config menus — auto-generate, then merge page menus on top
|
||||
collect_auto_menus(site)
|
||||
merge_page_menus(site)
|
||||
warn_all_duplicate_weights(site)
|
||||
}
|
||||
|
||||
// merge_page_menus collects frontmatter menu entries from pages and merges
|
||||
// them into site.menus (which may already contain auto-generated entries).
|
||||
// If no pages have menus set, this is a no-op.
|
||||
merge_page_menus :: proc(site: ^Site) {
|
||||
alloc := site_allocator(site)
|
||||
|
||||
// Collect page entries by menu name
|
||||
page_entries := make(map[string][dynamic]Menu_Entry, 16, alloc)
|
||||
for page in site.pages {
|
||||
for menu_name, entry in page.menus {
|
||||
if _, ok := page_entries[menu_name]; !ok {
|
||||
page_entries[menu_name] = make([dynamic]Menu_Entry, 0, 4, alloc)
|
||||
}
|
||||
// Effective weight: per-menu weight if set, else page.weight
|
||||
effective := entry.weight
|
||||
if effective == nil {
|
||||
effective = page.weight
|
||||
}
|
||||
append(
|
||||
&page_entries[menu_name],
|
||||
Menu_Entry{name = entry.name, url = entry.url, weight = effective},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if len(page_entries) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
if site.menus == nil {
|
||||
site.menus = make(map[string][]Menu_Entry, alloc)
|
||||
}
|
||||
|
||||
for menu_name, entries in page_entries {
|
||||
sort_menu_entries(entries[:])
|
||||
if existing, ok := site.menus[menu_name]; ok {
|
||||
// Merge with existing auto-generated entries
|
||||
merged := make([dynamic]Menu_Entry, 0, len(existing) + len(entries), alloc)
|
||||
append(&merged, ..existing)
|
||||
append(&merged, ..entries[:])
|
||||
sort_menu_entries(merged[:])
|
||||
site.menus[menu_name] = merged[:]
|
||||
} else {
|
||||
site.menus[menu_name] = entries[:]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
collect_auto_menus :: proc(site: ^Site) {
|
||||
alloc := site_allocator(site)
|
||||
sections: map[string]bool
|
||||
|
||||
for page in site.pages {
|
||||
if page._is_index {
|
||||
continue
|
||||
}
|
||||
if page.section != "" {
|
||||
sections[page.section] = true
|
||||
}
|
||||
}
|
||||
|
||||
entries := make([dynamic]Menu_Entry, 0, 8, alloc)
|
||||
|
||||
// Section entries (one per section directory)
|
||||
for section in sections {
|
||||
name := to_title_case(section, alloc)
|
||||
url := fmt.aprintf("/%s/", section, allocator = alloc)
|
||||
skip := false
|
||||
for page in site.pages {
|
||||
if page.section == section && page._is_index {
|
||||
url = page.permalink
|
||||
if page.title != "" {
|
||||
name = page.title
|
||||
}
|
||||
if _, has_main := page.menus["main"]; has_main {
|
||||
skip = true
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
if !skip {
|
||||
append(&entries, Menu_Entry{name = name, url = url})
|
||||
}
|
||||
}
|
||||
|
||||
// Root-level page entries (section = "", not index)
|
||||
for page in site.pages {
|
||||
if page._is_index || page.section != "" || page.title == "" {
|
||||
continue
|
||||
}
|
||||
if _, has_main := page.menus["main"]; has_main {
|
||||
continue
|
||||
}
|
||||
append(&entries, Menu_Entry{name = page.title, url = page.permalink, weight = page.weight})
|
||||
}
|
||||
|
||||
if len(entries) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
sort_menu_entries(entries[:])
|
||||
site.menus = make(map[string][]Menu_Entry, alloc)
|
||||
site.menus["main"] = entries[:]
|
||||
}
|
||||
|
||||
compare_menu_entries :: proc(a, b: Menu_Entry) -> int {
|
||||
aw := a.weight.? or_else DEFAULT_WEIGHT
|
||||
bw := b.weight.? or_else DEFAULT_WEIGHT
|
||||
if aw != bw do return aw - bw
|
||||
a_set := a.weight != nil
|
||||
b_set := b.weight != nil
|
||||
if a_set != b_set {
|
||||
return a_set ? -1 : 1
|
||||
}
|
||||
return strings.compare(a.name, b.name)
|
||||
}
|
||||
|
||||
sort_menu_entries :: proc(entries: []Menu_Entry) {
|
||||
for i in 1 ..< len(entries) {
|
||||
key := entries[i]
|
||||
j := i - 1
|
||||
for j >= 0 && compare_menu_entries(entries[j], key) > 0 {
|
||||
entries[j + 1] = entries[j]
|
||||
j -= 1
|
||||
}
|
||||
entries[j + 1] = key
|
||||
}
|
||||
}
|
||||
|
||||
// warn_duplicate_weights logs a warning for each pair of adjacent entries
|
||||
// (pre-sorted) that have the same explicitly-set weight. Entries with nil
|
||||
// weight (unset/default) are never flagged.
|
||||
warn_duplicate_weights :: proc(menu_name: string, entries: []Menu_Entry) {
|
||||
for i in 0 ..< len(entries) - 1 {
|
||||
if entries[i].weight != nil && entries[i].weight == entries[i + 1].weight {
|
||||
log.warnf(
|
||||
"menus('%s'): '%s' and '%s' share the same menu weight (%d).",
|
||||
menu_name,
|
||||
entries[i].name,
|
||||
entries[i + 1].name,
|
||||
entries[i].weight,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
warn_all_duplicate_weights :: proc(site: ^Site) {
|
||||
for menu_name, entries in site.menus {
|
||||
warn_duplicate_weights(menu_name, entries)
|
||||
}
|
||||
}
|
||||
|
||||
// parse_config_menus converts raw JSON from thor.json into map[string][]Menu_Entry.
|
||||
// Entries are sorted by weight, then name.
|
||||
parse_config_menus :: proc(
|
||||
raw: json.Value,
|
||||
allocator := context.allocator,
|
||||
) -> map[string][]Menu_Entry {
|
||||
obj, ok := raw.(json.Object)
|
||||
if !ok || len(obj) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
result := make(map[string][]Menu_Entry, allocator)
|
||||
for menu_name, menu_val in obj {
|
||||
arr, ok := menu_val.(json.Array)
|
||||
if !ok {
|
||||
log.warnf("menus: '%s' is not an array, skipping", menu_name)
|
||||
continue
|
||||
}
|
||||
|
||||
entries := make([dynamic]Menu_Entry, 0, len(arr), allocator)
|
||||
for item, idx in arr {
|
||||
entry_obj, ok := item.(json.Object)
|
||||
if !ok {
|
||||
log.warnf("menus: '%s' entry %d is not an object, skipping", menu_name, idx)
|
||||
continue
|
||||
}
|
||||
|
||||
name := ""
|
||||
url := ""
|
||||
weight: Maybe(int) = nil
|
||||
|
||||
if v, ok := entry_obj["name"]; ok {
|
||||
if s, ok2 := v.(json.String); ok2 {
|
||||
name = string(s)
|
||||
} else {
|
||||
log.warnf(
|
||||
"menus: '%s' entry %d: 'name' must be a string, got %v, skipping",
|
||||
menu_name,
|
||||
idx,
|
||||
v,
|
||||
)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := entry_obj["url"]; ok {
|
||||
if s, ok2 := v.(json.String); ok2 {
|
||||
url = string(s)
|
||||
} else {
|
||||
log.warnf(
|
||||
"menus: '%s' entry %d: 'url' must be a string, got %v, skipping",
|
||||
menu_name,
|
||||
idx,
|
||||
v,
|
||||
)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
if v, ok := entry_obj["weight"]; ok {
|
||||
switch wval in v {
|
||||
case json.Integer:
|
||||
weight = int(wval)
|
||||
case json.Float:
|
||||
weight = int(wval)
|
||||
case json.Null, json.Boolean, json.String, json.Array, json.Object:
|
||||
log.warnf(
|
||||
"menus: '%s' entry %d: 'weight' must be a number, got %v",
|
||||
menu_name,
|
||||
idx,
|
||||
v,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
if name == "" {
|
||||
log.warnf("menus: '%s' entry %d missing 'name', skipping", menu_name, idx)
|
||||
continue
|
||||
}
|
||||
|
||||
append(&entries, Menu_Entry{name = name, url = url, weight = weight})
|
||||
}
|
||||
sort_menu_entries(entries[:])
|
||||
result[menu_name] = entries[:]
|
||||
}
|
||||
|
||||
return result
|
||||
}
|
||||
+517
@@ -0,0 +1,517 @@
|
||||
#+test
|
||||
package main
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:log"
|
||||
import "core:mem"
|
||||
import "core:os"
|
||||
import "core:strings"
|
||||
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(t, entry.weight == nil, "string form should have nil weight")
|
||||
}
|
||||
|
||||
@(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(t, entry.weight == nil, "object without weight key should have nil weight")
|
||||
}
|
||||
|
||||
@(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(t, entry.weight == nil, "non-object value should have nil weight")
|
||||
}
|
||||
|
||||
@(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(t, entry.weight == nil, "non-numeric weight should have nil weight")
|
||||
}
|
||||
|
||||
@(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")
|
||||
}
|
||||
|
||||
// --- sort_menu_entries tests ---
|
||||
|
||||
@(test)
|
||||
test_sort_weight_orders_correctly :: proc(t: ^testing.T) {
|
||||
entries := []Menu_Entry {
|
||||
{name = "Zeta", url = "/z/"},
|
||||
{name = "Alpha", url = "/a/", weight = 5},
|
||||
{name = "Beta", url = "/b/", weight = 1},
|
||||
}
|
||||
sort_menu_entries(entries)
|
||||
// weight 1 first, then weight 5, then nil (DEFAULT_WEIGHT)
|
||||
testing.expect_value(t, entries[0].name, "Beta")
|
||||
testing.expect_value(t, entries[1].name, "Alpha")
|
||||
testing.expect_value(t, entries[2].name, "Zeta")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_sort_equal_weights_alphabetical :: proc(t: ^testing.T) {
|
||||
entries := []Menu_Entry {
|
||||
{name = "Zebra", url = "/z/"},
|
||||
{name = "Apple", url = "/a/"},
|
||||
{name = "Mango", url = "/m/"},
|
||||
}
|
||||
sort_menu_entries(entries)
|
||||
testing.expect_value(t, entries[0].name, "Apple")
|
||||
testing.expect_value(t, entries[1].name, "Mango")
|
||||
testing.expect_value(t, entries[2].name, "Zebra")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_sort_mixed_weights :: proc(t: ^testing.T) {
|
||||
entries := []Menu_Entry {
|
||||
{name = "Charlie", url = "/c/"},
|
||||
{name = "Alpha", url = "/a/"},
|
||||
{name = "Bravo", url = "/b/", weight = 3},
|
||||
{name = "Delta", url = "/d/", weight = 1},
|
||||
}
|
||||
sort_menu_entries(entries)
|
||||
// weight 1 (Delta), weight 3 (Bravo), then nil weight alphabetical (Alpha, Charlie)
|
||||
testing.expect_value(t, entries[0].name, "Delta")
|
||||
testing.expect_value(t, entries[1].name, "Bravo")
|
||||
testing.expect_value(t, entries[2].name, "Alpha")
|
||||
testing.expect_value(t, entries[3].name, "Charlie")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_sort_explicit_zero_before_nil :: proc(t: ^testing.T) {
|
||||
// Explicit weight 0 is distinguishable from unset (nil → DEFAULT_WEIGHT).
|
||||
// This is the key behavioral improvement of Maybe(int).
|
||||
entries := []Menu_Entry {
|
||||
{name = "Unset", url = "/u/"},
|
||||
{name = "ExplicitZero", url = "/0/", weight = 0},
|
||||
{name = "ExplicitFive", url = "/5/", weight = 5},
|
||||
}
|
||||
sort_menu_entries(entries)
|
||||
// weight 0 first, then weight 5, then nil (DEFAULT_WEIGHT = 10)
|
||||
testing.expect_value(t, entries[0].name, "ExplicitZero")
|
||||
testing.expect_value(t, entries[1].name, "ExplicitFive")
|
||||
testing.expect_value(t, entries[2].name, "Unset")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_config_weight_parsing_and_sort :: 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)
|
||||
|
||||
raw := parse_raw(
|
||||
`{
|
||||
"main": [
|
||||
{"name": "Heavy", "url": "/h/", "weight": 20},
|
||||
{"name": "Light", "url": "/l/", "weight": 1},
|
||||
{"name": "Default", "url": "/d/"}
|
||||
]
|
||||
}`,
|
||||
)
|
||||
|
||||
menus := parse_config_menus(raw, context.allocator)
|
||||
main, ok := menus["main"]
|
||||
testing.expect(t, ok)
|
||||
testing.expect(t, len(main) == 3)
|
||||
testing.expect_value(t, main[0].name, "Light")
|
||||
testing.expect_value(t, main[0].weight, 1)
|
||||
testing.expect_value(t, main[1].name, "Default")
|
||||
testing.expect(t, main[1].weight == nil, "entry without weight should be nil")
|
||||
testing.expect_value(t, main[2].name, "Heavy")
|
||||
testing.expect_value(t, main[2].weight, 20)
|
||||
}
|
||||
|
||||
// --- json_get_int tests ---
|
||||
|
||||
@(test)
|
||||
test_json_get_int_integer :: proc(t: ^testing.T) {
|
||||
obj, _ := json.parse_string(`{"weight": 5}`, spec = .JSON)
|
||||
defer json.destroy_value(obj)
|
||||
o, _ := obj.(json.Object)
|
||||
testing.expect_value(t, json_get_int(o, "weight"), 5)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_json_get_int_float :: proc(t: ^testing.T) {
|
||||
obj, _ := json.parse_string(`{"weight": 5.0}`, spec = .JSON)
|
||||
defer json.destroy_value(obj)
|
||||
o, _ := obj.(json.Object)
|
||||
testing.expect_value(t, json_get_int(o, "weight"), 5)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_json_get_int_missing :: proc(t: ^testing.T) {
|
||||
obj, _ := json.parse_string(`{}`, spec = .JSON)
|
||||
defer json.destroy_value(obj)
|
||||
o, _ := obj.(json.Object)
|
||||
testing.expect(t, json_get_int(o, "weight") == nil, "missing key should return nil")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_json_get_int_non_numeric :: proc(t: ^testing.T) {
|
||||
obj, _ := json.parse_string(`{"weight": "5"}`, spec = .JSON)
|
||||
defer json.destroy_value(obj)
|
||||
o, _ := obj.(json.Object)
|
||||
testing.expect(t, json_get_int(o, "weight") == nil, "non-numeric should return nil")
|
||||
}
|
||||
|
||||
// --- sort_pages tests ---
|
||||
|
||||
@(test)
|
||||
test_sort_pages_weight_primary :: proc(t: ^testing.T) {
|
||||
pages := make(#soa[dynamic]Page, 0, 3)
|
||||
defer delete(pages)
|
||||
append(&pages, Page{title = "Gamma", date = "2025-01-03"})
|
||||
append(&pages, Page{title = "Alpha", date = "2025-01-01", weight = 5})
|
||||
append(&pages, Page{title = "Beta", date = "2025-01-02", weight = 1})
|
||||
|
||||
sort_pages(pages[:])
|
||||
|
||||
// weight 1, weight 5, then nil weight (DEFAULT_WEIGHT)
|
||||
testing.expect_value(t, pages.title[0], "Beta")
|
||||
testing.expect_value(t, pages.title[1], "Alpha")
|
||||
testing.expect_value(t, pages.title[2], "Gamma")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_sort_pages_equal_weights_by_date :: proc(t: ^testing.T) {
|
||||
pages := make(#soa[dynamic]Page, 0, 3)
|
||||
defer delete(pages)
|
||||
append(&pages, Page{title = "Old", date = "2025-01-01"})
|
||||
append(&pages, Page{title = "New", date = "2025-06-01"})
|
||||
append(&pages, Page{title = "Mid", date = "2025-03-01"})
|
||||
|
||||
sort_pages(pages[:])
|
||||
|
||||
// All nil weight → date descending
|
||||
testing.expect_value(t, pages.title[0], "New")
|
||||
testing.expect_value(t, pages.title[1], "Mid")
|
||||
testing.expect_value(t, pages.title[2], "Old")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_sort_pages_mixed :: proc(t: ^testing.T) {
|
||||
pages := make(#soa[dynamic]Page, 0, 4)
|
||||
defer delete(pages)
|
||||
append(&pages, Page{title = "DefaultOld", date = "2025-01-01"})
|
||||
append(&pages, Page{title = "DefaultNew", date = "2025-06-01"})
|
||||
append(&pages, Page{title = "Heavy", date = "2025-03-01", weight = 20})
|
||||
append(&pages, Page{title = "Light", date = "2025-02-01", weight = 1})
|
||||
|
||||
sort_pages(pages[:])
|
||||
|
||||
// weight 1, nil weight (DefaultNew by date), nil weight (DefaultOld by date), weight 20
|
||||
testing.expect_value(t, pages.title[0], "Light")
|
||||
testing.expect_value(t, pages.title[1], "DefaultNew")
|
||||
testing.expect_value(t, pages.title[2], "DefaultOld")
|
||||
testing.expect_value(t, pages.title[3], "Heavy")
|
||||
}
|
||||
|
||||
// --- merge_page_menus effective weight tests ---
|
||||
|
||||
@(test)
|
||||
test_merge_page_menus_weight_fallback :: proc(t: ^testing.T) {
|
||||
site: Site
|
||||
mem.dynamic_arena_init(&site.arena)
|
||||
defer mem.dynamic_arena_destroy(&site.arena)
|
||||
context.allocator = site_allocator(&site)
|
||||
|
||||
page := make_page("Test", "/test/")
|
||||
page.weight = 3
|
||||
page.menus = parse_page_menus(parse_raw(`"main"`), page, site_allocator(&site))
|
||||
|
||||
site.pages = make(#soa[dynamic]Page, 0, 1, site_allocator(&site))
|
||||
append(&site.pages, page)
|
||||
|
||||
// Don't call collect_auto_menus — test merge_page_menus in isolation
|
||||
merge_page_menus(&site)
|
||||
|
||||
main, ok := site.menus["main"]
|
||||
testing.expect(t, ok)
|
||||
testing.expect(t, len(main) == 1, "expected exactly 1 entry")
|
||||
testing.expect_value(t, main[0].name, "Test")
|
||||
testing.expect_value(t, main[0].weight, 3)
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_auto_menus_no_duplicate_with_frontmatter :: proc(t: ^testing.T) {
|
||||
site: Site
|
||||
mem.dynamic_arena_init(&site.arena)
|
||||
defer mem.dynamic_arena_destroy(&site.arena)
|
||||
context.allocator = site_allocator(&site)
|
||||
|
||||
// Root-level page with explicit "menus": "main"
|
||||
page := make_page("Ideas", "/ideas/")
|
||||
page.menus = parse_page_menus(parse_raw(`"main"`), page, site_allocator(&site))
|
||||
|
||||
site.pages = make(#soa[dynamic]Page, 0, 1, site_allocator(&site))
|
||||
append(&site.pages, page)
|
||||
|
||||
collect_auto_menus(&site)
|
||||
merge_page_menus(&site)
|
||||
|
||||
main, ok := site.menus["main"]
|
||||
testing.expect(t, ok)
|
||||
testing.expect(t, len(main) == 1, "expected exactly 1 entry (no duplicate)")
|
||||
testing.expect_value(t, main[0].name, "Ideas")
|
||||
}
|
||||
|
||||
// --- warn_duplicate_weights tests ---
|
||||
|
||||
@(test)
|
||||
test_warn_duplicate_weights_explicit :: proc(t: ^testing.T) {
|
||||
path := "/tmp/thor_test_warn_explicit.log"
|
||||
os.remove(path)
|
||||
f, err := os.open(path, os.O_RDWR | os.O_CREATE | os.O_TRUNC)
|
||||
if err != nil {
|
||||
testing.expect(t, false, "failed to open temp log file")
|
||||
return
|
||||
}
|
||||
|
||||
logger := log.create_file_logger(f)
|
||||
context.logger = logger
|
||||
|
||||
entries := []Menu_Entry {
|
||||
{name = "Alpha", url = "/a/", weight = 5},
|
||||
{name = "Beta", url = "/b/", weight = 5},
|
||||
}
|
||||
warn_duplicate_weights("main", entries)
|
||||
|
||||
log.destroy_file_logger(logger)
|
||||
|
||||
data, _ := os.read_entire_file_from_path(path, context.temp_allocator)
|
||||
output := string(data)
|
||||
os.remove(path)
|
||||
|
||||
testing.expect(t, strings.contains(output, "duplicate weight 5"), "expected weight in warning")
|
||||
testing.expect(t, strings.contains(output, "Alpha"), "expected first entry name")
|
||||
testing.expect(t, strings.contains(output, "Beta"), "expected second entry name")
|
||||
testing.expect(t, strings.contains(output, "'main'"), "expected menu name in warning")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_warn_duplicate_weights_nil_not_flagged :: proc(t: ^testing.T) {
|
||||
path := "/tmp/thor_test_warn_nil.log"
|
||||
os.remove(path)
|
||||
f, err := os.open(path, os.O_RDWR | os.O_CREATE | os.O_TRUNC)
|
||||
if err != nil {
|
||||
testing.expect(t, false, "failed to open temp log file")
|
||||
return
|
||||
}
|
||||
|
||||
logger := log.create_file_logger(f)
|
||||
context.logger = logger
|
||||
|
||||
entries := []Menu_Entry{{name = "Alpha", url = "/a/"}, {name = "Beta", url = "/b/"}}
|
||||
warn_duplicate_weights("main", entries)
|
||||
|
||||
log.destroy_file_logger(logger)
|
||||
|
||||
data, _ := os.read_entire_file_from_path(path, context.temp_allocator)
|
||||
output := string(data)
|
||||
os.remove(path)
|
||||
|
||||
testing.expect(t, output == "", "nil-weight entries should not produce warnings")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_warn_duplicate_weights_explicit_default :: proc(t: ^testing.T) {
|
||||
path := "/tmp/thor_test_warn_default.log"
|
||||
os.remove(path)
|
||||
f, err := os.open(path, os.O_RDWR | os.O_CREATE | os.O_TRUNC)
|
||||
if err != nil {
|
||||
testing.expect(t, false, "failed to open temp log file")
|
||||
return
|
||||
}
|
||||
|
||||
logger := log.create_file_logger(f)
|
||||
context.logger = logger
|
||||
|
||||
entries := []Menu_Entry {
|
||||
{name = "Alpha", url = "/a/", weight = 10},
|
||||
{name = "Beta", url = "/b/", weight = 10},
|
||||
}
|
||||
warn_duplicate_weights("main", entries)
|
||||
|
||||
log.destroy_file_logger(logger)
|
||||
|
||||
data, _ := os.read_entire_file_from_path(path, context.temp_allocator)
|
||||
output := string(data)
|
||||
os.remove(path)
|
||||
|
||||
testing.expect(
|
||||
t,
|
||||
strings.contains(output, "duplicate weight 10"),
|
||||
"explicit weight 10 (== DEFAULT_WEIGHT) should warn — this is the Maybe(int) win",
|
||||
)
|
||||
}
|
||||
+21
-21
@@ -54,7 +54,7 @@ minify_html :: proc(source: string) -> string {
|
||||
segment := source[i:p.end]
|
||||
strings.write_string(&sb, segment)
|
||||
if len(segment) > 0 {
|
||||
last_written = segment[len(segment)-1]
|
||||
last_written = segment[len(segment) - 1]
|
||||
}
|
||||
i = int(p.end)
|
||||
pi += 1
|
||||
@@ -103,27 +103,27 @@ collect_html_ranges :: proc(
|
||||
preserves: ^[dynamic]Range,
|
||||
) {
|
||||
child_count := ts.node_named_child_count(node)
|
||||
for i in 0..<child_count {
|
||||
for i in 0 ..< child_count {
|
||||
child := ts.node_named_child(node, u32(i))
|
||||
type_str := string(ts.node_type(child))
|
||||
|
||||
if type_str == "comment" {
|
||||
append(comments, Range{
|
||||
start = ts.node_start_byte(child),
|
||||
end = ts.node_end_byte(child),
|
||||
})
|
||||
append(
|
||||
comments,
|
||||
Range{start = ts.node_start_byte(child), end = ts.node_end_byte(child)},
|
||||
)
|
||||
} else if type_str == "script_element" || type_str == "style_element" {
|
||||
append(preserves, Range{
|
||||
start = ts.node_start_byte(child),
|
||||
end = ts.node_end_byte(child),
|
||||
})
|
||||
append(
|
||||
preserves,
|
||||
Range{start = ts.node_start_byte(child), end = ts.node_end_byte(child)},
|
||||
)
|
||||
} else if type_str == "element" {
|
||||
tag := html_tag_name(child, source)
|
||||
if is_preserve_tag(tag) {
|
||||
append(preserves, Range{
|
||||
start = ts.node_start_byte(child),
|
||||
end = ts.node_end_byte(child),
|
||||
})
|
||||
append(
|
||||
preserves,
|
||||
Range{start = ts.node_start_byte(child), end = ts.node_end_byte(child)},
|
||||
)
|
||||
} else {
|
||||
collect_html_ranges(child, source, comments, preserves)
|
||||
}
|
||||
@@ -135,11 +135,11 @@ collect_html_ranges :: proc(
|
||||
|
||||
html_tag_name :: proc(element: ts.Node, source: string) -> string {
|
||||
child_count := ts.node_named_child_count(element)
|
||||
for i in 0..<child_count {
|
||||
for i in 0 ..< child_count {
|
||||
child := ts.node_named_child(element, u32(i))
|
||||
if string(ts.node_type(child)) == "start_tag" {
|
||||
tag_child_count := ts.node_named_child_count(child)
|
||||
for j in 0..<tag_child_count {
|
||||
for j in 0 ..< tag_child_count {
|
||||
tag_child := ts.node_named_child(child, u32(j))
|
||||
if string(ts.node_type(tag_child)) == "tag_name" {
|
||||
start := ts.node_start_byte(tag_child)
|
||||
@@ -241,13 +241,13 @@ minify_css :: proc(source: string) -> string {
|
||||
|
||||
collect_css_comments :: proc(node: ts.Node, comments: ^[dynamic]Range) {
|
||||
child_count := ts.node_named_child_count(node)
|
||||
for i in 0..<child_count {
|
||||
for i in 0 ..< child_count {
|
||||
child := ts.node_named_child(node, u32(i))
|
||||
if string(ts.node_type(child)) == "comment" {
|
||||
append(comments, Range{
|
||||
start = ts.node_start_byte(child),
|
||||
end = ts.node_end_byte(child),
|
||||
})
|
||||
append(
|
||||
comments,
|
||||
Range{start = ts.node_start_byte(child), end = ts.node_end_byte(child)},
|
||||
)
|
||||
} else {
|
||||
collect_css_comments(child, comments)
|
||||
}
|
||||
|
||||
@@ -58,7 +58,7 @@ Errors (returned as `Data_Error` at render time):
|
||||
|
||||
#### `format`
|
||||
|
||||
Formats an ISO 8601 date string as a display string. Takes a string, returns a string (e.g. `"2026-03-15T08:49:54-04:00"` → `"15 Mar 2026"`). Invalid input (empty, too-short, non-string, or unparseable) returns a `Data_Error`. Templates that need to skip dateless pages should gate with a section — `{{#date}}<time datetime="{{.}}">{{. | format}}</time>{{/date}}` — so the section's truthiness check catches empty before the filter runs. Commonly used inline as `{{date | format}}` to render a display string while keeping the raw ISO available via `{{date}}` for the `datetime=` attribute.
|
||||
Formats an ISO 8601 date string as a display string. Takes a string, returns a string (e.g. `"2026-03-15T08:49:54-04:00"` → `"15 Mar 2026"`). Invalid input (empty, too-short, non-string, or unparsable) returns a `Data_Error`. Templates that need to skip dateless pages should gate with a section — `{{#date}}<time datetime="{{.}}">{{. | format}}</time>{{/date}}` — so the section's truthiness check catches empty before the filter runs. Commonly used inline as `{{date | format}}` to render a display string while keeping the raw ISO available via `{{date}}` for the `datetime=` attribute.
|
||||
|
||||
Internally: parses the invariant `YYYY-MM-DD` prefix by char offset, stringifies `time.Month(month_num)` and slices `[:3]` for the abbreviation. Accepts any of these ISO 8601 forms (the date prefix is what matters): `2023-10-15T13:18:50-07:00`, `2023-10-15T13:18:50-0700`, `2023-10-15T13:18:50Z`, `2023-10-15T13:18:50`, `2023-10-15`.
|
||||
|
||||
|
||||
+16
-26
@@ -137,6 +137,22 @@ resolve_name :: proc(name: string, ctx: []any) -> any {
|
||||
return result
|
||||
}
|
||||
|
||||
collect_map_keys :: proc(container: any, allocator := context.temp_allocator) -> []string {
|
||||
val, info := base_value(container)
|
||||
if info == nil do return nil
|
||||
if _, ok := info.variant.(runtime.Type_Info_Map); !ok {
|
||||
return nil
|
||||
}
|
||||
out := make([dynamic]string, 0, 4, allocator)
|
||||
it := 0
|
||||
for {
|
||||
key, _ := reflect.iterate_map(val, &it) or_break
|
||||
key_str := key.(string) or_continue
|
||||
append(&out, key_str)
|
||||
}
|
||||
return out[:]
|
||||
}
|
||||
|
||||
// is_truthy checks mustache truthiness.
|
||||
is_truthy :: proc(a: any) -> bool {
|
||||
if a == nil {
|
||||
@@ -159,32 +175,6 @@ is_truthy :: proc(a: any) -> bool {
|
||||
}
|
||||
}
|
||||
|
||||
call_interp_lambda :: proc(val: any) -> (result: string, ok: bool) {
|
||||
switch v in val {
|
||||
case proc() -> string:
|
||||
return v(), true
|
||||
case proc() -> int:
|
||||
return fmt.tprintf("%d", v()), true
|
||||
case proc() -> bool:
|
||||
return "true" if v() else "false", true
|
||||
case:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
call_section_lambda :: proc(val: any, text: string) -> (result: string, ok: bool) {
|
||||
switch v in val {
|
||||
case proc(_: string) -> string:
|
||||
return v(text), true
|
||||
case proc(_: string) -> int:
|
||||
return fmt.tprintf("%d", v(text)), true
|
||||
case proc(_: string) -> bool:
|
||||
return "true" if v(text) else "false", true
|
||||
case:
|
||||
return "", false
|
||||
}
|
||||
}
|
||||
|
||||
// list_info returns element type info, count, and data pointer for a list value.
|
||||
// Returns elem_info=nil if the value is not a list.
|
||||
list_info :: proc(a: any) -> (elem_info: ^runtime.Type_Info, count: int, data: rawptr) {
|
||||
|
||||
@@ -185,6 +185,7 @@ format_error :: proc(
|
||||
context_before: int = 2,
|
||||
context_after: int = 2,
|
||||
colorize: bool = false,
|
||||
span: int = 0,
|
||||
) -> string {
|
||||
line, col := line_col(source, pos)
|
||||
total_lines := count_lines(source)
|
||||
@@ -226,14 +227,17 @@ format_error :: proc(
|
||||
}
|
||||
strings.write_string(&sb, "--> ")
|
||||
strings.write_string(&sb, reset)
|
||||
strings.write_string(&sb, fmt.tprintf("%s:%d:%d\n", path, line, col))
|
||||
fmt.sbprintf(&sb, "%s:%d:%d\n", path, line, col)
|
||||
|
||||
// Top gutter line.
|
||||
write_gutter(&sb, width, faint, reset)
|
||||
|
||||
// Caret extent for the error line.
|
||||
_, token_start, token_end := context_extent(source, pos)
|
||||
line_start, _, _ := context_extent(source, pos)
|
||||
line_start, token_start, token_end := context_extent(source, pos)
|
||||
if span > 0 {
|
||||
token_start = pos
|
||||
token_end = pos + span
|
||||
}
|
||||
caret_start_col := token_start - line_start + 1
|
||||
caret_end_col := token_end - line_start + 1
|
||||
if caret_end_col <= caret_start_col {
|
||||
@@ -309,15 +313,18 @@ write_gutter :: proc(sb: ^strings.Builder, width: int, faint: string, reset: str
|
||||
|
||||
// format_render_error produces a diagnostic for an Error value using the
|
||||
// template's path and source for context. Returns "" for nil errors.
|
||||
// If the error carries its own source/path (from tag_error), those are used
|
||||
// instead of the passed-in template — this ensures errors inside partials
|
||||
// point at the correct file.
|
||||
format_render_error :: proc(err: Error, tmpl: Template, colorize: bool = false) -> string {
|
||||
if err == nil {
|
||||
return ""
|
||||
}
|
||||
path := tmpl.path
|
||||
b := body(err)
|
||||
source := b.source != "" ? b.source : tmpl.source
|
||||
path := b.path != "" ? b.path : tmpl.path
|
||||
if path == "" {
|
||||
path = "<input>"
|
||||
}
|
||||
b := body(err)
|
||||
return format_error(path, tmpl.source, b.pos, b.msg, colorize = colorize)
|
||||
return format_error(path, source, b.pos, b.msg, hint = b.hint, colorize = colorize, span = b.span)
|
||||
}
|
||||
|
||||
|
||||
@@ -557,4 +557,3 @@ test_parse_error_pipe_parse_in_inverted_keeps_double_braces :: proc(t: ^testing.
|
||||
fmt.tprintf("msg should contain literal '{{^', got %q", b.msg),
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
+12
-13
@@ -129,12 +129,12 @@ match_token :: proc(b: ^strings.Builder, dt: Date_Components, s: string) -> int
|
||||
if strings.has_prefix(
|
||||
s,
|
||||
"January",
|
||||
) {strings.write_string(b, fmt.tprintf("%s", time.Month(dt.month))); return 7}
|
||||
) {fmt.sbprintf(b, "%s", time.Month(dt.month)); return 7}
|
||||
if strings.has_prefix(s, "Monday") {emit_weekday(b, dt, full = true); return 6}
|
||||
if strings.has_prefix(
|
||||
s,
|
||||
"2006",
|
||||
) {strings.write_string(b, fmt.tprintf("%04d", dt.year)); return 4}
|
||||
) {fmt.sbprintf(b, "%04d", dt.year); return 4}
|
||||
if strings.has_prefix(s, "MST") {
|
||||
abbr := dt.tz_abbr
|
||||
if len(abbr) == 0 do abbr = "UTC"
|
||||
@@ -146,37 +146,37 @@ match_token :: proc(b: ^strings.Builder, dt: Date_Components, s: string) -> int
|
||||
if strings.has_prefix(
|
||||
s,
|
||||
"06",
|
||||
) {strings.write_string(b, fmt.tprintf("%02d", dt.year % 100)); return 2}
|
||||
if strings.has_prefix(s, "02") {strings.write_string(b, fmt.tprintf("%02d", dt.day)); return 2}
|
||||
) {fmt.sbprintf(b, "%02d", dt.year % 100); return 2}
|
||||
if strings.has_prefix(s, "02") {fmt.sbprintf(b, "%02d", dt.day); return 2}
|
||||
if strings.has_prefix(
|
||||
s,
|
||||
"15",
|
||||
) {strings.write_string(b, fmt.tprintf("%02d", dt.hour)); return 2}
|
||||
) {fmt.sbprintf(b, "%02d", dt.hour); return 2}
|
||||
if strings.has_prefix(
|
||||
s,
|
||||
"04",
|
||||
) {strings.write_string(b, fmt.tprintf("%02d", dt.minute)); return 2}
|
||||
) {fmt.sbprintf(b, "%02d", dt.minute); return 2}
|
||||
if strings.has_prefix(
|
||||
s,
|
||||
"05",
|
||||
) {strings.write_string(b, fmt.tprintf("%02d", dt.second)); return 2}
|
||||
) {fmt.sbprintf(b, "%02d", dt.second); return 2}
|
||||
if strings.has_prefix(
|
||||
s,
|
||||
"01",
|
||||
) {strings.write_string(b, fmt.tprintf("%02d", dt.month)); return 2}
|
||||
) {fmt.sbprintf(b, "%02d", dt.month); return 2}
|
||||
if strings.has_prefix(s, "03") {emit_hour_12(b, dt, pad = true); return 2}
|
||||
if strings.has_prefix(s, "PM") {emit_am_pm(b, dt); return 2}
|
||||
if strings.has_prefix(s, "pm") {emit_am_pm_lower(b, dt); return 2}
|
||||
if len(s) >= 1 {
|
||||
switch s[0] {
|
||||
case '2':
|
||||
strings.write_string(b, fmt.tprintf("%d", dt.day)); return 1
|
||||
fmt.sbprintf(b, "%d", dt.day); return 1
|
||||
case '1':
|
||||
strings.write_string(b, fmt.tprintf("%d", dt.month)); return 1
|
||||
fmt.sbprintf(b, "%d", dt.month); return 1
|
||||
case '4':
|
||||
strings.write_string(b, fmt.tprintf("%d", dt.minute)); return 1
|
||||
fmt.sbprintf(b, "%d", dt.minute); return 1
|
||||
case '5':
|
||||
strings.write_string(b, fmt.tprintf("%d", dt.second)); return 1
|
||||
fmt.sbprintf(b, "%d", dt.second); return 1
|
||||
case '3':
|
||||
emit_hour_12(b, dt, pad = false); return 1
|
||||
case:
|
||||
@@ -321,4 +321,3 @@ convert_to_tz :: proc(
|
||||
},
|
||||
true
|
||||
}
|
||||
|
||||
|
||||
+116
-27
@@ -104,38 +104,64 @@ test_parse_offset_skips_fractional_seconds :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_format_date_weekday_full :: proc(t: ^testing.T) {
|
||||
// 2026-01-01 is a Thursday.
|
||||
dt := Date_Components{year = 2026, month = 1, day = 1}
|
||||
dt := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
}
|
||||
result := format_date(dt, "Monday")
|
||||
testing.expect_value(t, result, "Thursday")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_format_date_weekday_abbr :: proc(t: ^testing.T) {
|
||||
dt := Date_Components{year = 2026, month = 1, day = 1}
|
||||
dt := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
}
|
||||
result := format_date(dt, "Mon")
|
||||
testing.expect_value(t, result, "Thu")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_format_date_month_full_name :: proc(t: ^testing.T) {
|
||||
dt := Date_Components{year = 2026, month = 3, day = 15}
|
||||
dt := Date_Components {
|
||||
year = 2026,
|
||||
month = 3,
|
||||
day = 15,
|
||||
}
|
||||
result := format_date(dt, "January")
|
||||
testing.expect_value(t, result, "March")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_format_date_two_digit_year :: proc(t: ^testing.T) {
|
||||
dt := Date_Components{year = 2026, month = 3, day = 15}
|
||||
dt := Date_Components {
|
||||
year = 2026,
|
||||
month = 3,
|
||||
day = 15,
|
||||
}
|
||||
result := format_date(dt, "06")
|
||||
testing.expect_value(t, result, "26")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_format_date_hour24_padded :: proc(t: ^testing.T) {
|
||||
midnight := Date_Components{year = 2026, month = 1, day = 1, hour = 0}
|
||||
midnight := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
hour = 0,
|
||||
}
|
||||
testing.expect_value(t, format_date(midnight, "15"), "00")
|
||||
|
||||
afternoon := Date_Components{year = 2026, month = 1, day = 1, hour = 13}
|
||||
afternoon := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
hour = 13,
|
||||
}
|
||||
testing.expect_value(t, format_date(afternoon, "15"), "13")
|
||||
}
|
||||
|
||||
@@ -147,7 +173,12 @@ test_format_date_hour12_padded_am_pm_boundaries :: proc(t: ^testing.T) {
|
||||
}{{0, "12 AM"}, {12, "12 PM"}, {13, "01 PM"}, {23, "11 PM"}}
|
||||
|
||||
for &c in cases {
|
||||
dt := Date_Components{year = 2026, month = 1, day = 1, hour = c.hour}
|
||||
dt := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
hour = c.hour,
|
||||
}
|
||||
result := format_date(dt, "03 PM")
|
||||
testing.expect_value(t, result, c.expected)
|
||||
}
|
||||
@@ -155,32 +186,62 @@ test_format_date_hour12_padded_am_pm_boundaries :: proc(t: ^testing.T) {
|
||||
|
||||
@(test)
|
||||
test_format_date_hour12_unpadded :: proc(t: ^testing.T) {
|
||||
one_am := Date_Components{year = 2026, month = 1, day = 1, hour = 1}
|
||||
one_am := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
hour = 1,
|
||||
}
|
||||
testing.expect_value(t, format_date(one_am, "3"), "1")
|
||||
|
||||
one_pm := Date_Components{year = 2026, month = 1, day = 1, hour = 13}
|
||||
one_pm := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
hour = 13,
|
||||
}
|
||||
testing.expect_value(t, format_date(one_pm, "3"), "1")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_format_date_am_pm_lowercase :: proc(t: ^testing.T) {
|
||||
afternoon := Date_Components{year = 2026, month = 1, day = 1, hour = 13}
|
||||
afternoon := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
hour = 13,
|
||||
}
|
||||
testing.expect_value(t, format_date(afternoon, "pm"), "pm")
|
||||
|
||||
morning := Date_Components{year = 2026, month = 1, day = 1, hour = 9}
|
||||
morning := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
hour = 9,
|
||||
}
|
||||
testing.expect_value(t, format_date(morning, "pm"), "am")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_format_date_minute_second_padding :: proc(t: ^testing.T) {
|
||||
dt := Date_Components{year = 2026, month = 1, day = 1, minute = 4, second = 5}
|
||||
dt := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
minute = 4,
|
||||
second = 5,
|
||||
}
|
||||
testing.expect_value(t, format_date(dt, "04:05"), "04:05")
|
||||
testing.expect_value(t, format_date(dt, "4:5"), "4:5")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_format_date_month_day_numeric_padding :: proc(t: ^testing.T) {
|
||||
dt := Date_Components{year = 2026, month = 3, day = 5}
|
||||
dt := Date_Components {
|
||||
year = 2026,
|
||||
month = 3,
|
||||
day = 5,
|
||||
}
|
||||
testing.expect_value(t, format_date(dt, "01"), "03")
|
||||
testing.expect_value(t, format_date(dt, "1"), "3")
|
||||
testing.expect_value(t, format_date(dt, "02"), "05")
|
||||
@@ -191,14 +252,23 @@ test_format_date_month_day_numeric_padding :: proc(t: ^testing.T) {
|
||||
test_format_date_mst_defaults_utc :: proc(t: ^testing.T) {
|
||||
// Date_Components constructed directly (not via parse_iso_date)
|
||||
// defaults to UTC for the MST token.
|
||||
dt := Date_Components{year = 2026, month = 1, day = 1, hour = 12}
|
||||
dt := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
hour = 12,
|
||||
}
|
||||
result := format_date(dt, "MST")
|
||||
testing.expect_value(t, result, "UTC")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_format_date_literal_passthrough :: proc(t: ^testing.T) {
|
||||
dt := Date_Components{year = 2026, month = 1, day = 1}
|
||||
dt := Date_Components {
|
||||
year = 2026,
|
||||
month = 1,
|
||||
day = 1,
|
||||
}
|
||||
result := format_date(dt, "Year: 2006!")
|
||||
testing.expect_value(t, result, "Year: 2026!")
|
||||
}
|
||||
@@ -206,7 +276,14 @@ test_format_date_literal_passthrough :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_format_date_combined_go_reference_layout :: proc(t: ^testing.T) {
|
||||
// 2023-10-15 is a Sunday.
|
||||
dt := Date_Components{year = 2023, month = 10, day = 15, hour = 13, minute = 18, second = 50}
|
||||
dt := Date_Components {
|
||||
year = 2023,
|
||||
month = 10,
|
||||
day = 15,
|
||||
hour = 13,
|
||||
minute = 18,
|
||||
second = 50,
|
||||
}
|
||||
result := format_date(dt, "Mon Jan 2 15:04:05 MST 2006")
|
||||
testing.expect_value(t, result, "Sun Oct 15 13:18:50 UTC 2023")
|
||||
}
|
||||
@@ -241,9 +318,13 @@ test_convert_to_tz_no_offset_assumes_target :: proc(t: ^testing.T) {
|
||||
if !tz_ok do return
|
||||
defer timezone.region_destroy(tz, context.temp_allocator)
|
||||
|
||||
c := Date_Components{
|
||||
year = 2026, month = 3, day = 15,
|
||||
hour = 8, minute = 49, second = 54,
|
||||
c := Date_Components {
|
||||
year = 2026,
|
||||
month = 3,
|
||||
day = 15,
|
||||
hour = 8,
|
||||
minute = 49,
|
||||
second = 54,
|
||||
}
|
||||
result, ok := convert_to_tz(c, tz)
|
||||
testing.expect_value(t, ok, true)
|
||||
@@ -260,11 +341,15 @@ test_convert_to_tz_with_offset_converts :: proc(t: ^testing.T) {
|
||||
defer timezone.region_destroy(tz, context.temp_allocator)
|
||||
|
||||
// 2026-03-15T12:49:54Z (UTC) → 08:49:54 EDT (UTC-4)
|
||||
c := Date_Components{
|
||||
year = 2026, month = 3, day = 15,
|
||||
hour = 12, minute = 49, second = 54,
|
||||
c := Date_Components {
|
||||
year = 2026,
|
||||
month = 3,
|
||||
day = 15,
|
||||
hour = 12,
|
||||
minute = 49,
|
||||
second = 54,
|
||||
offset_seconds = 0,
|
||||
has_offset = true,
|
||||
has_offset = true,
|
||||
}
|
||||
result, ok := convert_to_tz(c, tz)
|
||||
testing.expect_value(t, ok, true)
|
||||
@@ -281,11 +366,15 @@ test_convert_to_tz_with_negative_offset_converts :: proc(t: ^testing.T) {
|
||||
defer timezone.region_destroy(tz, context.temp_allocator)
|
||||
|
||||
// 2026-03-15T08:49:54-04:00 → UTC 12:49:54 → EDT 08:49:54
|
||||
c := Date_Components{
|
||||
year = 2026, month = 3, day = 15,
|
||||
hour = 8, minute = 49, second = 54,
|
||||
c := Date_Components {
|
||||
year = 2026,
|
||||
month = 3,
|
||||
day = 15,
|
||||
hour = 8,
|
||||
minute = 49,
|
||||
second = 54,
|
||||
offset_seconds = -14400,
|
||||
has_offset = true,
|
||||
has_offset = true,
|
||||
}
|
||||
result, ok := convert_to_tz(c, tz)
|
||||
testing.expect_value(t, ok, true)
|
||||
|
||||
@@ -1,155 +0,0 @@
|
||||
#+test
|
||||
package mustache
|
||||
|
||||
import "core:fmt"
|
||||
import "core:testing"
|
||||
|
||||
/*
|
||||
|
||||
// --- Spec test 1: Interpolation ---
|
||||
// A lambda's return value should be interpolated.
|
||||
|
||||
Interp_Data :: struct {
|
||||
lambda: proc() -> string,
|
||||
planet: string,
|
||||
}
|
||||
|
||||
Interp_Data_Int :: struct {
|
||||
lambda: proc() -> int,
|
||||
planet: string,
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_lambda_interpolation :: proc(t: ^testing.T) {
|
||||
data := Interp_Data {
|
||||
lambda = proc() -> string {return "world"},
|
||||
}
|
||||
tpl, _ := parse("Hello, {{lambda}}!", "<test>", context.temp_allocator)
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "Hello, world!")
|
||||
}
|
||||
|
||||
// --- Spec test 2: Interpolation - Expansion ---
|
||||
// A lambda's return value should be parsed.
|
||||
|
||||
@(test)
|
||||
test_lambda_interpolation_expansion :: proc(t: ^testing.T) {
|
||||
data := Interp_Data {
|
||||
lambda = proc() -> string {return "{{planet}}"},
|
||||
planet = "world",
|
||||
}
|
||||
tpl, _ := parse("Hello, {{lambda}}!", "<test>", context.temp_allocator)
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "Hello, world!")
|
||||
}
|
||||
|
||||
// --- Spec test 4: Interpolation - Multiple Calls ---
|
||||
// Interpolated lambdas should not be cached.
|
||||
|
||||
counter_lambda :: proc() -> int {
|
||||
@(static) call_count := 0
|
||||
call_count += 1
|
||||
return call_count
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_lambda_interpolation_multiple_calls :: proc(t: ^testing.T) {
|
||||
data := Interp_Data_Int {
|
||||
lambda = counter_lambda,
|
||||
}
|
||||
tpl, _ := parse("{{lambda}} == {{{lambda}}} == {{lambda}}", "<test>", context.temp_allocator)
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "1 == 2 == 3")
|
||||
}
|
||||
|
||||
// --- Spec test 5: Escaping ---
|
||||
// Lambda results should be appropriately escaped.
|
||||
|
||||
@(test)
|
||||
test_lambda_escaping :: proc(t: ^testing.T) {
|
||||
data := Interp_Data {
|
||||
lambda = proc() -> string {return ">"},
|
||||
}
|
||||
tpl, _ := parse("<{{lambda}}{{{lambda}}}", "<test>", context.temp_allocator)
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "<>>")
|
||||
}
|
||||
|
||||
// --- Spec test 6: Section ---
|
||||
// Lambdas used for sections should receive the raw section string.
|
||||
|
||||
Section_Data :: struct {
|
||||
lambda: proc(_: string) -> string,
|
||||
x: string,
|
||||
planet: string,
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_lambda_section :: proc(t: ^testing.T) {
|
||||
data := Section_Data {
|
||||
lambda = proc(text: string) -> string {
|
||||
if text == "{{x}}" {return "yes"} else {return "no"}
|
||||
},
|
||||
x = "Error!",
|
||||
}
|
||||
tpl, _ := parse("<{{#lambda}}{{x}}{{/lambda}}>", "<test>", context.temp_allocator)
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "<yes>")
|
||||
}
|
||||
|
||||
// --- Spec test 7: Section - Expansion ---
|
||||
// Lambdas used for sections should have their results parsed.
|
||||
|
||||
@(test)
|
||||
test_lambda_section_expansion :: proc(t: ^testing.T) {
|
||||
data := Section_Data {
|
||||
lambda = proc(text: string) -> string {
|
||||
return fmt.tprintf("%s{{{{planet}}}}%s", text, text)
|
||||
},
|
||||
planet = "Earth",
|
||||
}
|
||||
tpl, _ := parse("<{{#lambda}}-{{/lambda}}>", "<test>", context.temp_allocator)
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "<-Earth->")
|
||||
}
|
||||
|
||||
// --- Spec test 9: Section - Multiple Calls ---
|
||||
// Lambdas used for sections should not be cached.
|
||||
|
||||
@(test)
|
||||
test_lambda_section_multiple_calls :: proc(t: ^testing.T) {
|
||||
data := Section_Data {
|
||||
lambda = proc(text: string) -> string {
|
||||
return fmt.tprintf("__%s__", text)
|
||||
},
|
||||
}
|
||||
tpl, _ := parse(
|
||||
"{{#lambda}}FILE{{/lambda}} != {{#lambda}}LINE{{/lambda}}",
|
||||
"<test>",
|
||||
context.temp_allocator,
|
||||
)
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "__FILE__ != __LINE__")
|
||||
}
|
||||
|
||||
// --- Spec test 10: Inverted Section ---
|
||||
// Lambdas used for inverted sections should be considered truthy.
|
||||
|
||||
Inverted_Data :: struct {
|
||||
lambda: proc(_: string) -> bool,
|
||||
static: string,
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_lambda_inverted_section :: proc(t: ^testing.T) {
|
||||
data := Inverted_Data {
|
||||
lambda = proc(text: string) -> bool {return false},
|
||||
static = "static",
|
||||
}
|
||||
tpl, _ := parse("<{{^lambda}}{{static}}{{/lambda}}>", "<test>", context.temp_allocator)
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "<>")
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
+223
-188
@@ -1,34 +1,69 @@
|
||||
package mustache
|
||||
|
||||
import "base:runtime"
|
||||
import "core:fmt"
|
||||
import "core:log"
|
||||
import "core:reflect"
|
||||
import "core:strings"
|
||||
|
||||
// A hypothetical maximum context depth. Trying to pass more than this many items
|
||||
// to render(tpl, data), or nesting templates further than this depth would be
|
||||
// an error.
|
||||
// May be enforced in a later version (for performance)
|
||||
MAX_CONTEXT_DEPTH :: #config(MAX_CONTEXT_DEPTH, 16)
|
||||
|
||||
// Context_Stack is the growable stack of data frames walked top-to-bottom by
|
||||
// resolve_name. Frames are pushed on section descent and popped on exit; the
|
||||
// root data (or each element of a root []any) forms the base frames.
|
||||
Context_Stack :: [dynamic]any
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Error types
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
Error_Kind :: enum {
|
||||
Syntax, // parse-time: malformed template
|
||||
Data, // render-time: template fine, data wrong (e.g. filter misuse)
|
||||
Data, // render-time: template fine, data wrong (e.g. filter misuse)
|
||||
}
|
||||
|
||||
Error_Body :: struct {
|
||||
msg: string,
|
||||
pos: int,
|
||||
kind: Error_Kind,
|
||||
msg: string,
|
||||
pos: int,
|
||||
kind: Error_Kind,
|
||||
source: string,
|
||||
path: string,
|
||||
span: int,
|
||||
hint: string,
|
||||
}
|
||||
|
||||
// Error is nil when no error occurred.
|
||||
Error :: union { Error_Body }
|
||||
Error :: union {
|
||||
Error_Body,
|
||||
}
|
||||
|
||||
// body unwraps the Error_Body from a non-nil Error.
|
||||
// Precondition: err != nil.
|
||||
body :: proc(err: Error) -> Error_Body {
|
||||
switch e in err {
|
||||
case Error_Body: return e
|
||||
case: return {}
|
||||
case Error_Body:
|
||||
return e
|
||||
case:
|
||||
return {}
|
||||
}
|
||||
}
|
||||
|
||||
// tag_error stamps an Error with the source/path of the template where it
|
||||
// originated, so diagnostics point at the correct file (e.g. a partial).
|
||||
tag_error :: proc(err: Error, tmpl: Template) -> Error {
|
||||
if err == nil do return nil
|
||||
b := body(err)
|
||||
return Error_Body {
|
||||
msg = b.msg,
|
||||
pos = b.pos,
|
||||
span = b.span,
|
||||
hint = b.hint,
|
||||
kind = b.kind,
|
||||
source = tmpl.source,
|
||||
path = tmpl.path,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -63,9 +98,11 @@ Node :: struct {
|
||||
// 1 for leaf nodes, 1 + len(children) for container nodes (whose children
|
||||
// are stored contiguously after them in the array).
|
||||
node_span :: proc(n: Node) -> int {
|
||||
#partial switch n.kind {
|
||||
switch n.kind {
|
||||
case .Section, .Inverted, .Parent, .Block:
|
||||
return 1 + len(n.children)
|
||||
case .Text, .Variable, .Unescaped, .Partial:
|
||||
fallthrough
|
||||
case:
|
||||
return 1
|
||||
}
|
||||
@@ -142,12 +179,24 @@ render :: proc(
|
||||
err: Error,
|
||||
) {
|
||||
builder: strings.Builder
|
||||
strings.builder_init(&builder, allocator)
|
||||
defer strings.builder_destroy(&builder)
|
||||
strings.builder_init(&builder, context.temp_allocator)
|
||||
|
||||
ctx := make([dynamic]any, 0, 4, allocator)
|
||||
defer delete(ctx)
|
||||
append(&ctx, data)
|
||||
ctx := make(Context_Stack, 0, 4, context.temp_allocator)
|
||||
|
||||
// If data is a []any, expand into individual context frames.
|
||||
// Otherwise, push as a single frame.
|
||||
elem_info, count, slice_data := list_info(data)
|
||||
if elem_info != nil {
|
||||
if _, is_any := elem_info.variant.(runtime.Type_Info_Any); is_any {
|
||||
for j in 0 ..< count {
|
||||
append(&ctx, extract_list_element(elem_info, slice_data, j))
|
||||
}
|
||||
} else {
|
||||
append(&ctx, data)
|
||||
}
|
||||
} else {
|
||||
append(&ctx, data)
|
||||
}
|
||||
|
||||
all_nodes := tmpl.nodes[:]
|
||||
err = render_nodes(tmpl, all_nodes, &ctx, partials, &builder)
|
||||
@@ -180,6 +229,22 @@ parse_tokens :: proc(
|
||||
return
|
||||
}
|
||||
|
||||
// tag_content_base returns the absolute byte offset in source where the
|
||||
// trimmed tag content begins (after {{, optional sigil, and whitespace).
|
||||
tag_content_base :: proc(source: string, tag_pos: int) -> int {
|
||||
base := tag_pos + 2 // skip {{
|
||||
if base < len(source) {
|
||||
switch source[base] {
|
||||
case '#', '^', '/', '&', '>', '<', '$', '!':
|
||||
base += 1
|
||||
}
|
||||
}
|
||||
for base < len(source) && (source[base] == ' ' || source[base] == '\t') {
|
||||
base += 1
|
||||
}
|
||||
return base
|
||||
}
|
||||
|
||||
parse_section :: proc(
|
||||
tokens: []Token,
|
||||
pos: ^int,
|
||||
@@ -200,7 +265,7 @@ parse_section :: proc(
|
||||
case .Variable:
|
||||
idx := len(nodes)
|
||||
append(nodes, Node{kind = .Variable})
|
||||
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos)
|
||||
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos, tag_content_base(source, tok.pos))
|
||||
if perr != nil {
|
||||
return Error_Body {
|
||||
msg = fmt.tprintf("pipe parse error in '{{{{%s}}}}': %v", tok.value, perr),
|
||||
@@ -214,7 +279,7 @@ parse_section :: proc(
|
||||
case .Unescaped:
|
||||
idx := len(nodes)
|
||||
append(nodes, Node{kind = .Unescaped})
|
||||
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos)
|
||||
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos, tag_content_base(source, tok.pos))
|
||||
if perr != nil {
|
||||
return Error_Body {
|
||||
msg = fmt.tprintf("pipe parse error in '{{{{&%s}}}}': %v", tok.value, perr),
|
||||
@@ -234,7 +299,7 @@ parse_section :: proc(
|
||||
content_start := 0
|
||||
if pos^ < len(tokens) {content_start = tokens[pos^].pos}
|
||||
append(nodes, Node{kind = .Section, pos = tok.pos})
|
||||
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos)
|
||||
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos, tag_content_base(source, tok.pos))
|
||||
if perr != nil {
|
||||
return Error_Body {
|
||||
msg = fmt.tprintf("pipe parse error in '{{{{#%s}}}}': %v", tok.value, perr),
|
||||
@@ -255,7 +320,7 @@ parse_section :: proc(
|
||||
content_start := 0
|
||||
if pos^ < len(tokens) {content_start = tokens[pos^].pos}
|
||||
append(nodes, Node{kind = .Inverted, pos = tok.pos})
|
||||
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos)
|
||||
pipe_key, perr := parse_pipeline(tok.value, &nodes[idx].filters, tok.pos, tag_content_base(source, tok.pos))
|
||||
if perr != nil {
|
||||
return Error_Body {
|
||||
msg = fmt.tprintf("pipe parse error in '{{{{^%s}}}}': %v", tok.value, perr),
|
||||
@@ -272,14 +337,14 @@ parse_section :: proc(
|
||||
|
||||
case .Section_Close:
|
||||
if strings.contains(tok.value, "|") {
|
||||
return Error_Body {
|
||||
msg = fmt.tprintf(
|
||||
"pipe expression not allowed in close tag '{{{{/%s}}}}' — use the bare key",
|
||||
tok.value,
|
||||
),
|
||||
pos = tok.pos,
|
||||
kind = .Syntax,
|
||||
}
|
||||
return Error_Body {
|
||||
msg = fmt.tprintf(
|
||||
"pipe expression not allowed in close tag '{{{{/%s}}}}' — use the bare key",
|
||||
tok.value,
|
||||
),
|
||||
pos = tok.pos,
|
||||
kind = .Syntax,
|
||||
}
|
||||
}
|
||||
if end_tag != "" && tok.value == end_tag {
|
||||
pos^ += 1
|
||||
@@ -316,12 +381,7 @@ parse_section :: proc(
|
||||
idx := len(nodes)
|
||||
append(
|
||||
nodes,
|
||||
Node {
|
||||
kind = .Parent,
|
||||
key = tok.value,
|
||||
indent = tok.indent,
|
||||
pos = tok.pos,
|
||||
},
|
||||
Node{kind = .Parent, key = tok.value, indent = tok.indent, pos = tok.pos},
|
||||
)
|
||||
parse_section(tokens, pos, nodes, tok.value, source, allocator, tok.pos) or_return
|
||||
nodes[idx].children = nodes[idx + 1:len(nodes)]
|
||||
@@ -329,15 +389,7 @@ parse_section :: proc(
|
||||
case .Block_Open:
|
||||
pos^ += 1
|
||||
idx := len(nodes)
|
||||
append(
|
||||
nodes,
|
||||
Node {
|
||||
kind = .Block,
|
||||
key = tok.value,
|
||||
indent = tok.indent,
|
||||
pos = tok.pos,
|
||||
},
|
||||
)
|
||||
append(nodes, Node{kind = .Block, key = tok.value, indent = tok.indent, pos = tok.pos})
|
||||
parse_section(tokens, pos, nodes, tok.value, source, allocator, tok.pos) or_return
|
||||
nodes[idx].children = nodes[idx + 1:len(nodes)]
|
||||
}
|
||||
@@ -360,7 +412,7 @@ parse_section :: proc(
|
||||
deindent_blocks :: proc(nodes: []Node, allocator := context.allocator) {
|
||||
i := 0
|
||||
for i < len(nodes) {
|
||||
#partial switch nodes[i].kind {
|
||||
switch nodes[i].kind {
|
||||
case .Block:
|
||||
if len(nodes[i].children) > 0 {
|
||||
children := nodes[i].children
|
||||
@@ -387,6 +439,8 @@ deindent_blocks :: proc(nodes: []Node, allocator := context.allocator) {
|
||||
if len(nodes[i].children) > 0 {
|
||||
deindent_blocks(nodes[i].children, allocator)
|
||||
}
|
||||
case .Text, .Variable, .Unescaped, .Partial:
|
||||
// Do nothing
|
||||
}
|
||||
i += node_span(nodes[i])
|
||||
}
|
||||
@@ -484,21 +538,29 @@ remove_line_indent :: proc(s: string, indent: string, allocator := context.alloc
|
||||
|
||||
render_template :: proc(
|
||||
pt: Template,
|
||||
ctx: ^[dynamic]any,
|
||||
ctx: ^Context_Stack,
|
||||
partials: map[string]Template,
|
||||
b: ^strings.Builder,
|
||||
blocks: map[string]Block_Override,
|
||||
indent: string,
|
||||
) -> Error {
|
||||
if len(indent) > 0 {
|
||||
state := Indent_State{indent = indent, at_line_start = false}
|
||||
state := Indent_State {
|
||||
indent = indent,
|
||||
at_line_start = false,
|
||||
}
|
||||
strings.write_string(b, indent) // first line always gets indent
|
||||
return render_nodes(pt, pt.nodes[:], ctx, partials, b, blocks, &state)
|
||||
}
|
||||
return render_nodes(pt, pt.nodes[:], ctx, partials, b, blocks, nil)
|
||||
}
|
||||
|
||||
write_indented :: proc(b: ^strings.Builder, indent: string, content: string, at_line_start: ^bool) {
|
||||
write_indented :: proc(
|
||||
b: ^strings.Builder,
|
||||
indent: string,
|
||||
content: string,
|
||||
at_line_start: ^bool,
|
||||
) {
|
||||
if len(indent) == 0 || len(content) == 0 {
|
||||
strings.write_string(b, content)
|
||||
return
|
||||
@@ -529,7 +591,7 @@ write_indented :: proc(b: ^strings.Builder, indent: string, content: string, at_
|
||||
render_nodes :: proc(
|
||||
current: Template,
|
||||
nodes: []Node,
|
||||
ctx: ^[dynamic]any,
|
||||
ctx: ^Context_Stack,
|
||||
partials: map[string]Template,
|
||||
b: ^strings.Builder,
|
||||
blocks: map[string]Block_Override = nil,
|
||||
@@ -559,34 +621,11 @@ render_nodes :: proc(
|
||||
if len(node.filters) > 0 {
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:])
|
||||
if perr != nil {
|
||||
return perr
|
||||
return tag_error(perr, current)
|
||||
}
|
||||
val = transformed
|
||||
}
|
||||
if result_str, ok := call_interp_lambda(val); ok {
|
||||
sub_tpl, perr := parse(
|
||||
result_str,
|
||||
fmt.tprintf("<lambda output from '%s'>", node.key),
|
||||
context.temp_allocator,
|
||||
context.temp_allocator,
|
||||
)
|
||||
if perr == nil {
|
||||
temp: strings.Builder
|
||||
strings.builder_init(&temp, context.temp_allocator)
|
||||
render_nodes(
|
||||
sub_tpl,
|
||||
sub_tpl.nodes[:],
|
||||
ctx,
|
||||
partials,
|
||||
&temp,
|
||||
blocks,
|
||||
nil,
|
||||
) or_return
|
||||
write_value(b, strings.to_string(temp), escape = true)
|
||||
}
|
||||
} else {
|
||||
write_value(b, val, escape = true)
|
||||
}
|
||||
write_value(b, val, escape = true)
|
||||
i += 1
|
||||
|
||||
case .Unescaped:
|
||||
@@ -601,34 +640,11 @@ render_nodes :: proc(
|
||||
if len(node.filters) > 0 {
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:])
|
||||
if perr != nil {
|
||||
return perr
|
||||
return tag_error(perr, current)
|
||||
}
|
||||
val = transformed
|
||||
}
|
||||
if result_str, ok := call_interp_lambda(val); ok {
|
||||
sub_tpl, perr := parse(
|
||||
result_str,
|
||||
fmt.tprintf("<lambda output from '%s'>", node.key),
|
||||
context.temp_allocator,
|
||||
context.temp_allocator,
|
||||
)
|
||||
if perr == nil {
|
||||
temp: strings.Builder
|
||||
strings.builder_init(&temp, context.temp_allocator)
|
||||
render_nodes(
|
||||
sub_tpl,
|
||||
sub_tpl.nodes[:],
|
||||
ctx,
|
||||
partials,
|
||||
&temp,
|
||||
blocks,
|
||||
nil,
|
||||
) or_return
|
||||
write_value(b, strings.to_string(temp), escape = false)
|
||||
}
|
||||
} else {
|
||||
write_value(b, val, escape = false)
|
||||
}
|
||||
write_value(b, val, escape = false)
|
||||
i += 1
|
||||
|
||||
case .Section:
|
||||
@@ -639,35 +655,30 @@ render_nodes :: proc(
|
||||
if len(node.filters) > 0 {
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:])
|
||||
if perr != nil {
|
||||
return perr
|
||||
return tag_error(perr, current)
|
||||
}
|
||||
val = transformed
|
||||
}
|
||||
if result_str, ok := call_section_lambda(val, node.content); ok {
|
||||
sub_tpl, perr := parse(
|
||||
result_str,
|
||||
fmt.tprintf("<lambda output from '%s'>", node.key),
|
||||
context.temp_allocator,
|
||||
context.temp_allocator,
|
||||
)
|
||||
if perr == nil {
|
||||
render_nodes(
|
||||
sub_tpl,
|
||||
sub_tpl.nodes[:],
|
||||
ctx,
|
||||
partials,
|
||||
b,
|
||||
blocks,
|
||||
nil,
|
||||
) or_return
|
||||
}
|
||||
} else if is_truthy(val) {
|
||||
children := node.children
|
||||
elem_info, count, data := list_info(val)
|
||||
if elem_info != nil {
|
||||
for j in 0 ..< count {
|
||||
elem := extract_list_element(elem_info, data, j)
|
||||
append(ctx, elem)
|
||||
if is_truthy(val) {
|
||||
children := node.children
|
||||
elem_info, count, data := list_info(val)
|
||||
if elem_info != nil {
|
||||
for j in 0 ..< count {
|
||||
elem := extract_list_element(elem_info, data, j)
|
||||
context_push(ctx, elem, current, node)
|
||||
defer pop(ctx)
|
||||
render_nodes(
|
||||
current,
|
||||
children,
|
||||
ctx,
|
||||
partials,
|
||||
b,
|
||||
blocks,
|
||||
indent_state,
|
||||
) or_return
|
||||
}
|
||||
} else {
|
||||
context_push(ctx, val, current, node)
|
||||
defer pop(ctx)
|
||||
render_nodes(
|
||||
current,
|
||||
@@ -679,13 +690,8 @@ render_nodes :: proc(
|
||||
indent_state,
|
||||
) or_return
|
||||
}
|
||||
} else {
|
||||
append(ctx, val)
|
||||
defer pop(ctx)
|
||||
render_nodes(current, children, ctx, partials, b, blocks, indent_state) or_return
|
||||
}
|
||||
}
|
||||
i += 1 + len(node.children)
|
||||
i += 1 + len(node.children)
|
||||
|
||||
case .Inverted:
|
||||
val := resolve_name(node.key, ctx[:])
|
||||
@@ -695,14 +701,22 @@ render_nodes :: proc(
|
||||
if len(node.filters) > 0 {
|
||||
transformed, perr := apply_pipeline(val, node.filters[:], node.pos, ctx[:])
|
||||
if perr != nil {
|
||||
return perr
|
||||
return tag_error(perr, current)
|
||||
}
|
||||
val = transformed
|
||||
}
|
||||
if !is_truthy(val) {
|
||||
render_nodes(current, node.children, ctx, partials, b, blocks, indent_state) or_return
|
||||
}
|
||||
i += 1 + len(node.children)
|
||||
if !is_truthy(val) {
|
||||
render_nodes(
|
||||
current,
|
||||
node.children,
|
||||
ctx,
|
||||
partials,
|
||||
b,
|
||||
blocks,
|
||||
indent_state,
|
||||
) or_return
|
||||
}
|
||||
i += 1 + len(node.children)
|
||||
|
||||
case .Partial:
|
||||
name := node.key
|
||||
@@ -721,64 +735,64 @@ render_nodes :: proc(
|
||||
}
|
||||
i += 1
|
||||
|
||||
case .Block:
|
||||
content_nodes: []Node
|
||||
content_blocks := blocks
|
||||
render_current := current
|
||||
case .Block:
|
||||
content_nodes: []Node
|
||||
content_blocks := blocks
|
||||
render_current := current
|
||||
|
||||
found_override := false
|
||||
if blocks != nil {
|
||||
if o, ok := blocks[node.key]; ok {
|
||||
content_nodes = o.nodes
|
||||
found_override = true
|
||||
render_current = o.source
|
||||
found_override := false
|
||||
if blocks != nil {
|
||||
if o, ok := blocks[node.key]; ok {
|
||||
content_nodes = o.nodes
|
||||
found_override = true
|
||||
render_current = o.source
|
||||
}
|
||||
}
|
||||
}
|
||||
if !found_override {
|
||||
content_nodes = node.children
|
||||
}
|
||||
|
||||
if len(node.indent) > 0 {
|
||||
temp: strings.Builder
|
||||
strings.builder_init(&temp, context.temp_allocator)
|
||||
render_nodes(
|
||||
render_current,
|
||||
content_nodes,
|
||||
ctx,
|
||||
partials,
|
||||
&temp,
|
||||
content_blocks,
|
||||
nil,
|
||||
) or_return
|
||||
at_ls := true
|
||||
write_indented(b, node.indent, strings.to_string(temp), &at_ls)
|
||||
} else {
|
||||
render_nodes(
|
||||
render_current,
|
||||
content_nodes,
|
||||
ctx,
|
||||
partials,
|
||||
b,
|
||||
content_blocks,
|
||||
indent_state,
|
||||
) or_return
|
||||
}
|
||||
i += 1 + len(node.children)
|
||||
|
||||
case .Parent:
|
||||
parent_children := node.children
|
||||
merged := merge_block_overrides(parent_children, blocks, current)
|
||||
pt, found := partials[node.key]
|
||||
if !found {
|
||||
warn_missing_partial(current, partials, node, node.key)
|
||||
} else {
|
||||
warn_unmatched_block_overrides(current, pt, parent_children)
|
||||
render_template(pt, ctx, partials, b, merged, node.indent) or_return
|
||||
if indent_state != nil {
|
||||
indent_state.at_line_start = false
|
||||
if !found_override {
|
||||
content_nodes = node.children
|
||||
}
|
||||
}
|
||||
i += 1 + len(node.children)
|
||||
|
||||
if len(node.indent) > 0 {
|
||||
temp: strings.Builder
|
||||
strings.builder_init(&temp, context.temp_allocator)
|
||||
render_nodes(
|
||||
render_current,
|
||||
content_nodes,
|
||||
ctx,
|
||||
partials,
|
||||
&temp,
|
||||
content_blocks,
|
||||
nil,
|
||||
) or_return
|
||||
at_ls := true
|
||||
write_indented(b, node.indent, strings.to_string(temp), &at_ls)
|
||||
} else {
|
||||
render_nodes(
|
||||
render_current,
|
||||
content_nodes,
|
||||
ctx,
|
||||
partials,
|
||||
b,
|
||||
content_blocks,
|
||||
indent_state,
|
||||
) or_return
|
||||
}
|
||||
i += 1 + len(node.children)
|
||||
|
||||
case .Parent:
|
||||
parent_children := node.children
|
||||
merged := merge_block_overrides(parent_children, blocks, current)
|
||||
pt, found := partials[node.key]
|
||||
if !found {
|
||||
warn_missing_partial(current, partials, node, node.key)
|
||||
} else {
|
||||
warn_unmatched_block_overrides(current, pt, parent_children)
|
||||
render_template(pt, ctx, partials, b, merged, node.indent) or_return
|
||||
if indent_state != nil {
|
||||
indent_state.at_line_start = false
|
||||
}
|
||||
}
|
||||
i += 1 + len(node.children)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
@@ -922,3 +936,24 @@ warn_unmatched_block_overrides :: proc(
|
||||
}
|
||||
}
|
||||
|
||||
context_push :: proc(ctx: ^Context_Stack, val: any, current: Template, node: Node) {
|
||||
append(ctx, val)
|
||||
if len(ctx^) == MAX_CONTEXT_DEPTH + 1 {
|
||||
warn_context_depth(current, node)
|
||||
}
|
||||
}
|
||||
|
||||
// warn_context_depth emits a diagnostic warning pointing at the section tag
|
||||
// whose push carried the context stack past MAX_CONTEXT_DEPTH.
|
||||
warn_context_depth :: proc(current: Template, node: Node) {
|
||||
msg := fmt.tprintf(
|
||||
"context stack depth exceeded %d (possible recursive section/partial)",
|
||||
MAX_CONTEXT_DEPTH,
|
||||
)
|
||||
path := current.path
|
||||
if path == "" {
|
||||
path = "<input>"
|
||||
}
|
||||
diag := format_error(path, current.source, node.pos, msg, "", colorize = should_colorize())
|
||||
log.warnf("%s", diag)
|
||||
}
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
#+test
|
||||
package mustache
|
||||
|
||||
import "core:log"
|
||||
import "core:mem"
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
|
||||
@(test)
|
||||
@@ -124,3 +126,38 @@ leak_repeated_render :: proc(t: ^testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
// A deeply nested map pushes the context stack past MAX_CONTEXT_DEPTH (16).
|
||||
// The depth warning must be non-fatal: rendering still succeeds.
|
||||
@(test)
|
||||
test_context_depth_warns :: proc(t: ^testing.T) {
|
||||
context.logger = log.nil_logger()
|
||||
|
||||
AMT :: MAX_CONTEXT_DEPTH + 2
|
||||
|
||||
// Build AMT nested {x: {...}} levels; the innermost holds `leaf`.
|
||||
data := make(map[string]any, context.temp_allocator)
|
||||
data["leaf"] = "found"
|
||||
for _ in 0 ..< AMT {
|
||||
outer := make(map[string]any, context.temp_allocator)
|
||||
outer["x"] = data
|
||||
data = outer
|
||||
}
|
||||
|
||||
// Template: AMT nested {{#x}} sections around {{leaf}}.
|
||||
src: strings.Builder
|
||||
strings.builder_init(&src, context.temp_allocator)
|
||||
for _ in 0 ..< AMT do strings.write_string(&src, "{{#x}}")
|
||||
strings.write_string(&src, "{{leaf}}")
|
||||
for _ in 0 ..< AMT do strings.write_string(&src, "{{/x}}")
|
||||
template := strings.to_string(src)
|
||||
|
||||
tmpl, perr := parse(template, "<depth-test>", context.temp_allocator)
|
||||
testing.expect(t, perr == nil, "should parse")
|
||||
if perr != nil {
|
||||
return
|
||||
}
|
||||
|
||||
result, rerr := render(tmpl, data, allocator = context.temp_allocator)
|
||||
testing.expect(t, rerr == nil, "depth warning must be non-fatal")
|
||||
testing.expect_value(t, result, "found")
|
||||
}
|
||||
|
||||
+101
-30
@@ -1,5 +1,6 @@
|
||||
package mustache
|
||||
|
||||
import "base:runtime"
|
||||
import "core:fmt"
|
||||
import "core:log"
|
||||
import "core:reflect"
|
||||
@@ -16,9 +17,48 @@ MAX_PIPE_ARGS :: 2
|
||||
|
||||
DEFAULT_DATE_FORMAT :: "2 Jan 2006"
|
||||
|
||||
Pipe_Op :: enum {
|
||||
Format,
|
||||
Group_By,
|
||||
}
|
||||
|
||||
// Pipe op names are derived from the enum via reflection (lowercased).
|
||||
// Adding a new op only requires adding it to the enum AND handling it in
|
||||
// apply_filter's switch — the compiler enforces exhaustive matching.
|
||||
|
||||
pipe_op_from_string :: proc(s: string) -> (Pipe_Op, bool) {
|
||||
ti := type_info_of(typeid_of(Pipe_Op))
|
||||
base := runtime.type_info_base(ti)
|
||||
#partial switch &e in base.variant {
|
||||
case runtime.Type_Info_Enum:
|
||||
for name, idx in e.names {
|
||||
lower := strings.to_lower(name, context.temp_allocator) or_else name
|
||||
if lower == s {
|
||||
return cast(Pipe_Op)idx, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return {}, false
|
||||
}
|
||||
|
||||
pipe_op_candidates :: proc(allocator := context.temp_allocator) -> []string {
|
||||
ti := type_info_of(typeid_of(Pipe_Op))
|
||||
base := runtime.type_info_base(ti)
|
||||
out := make([dynamic]string, 0, 2, allocator)
|
||||
#partial switch &e in base.variant {
|
||||
case runtime.Type_Info_Enum:
|
||||
for name in e.names {
|
||||
lower := strings.to_lower(name, allocator) or_else name
|
||||
append(&out, lower)
|
||||
}
|
||||
}
|
||||
return out[:]
|
||||
}
|
||||
|
||||
Pipe_Filter :: struct {
|
||||
op: string,
|
||||
args: [dynamic; MAX_PIPE_ARGS]string,
|
||||
op: string,
|
||||
args: [dynamic; MAX_PIPE_ARGS]string,
|
||||
op_pos: int,
|
||||
}
|
||||
|
||||
Group :: struct {
|
||||
@@ -53,8 +93,8 @@ tokenize_fields :: proc(seg: string, pos: int) -> (tokens: [dynamic]string, err:
|
||||
}
|
||||
if j >= len(seg) {
|
||||
return tokens, Error_Body {
|
||||
msg = fmt.tprintf("unterminated string literal: %s", seg),
|
||||
pos = pos,
|
||||
msg = fmt.tprintf("unterminated string literal: %s", seg),
|
||||
pos = pos,
|
||||
kind = .Syntax,
|
||||
}
|
||||
}
|
||||
@@ -77,6 +117,7 @@ parse_pipeline :: proc(
|
||||
content: string,
|
||||
filters_out: ^[dynamic; MAX_PIPES]Pipe_Filter,
|
||||
pos: int,
|
||||
content_base: int,
|
||||
) -> (
|
||||
key: string,
|
||||
err: Error,
|
||||
@@ -86,14 +127,13 @@ parse_pipeline :: proc(
|
||||
return key, nil
|
||||
}
|
||||
|
||||
segments := strings.split(content, "|", allocator = context.temp_allocator)
|
||||
|
||||
filter_count := len(segments) - 1
|
||||
if filter_count > MAX_PIPES {
|
||||
// Count pipes to validate against MAX_PIPES.
|
||||
pipe_count := strings.count(content, "|")
|
||||
if pipe_count > MAX_PIPES {
|
||||
return "", Error_Body {
|
||||
msg = fmt.tprintf(
|
||||
"pipe expression has %d filters, max is %d",
|
||||
filter_count,
|
||||
pipe_count,
|
||||
MAX_PIPES,
|
||||
),
|
||||
pos = pos,
|
||||
@@ -101,21 +141,38 @@ parse_pipeline :: proc(
|
||||
}
|
||||
}
|
||||
|
||||
key = strings.trim_space(segments[0])
|
||||
// Key is everything before the first |.
|
||||
first_pipe := strings.index(content, "|")
|
||||
key = strings.trim_space(content[:first_pipe])
|
||||
if len(key) == 0 {
|
||||
return "", Error_Body{msg = "pipe expression missing key", pos = pos, kind = .Syntax}
|
||||
}
|
||||
|
||||
if filter_count == 0 {
|
||||
if pipe_count == 0 {
|
||||
return key, nil
|
||||
}
|
||||
|
||||
for i in 0 ..< filter_count {
|
||||
seg := strings.trim_space(segments[i + 1])
|
||||
// Walk pipe-delimited segments, tracking byte offsets within content.
|
||||
seg_start := first_pipe + 1 // offset in content, just after |
|
||||
for seg_start <= len(content) {
|
||||
next_pipe := strings.index(content[seg_start:], "|")
|
||||
|
||||
// Raw segment text (may have leading/trailing whitespace).
|
||||
seg_end := seg_start + next_pipe if next_pipe >= 0 else len(content)
|
||||
raw_seg := content[seg_start:seg_end]
|
||||
|
||||
// Count leading whitespace to find op offset within content.
|
||||
ws := 0
|
||||
for ws < len(raw_seg) && is_pipe_space(raw_seg[ws]) {
|
||||
ws += 1
|
||||
}
|
||||
seg := strings.trim_space(raw_seg)
|
||||
if len(seg) == 0 {
|
||||
return "", Error_Body{msg = "empty filter", pos = pos, kind = .Syntax}
|
||||
}
|
||||
|
||||
op_offset_in_content := seg_start + ws
|
||||
|
||||
tokens, terr := tokenize_fields(seg, pos)
|
||||
if terr != nil {
|
||||
delete(tokens)
|
||||
@@ -140,13 +197,19 @@ parse_pipeline :: proc(
|
||||
}
|
||||
|
||||
filter := Pipe_Filter {
|
||||
op = tokens[0],
|
||||
op = tokens[0],
|
||||
op_pos = content_base + op_offset_in_content,
|
||||
}
|
||||
for j in 1 ..< len(tokens) {
|
||||
append(&filter.args, tokens[j])
|
||||
}
|
||||
append(filters_out, filter)
|
||||
delete(tokens)
|
||||
|
||||
if next_pipe < 0 {
|
||||
break
|
||||
}
|
||||
seg_start = seg_end + 1
|
||||
}
|
||||
|
||||
return key, nil
|
||||
@@ -177,28 +240,43 @@ resolve_format_string :: proc(name: string, ctx: []any, pos: int) -> (string, Er
|
||||
raw := resolve_name(name, ctx)
|
||||
if raw == nil {
|
||||
return "", Error_Body {
|
||||
msg = fmt.tprintf("unable to resolve date format key '%s'", name),
|
||||
pos = pos,
|
||||
msg = fmt.tprintf("unable to resolve date format key '%s'", name),
|
||||
pos = pos,
|
||||
kind = .Data,
|
||||
}
|
||||
}
|
||||
str, ok := reflect.as_string(raw)
|
||||
if !ok {
|
||||
return "", Error_Body {
|
||||
msg = fmt.tprintf("date format key '%s' is not a string", name),
|
||||
pos = pos,
|
||||
msg = fmt.tprintf("date format key '%s' is not a string", name),
|
||||
pos = pos,
|
||||
kind = .Data,
|
||||
}
|
||||
}
|
||||
return str, nil
|
||||
}
|
||||
|
||||
// TODO: diagnostics don't show anything relevent
|
||||
apply_filter :: proc(value: any, filter: ^Pipe_Filter, pos: int, ctx: []any) -> (any, Error) {
|
||||
switch filter.op {
|
||||
case "group_by":
|
||||
op, ok := pipe_op_from_string(filter.op)
|
||||
if !ok {
|
||||
hint := ""
|
||||
suggestion := suggest_correction(pipe_op_candidates(), filter.op)
|
||||
if suggestion != "" {
|
||||
hint = fmt.tprintf("did you mean '%s'?", suggestion)
|
||||
}
|
||||
return nil, Error_Body {
|
||||
msg = fmt.tprintf("unknown pipe op '%s'", filter.op),
|
||||
pos = filter.op_pos,
|
||||
span = len(filter.op),
|
||||
kind = .Data,
|
||||
hint = hint,
|
||||
}
|
||||
}
|
||||
|
||||
switch op {
|
||||
case .Group_By:
|
||||
return apply_group_by(value, filter.args[:], pos)
|
||||
case "format":
|
||||
case .Format:
|
||||
str, ok := reflect.as_string(value)
|
||||
if !ok {
|
||||
return value, Error_Body {
|
||||
@@ -236,14 +314,8 @@ apply_filter :: proc(value: any, filter: ^Pipe_Filter, pos: int, ctx: []any) ->
|
||||
} else {
|
||||
return any{new_clone(str2, context.temp_allocator), typeid_of(string)}, nil
|
||||
}
|
||||
|
||||
case:
|
||||
return nil, Error_Body {
|
||||
msg = fmt.tprintf("unknown pipe op '%s'", filter.op),
|
||||
pos = pos,
|
||||
kind = .Data,
|
||||
}
|
||||
}
|
||||
return {}, nil
|
||||
}
|
||||
|
||||
apply_format :: proc(
|
||||
@@ -337,4 +409,3 @@ apply_group_by :: proc(value: any, args: []string, pos: int) -> (result: any, er
|
||||
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
|
||||
+48
-14
@@ -2,6 +2,7 @@
|
||||
package mustache
|
||||
|
||||
import "core:fmt"
|
||||
import "core:strings"
|
||||
import "core:testing"
|
||||
import "core:time/datetime"
|
||||
import "core:time/timezone"
|
||||
@@ -214,7 +215,7 @@ test_interp_pipe_basic :: proc(t: ^testing.T) {
|
||||
timezone: ^datetime.TZ_Region,
|
||||
}
|
||||
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)
|
||||
@@ -230,7 +231,7 @@ test_interp_pipe_unescaped :: proc(t: ^testing.T) {
|
||||
timezone: ^datetime.TZ_Region,
|
||||
}
|
||||
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)
|
||||
@@ -246,10 +247,14 @@ test_interp_pipe_dot_current :: proc(t: ^testing.T) {
|
||||
timezone: ^datetime.TZ_Region,
|
||||
}
|
||||
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)
|
||||
testing.expect_value(t, result, "[6 Jan 2026][15 Jun 2026][15 Oct 2026]")
|
||||
}
|
||||
@@ -267,7 +272,7 @@ Format_Data :: struct {
|
||||
@(test)
|
||||
test_format_typical_iso :: proc(t: ^testing.T) {
|
||||
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)
|
||||
@@ -278,7 +283,7 @@ test_format_typical_iso :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_format_short_date_only :: proc(t: ^testing.T) {
|
||||
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)
|
||||
@@ -289,7 +294,7 @@ test_format_short_date_only :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_format_empty_input_errors :: proc(t: ^testing.T) {
|
||||
data := Format_Data {
|
||||
date = "",
|
||||
date = "",
|
||||
date_format = "2 Jan 2006",
|
||||
}
|
||||
tpl, _ := parse("[{{date | format}}]", "<test>", allocator = context.temp_allocator)
|
||||
@@ -301,7 +306,7 @@ test_format_empty_input_errors :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_format_non_date_string_errors :: proc(t: ^testing.T) {
|
||||
data := Format_Data {
|
||||
date = "abc",
|
||||
date = "abc",
|
||||
date_format = "2 Jan 2006",
|
||||
}
|
||||
tpl, _ := parse("[{{date | format}}]", "<test>", allocator = context.temp_allocator)
|
||||
@@ -327,7 +332,7 @@ test_format_non_string_value_errors :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_format_invalid_month_errors :: proc(t: ^testing.T) {
|
||||
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)
|
||||
@@ -341,7 +346,7 @@ test_format_inside_section_renders :: proc(t: ^testing.T) {
|
||||
// Mirrors the datetime.html partial pattern: section pushes raw string,
|
||||
// partial uses {{.}} for ISO attr and {{. | format}} for display.
|
||||
data := Format_Data {
|
||||
date = "2025-12-25T00:00:00Z",
|
||||
date = "2025-12-25T00:00:00Z",
|
||||
date_format = "2 Jan 2006",
|
||||
}
|
||||
tpl, _ := parse(
|
||||
@@ -356,10 +361,14 @@ test_format_inside_section_renders :: proc(t: ^testing.T) {
|
||||
@(test)
|
||||
test_format_inside_section_skips_when_empty :: proc(t: ^testing.T) {
|
||||
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)
|
||||
testing.expect_value(t, result, "[]")
|
||||
}
|
||||
@@ -374,7 +383,11 @@ test_format_quoted_literal_arg :: proc(t: ^testing.T) {
|
||||
date = "2026-03-15T08:49:54-04:00",
|
||||
date_format = "2 Jan 2006",
|
||||
}
|
||||
tpl, _ := parse(`{{date | format "Jan 2, 2006"}}`, "<test>", allocator = context.temp_allocator)
|
||||
tpl, _ := parse(
|
||||
`{{date | format "Jan 2, 2006"}}`,
|
||||
"<test>",
|
||||
allocator = context.temp_allocator,
|
||||
)
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "Mar 15, 2026")
|
||||
}
|
||||
@@ -465,7 +478,7 @@ test_format_handles_all_iso8601_variants :: proc(t: ^testing.T) {
|
||||
}
|
||||
for &c in cases {
|
||||
data := Format_Data {
|
||||
date = c.input,
|
||||
date = c.input,
|
||||
date_format = "2 Jan 2006",
|
||||
}
|
||||
tpl, _ := parse("{{date | format}}", "<test>", allocator = context.temp_allocator)
|
||||
@@ -549,3 +562,24 @@ test_format_mst_no_offset_no_timezone :: proc(t: ^testing.T) {
|
||||
result, _ := render(tpl, data, {}, context.temp_allocator)
|
||||
testing.expect_value(t, result, "UTC")
|
||||
}
|
||||
|
||||
// --- pipe op suggestion tests ---
|
||||
|
||||
@(test)
|
||||
test_unknown_pipe_op_suggestion :: proc(t: ^testing.T) {
|
||||
filter := Pipe_Filter{op = "formats", op_pos = 0}
|
||||
_, err := apply_filter("2026-01-15", &filter, 0, nil)
|
||||
testing.expect(t, err != nil, "should error on unknown op")
|
||||
b := body(err)
|
||||
testing.expect(t, strings.contains(b.hint, "format"), "hint should suggest 'format'")
|
||||
testing.expect(t, strings.contains(b.hint, "did you mean"), "hint should be a suggestion")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_unknown_pipe_op_no_suggestion :: proc(t: ^testing.T) {
|
||||
filter := Pipe_Filter{op = "xyz", op_pos = 0}
|
||||
_, err := apply_filter("2026-01-15", &filter, 0, nil)
|
||||
testing.expect(t, err != nil, "should error on unknown op")
|
||||
b := body(err)
|
||||
testing.expect(t, b.hint == "", "no suggestion expected for 'xyz'")
|
||||
}
|
||||
|
||||
@@ -132,4 +132,3 @@ spec_dynamic_names :: proc(t: ^testing.T) {
|
||||
spec_inheritance :: proc(t: ^testing.T) {
|
||||
run_spec_file(t, "spec/specs/~inheritance.json")
|
||||
}
|
||||
|
||||
|
||||
+12
-9
@@ -121,9 +121,18 @@ validate_key_path :: proc(
|
||||
for i in 1 ..< part_count {
|
||||
v, info := base_value(current)
|
||||
if info == nil {
|
||||
return false, parts[i], nil
|
||||
return true, "", nil
|
||||
}
|
||||
if _, is_map := info.variant.(runtime.Type_Info_Map); is_map {
|
||||
val, found := lookup_in(current, parts[i])
|
||||
if found {
|
||||
current = val
|
||||
continue
|
||||
}
|
||||
available := collect_map_keys(current, allocator)
|
||||
if suggest_correction(available, parts[i]) != "" {
|
||||
return false, parts[i], available
|
||||
}
|
||||
return true, "", nil
|
||||
}
|
||||
if _, is_struct := info.variant.(runtime.Type_Info_Struct); is_struct {
|
||||
@@ -153,10 +162,7 @@ suggest_correction :: proc(available: []string, missing: string) -> string {
|
||||
if len(available) == 0 || len(missing) == 0 {
|
||||
return ""
|
||||
}
|
||||
threshold := 2
|
||||
if len(missing) > 8 {
|
||||
threshold = len(missing) / 4
|
||||
}
|
||||
threshold := max(2, len(missing) / 3)
|
||||
|
||||
best: string
|
||||
best_dist := threshold + 1
|
||||
@@ -187,10 +193,7 @@ collect_partial_names :: proc(
|
||||
|
||||
// collect_block_names enumerates the unique `{{$name}}` block definitions in
|
||||
// a template's node array.
|
||||
collect_block_names :: proc(
|
||||
tmpl: Template,
|
||||
allocator := context.temp_allocator,
|
||||
) -> []string {
|
||||
collect_block_names :: proc(tmpl: Template, allocator := context.temp_allocator) -> []string {
|
||||
out := make([dynamic]string, 0, 0, allocator)
|
||||
seen := make(map[string]bool, allocator)
|
||||
defer delete(seen)
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
#+feature dynamic-literals
|
||||
package mustache
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:fmt"
|
||||
import "core:testing"
|
||||
|
||||
@@ -10,11 +11,15 @@ Inner :: struct {
|
||||
bar: int,
|
||||
}
|
||||
|
||||
Page :: struct {
|
||||
title: string,
|
||||
}
|
||||
|
||||
Outer :: struct {
|
||||
title: string,
|
||||
page_title: string,
|
||||
inner: Inner,
|
||||
numbers: [3]int,
|
||||
title: string,
|
||||
page: Page,
|
||||
inner: Inner,
|
||||
numbers: [3]int,
|
||||
}
|
||||
|
||||
@(test)
|
||||
@@ -150,8 +155,8 @@ test_validate_map_path_silent :: proc(t: ^testing.T) {
|
||||
|
||||
@(test)
|
||||
test_suggest_correction_exact :: proc(t: ^testing.T) {
|
||||
available := []string{"title", "page_title", "body"}
|
||||
testing.expect_value(t, suggest_correction(available, "page_titel"), "page_title")
|
||||
available := []string{"title", "page.title", "body"}
|
||||
testing.expect_value(t, suggest_correction(available, "page_titel"), "page.title")
|
||||
}
|
||||
|
||||
@(test)
|
||||
@@ -193,3 +198,67 @@ test_warn_no_false_positive_for_valid_keys :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, ok, true)
|
||||
}
|
||||
|
||||
// --- json.Value map crossing tests ---
|
||||
|
||||
JSON_Params_Context :: struct {
|
||||
params: json.Value,
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_validate_key_path_crosses_json_value_map :: proc(t: ^testing.T) {
|
||||
params_map := make(json.Object, context.temp_allocator)
|
||||
params_map["author"] = json.String("Tester")
|
||||
|
||||
data := JSON_Params_Context { params = params_map }
|
||||
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
|
||||
append(&ctx, data)
|
||||
|
||||
ok, missing, _ := validate_key_path(ctx[:], "params.starred")
|
||||
testing.expect(t, ok, "path crossing json.Value map should be ok")
|
||||
testing.expect(t, missing == "", "no missing segment for map crossing")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_validate_key_path_json_value_map_existing_key :: proc(t: ^testing.T) {
|
||||
params_map := make(json.Object, context.temp_allocator)
|
||||
params_map["author"] = json.String("Tester")
|
||||
|
||||
data := JSON_Params_Context { params = params_map }
|
||||
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
|
||||
append(&ctx, data)
|
||||
|
||||
ok, missing, _ := validate_key_path(ctx[:], "params.author")
|
||||
testing.expect(t, ok, "existing key in json.Value map should be ok")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_validate_key_path_map_typo :: proc(t: ^testing.T) {
|
||||
params_map := make(json.Object, context.temp_allocator)
|
||||
params_map["author"] = json.String("Tester")
|
||||
params_map["social"] = json.String("")
|
||||
|
||||
data := JSON_Params_Context { params = params_map }
|
||||
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
|
||||
append(&ctx, data)
|
||||
|
||||
ok, missing, available := validate_key_path(ctx[:], "params.authr")
|
||||
testing.expect(t, !ok, "typo of map key should not be ok")
|
||||
testing.expect_value(t, missing, "authr")
|
||||
suggestion := suggest_correction(available, "authr")
|
||||
testing.expect_value(t, suggestion, "author")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_validate_key_path_map_no_close_match :: proc(t: ^testing.T) {
|
||||
params_map := make(json.Object, context.temp_allocator)
|
||||
params_map["author"] = json.String("Tester")
|
||||
params_map["social"] = json.String("")
|
||||
|
||||
data := JSON_Params_Context { params = params_map }
|
||||
ctx := make([dynamic]any, 0, 1, context.temp_allocator)
|
||||
append(&ctx, data)
|
||||
|
||||
ok, missing, _ := validate_key_path(ctx[:], "params.xyz")
|
||||
testing.expect(t, ok, "unknown key with no close match should be ok (suppressed)")
|
||||
testing.expect(t, missing == "", "no missing segment when suppressed")
|
||||
}
|
||||
|
||||
+2
-12
@@ -104,11 +104,7 @@ tokenize :: proc(
|
||||
|
||||
close_idx := strings.index(src[key_start:], "}}")
|
||||
if close_idx < 0 {
|
||||
return tokens, Error_Body {
|
||||
msg = "unclosed tag '{{'",
|
||||
pos = tag_pos,
|
||||
kind = .Syntax,
|
||||
}
|
||||
return tokens, Error_Body{msg = "unclosed tag '{{'", pos = tag_pos, kind = .Syntax}
|
||||
}
|
||||
close := key_start + close_idx
|
||||
|
||||
@@ -125,12 +121,7 @@ tokenize :: proc(
|
||||
}
|
||||
append(
|
||||
&tokens,
|
||||
Token {
|
||||
kind = .Partial,
|
||||
value = trimmed,
|
||||
is_dynamic = is_dyn,
|
||||
pos = tag_pos,
|
||||
},
|
||||
Token{kind = .Partial, value = trimmed, is_dynamic = is_dyn, pos = tag_pos},
|
||||
)
|
||||
} else {
|
||||
append(
|
||||
@@ -299,4 +290,3 @@ should_trim_whitespace :: proc(kind: Token_Kind) -> bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
@@ -115,4 +115,3 @@ og_for_page :: proc(site_og: Open_Graph, page: Page) -> Open_Graph {
|
||||
|
||||
return og
|
||||
}
|
||||
|
||||
|
||||
+96
-66
@@ -11,43 +11,21 @@ import "core:time"
|
||||
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,
|
||||
og: Open_Graph,
|
||||
params: json.Value,
|
||||
site: Site_Context,
|
||||
menus: map[string][]Menu_Entry,
|
||||
page: Page,
|
||||
|
||||
// Home data
|
||||
pages: [dynamic]Page_Context,
|
||||
pages: [dynamic]Page,
|
||||
|
||||
// Section Data
|
||||
// TODO: Remove "posts" from the Odin code
|
||||
posts: [dynamic]Page_Context,
|
||||
}
|
||||
|
||||
Page_Context :: struct {
|
||||
permalink: string,
|
||||
title: string,
|
||||
starred: bool,
|
||||
date: string,
|
||||
year: string,
|
||||
}
|
||||
|
||||
build_page_context :: proc(page: Page) -> Page_Context {
|
||||
return Page_Context {
|
||||
permalink = page.permalink,
|
||||
title = page.title,
|
||||
starred = page.is_starred,
|
||||
date = page.date,
|
||||
year = get_year(page.date),
|
||||
}
|
||||
posts: [dynamic]Page,
|
||||
}
|
||||
|
||||
load_template :: proc(vfs: ^VFS, virtual_path: string) -> mustache.Template {
|
||||
@@ -113,27 +91,64 @@ get_template :: proc(
|
||||
return mustache.Template{}
|
||||
}
|
||||
|
||||
capitalize :: proc(s: string) -> string {
|
||||
to_title_case :: proc(s: string, allocator := context.allocator) -> string {
|
||||
if len(s) == 0 {
|
||||
return s
|
||||
}
|
||||
if s[0] >= 'a' && s[0] <= 'z' {
|
||||
return fmt.aprintf("%c%s", s[0] - 32, s[1:])
|
||||
|
||||
out := transmute([]byte)strings.clone(s, allocator)
|
||||
|
||||
capitalize_next := true
|
||||
for char, i in s {
|
||||
switch char {
|
||||
case '-', '_':
|
||||
out[i] = ' '
|
||||
fallthrough
|
||||
case ' ':
|
||||
capitalize_next = true
|
||||
case 'a' ..= 'z':
|
||||
if capitalize_next {
|
||||
out[i] = u8(char) - 32
|
||||
}
|
||||
fallthrough
|
||||
case:
|
||||
capitalize_next = false
|
||||
}
|
||||
}
|
||||
return s
|
||||
return string(out)
|
||||
}
|
||||
|
||||
merge_params :: proc(site, page: json.Value) -> json.Value {
|
||||
if page == nil do return site
|
||||
if site == nil do return page
|
||||
site_obj, ok1 := site.(json.Object)
|
||||
page_obj, ok2 := page.(json.Object)
|
||||
if !ok1 do return page
|
||||
if !ok2 do return site
|
||||
merged := make(json.Object, len(site_obj) + len(page_obj), context.temp_allocator)
|
||||
for k, v in site_obj {merged[k] = v}
|
||||
for k, v in page_obj {merged[k] = v}
|
||||
return merged
|
||||
}
|
||||
|
||||
render_template :: proc(
|
||||
content_tpl: mustache.Template,
|
||||
data: Template_Context,
|
||||
ctx: Template_Context,
|
||||
partials: map[string]mustache.Template,
|
||||
reported_errors: ^map[string]bool,
|
||||
) -> string {
|
||||
result, err := mustache.render(content_tpl, data, partials)
|
||||
result, err := mustache.render(content_tpl, []any{ctx.site, ctx.page, ctx}, partials)
|
||||
if err != nil {
|
||||
log.errorf(
|
||||
"%s",
|
||||
mustache.format_render_error(err, content_tpl, colorize = mustache.should_colorize()),
|
||||
formatted := mustache.format_render_error(
|
||||
err,
|
||||
content_tpl,
|
||||
colorize = mustache.should_colorize(),
|
||||
)
|
||||
if formatted in reported_errors^ {
|
||||
return ""
|
||||
}
|
||||
reported_errors[formatted] = true
|
||||
log.errorf("%s", formatted)
|
||||
return ""
|
||||
}
|
||||
return result
|
||||
@@ -142,7 +157,7 @@ render_template :: proc(
|
||||
render_site :: proc(site: ^Site) {
|
||||
allocator := site_allocator(site)
|
||||
pages := site.pages[:]
|
||||
sort_pages_by_date(pages)
|
||||
sort_pages(pages)
|
||||
|
||||
// Load shared resources
|
||||
partials := load_partials(&site.vfs)
|
||||
@@ -151,15 +166,17 @@ render_site :: proc(site: ^Site) {
|
||||
template_cache: map[string]mustache.Template
|
||||
defer delete(template_cache)
|
||||
|
||||
errors := make(map[string]bool, context.temp_allocator)
|
||||
|
||||
offset, ok := mustache.compute_utc_offset(site.tz)
|
||||
assert(ok)
|
||||
now, ok2 := time.time_to_rfc3339(time.now(), offset, false, allocator)
|
||||
assert(ok2)
|
||||
|
||||
ctx := Template_Context {
|
||||
site = site.site_context,
|
||||
menus = site.menus,
|
||||
now = now,
|
||||
params = site.params,
|
||||
description = site.description,
|
||||
og = site.og,
|
||||
date_format = site.date.format,
|
||||
timezone = site.tz,
|
||||
@@ -175,6 +192,7 @@ render_site :: proc(site: ^Site) {
|
||||
break
|
||||
}
|
||||
}
|
||||
ctx.page = home
|
||||
|
||||
// Collect sections
|
||||
sections := make(map[string]bool)
|
||||
@@ -191,7 +209,7 @@ render_site :: proc(site: ^Site) {
|
||||
continue
|
||||
}
|
||||
tpl := get_template(&site.vfs, page.layout, &template_cache)
|
||||
html := render_page_html(page, site, tpl, partials, ctx)
|
||||
html := render_page_html(page, site, tpl, partials, ctx, &errors)
|
||||
if .Minify in site.features {
|
||||
html = minify_html(html)
|
||||
}
|
||||
@@ -220,6 +238,7 @@ render_site :: proc(site: ^Site) {
|
||||
section_tpl,
|
||||
partials,
|
||||
ctx,
|
||||
&errors,
|
||||
)
|
||||
if .Minify in site.features {
|
||||
html = minify_html(html)
|
||||
@@ -230,7 +249,7 @@ render_site :: proc(site: ^Site) {
|
||||
// Render home page
|
||||
if has_home {
|
||||
home_tpl := get_template(&site.vfs, "home", &template_cache)
|
||||
home_html := render_home_html(home, site, home_tpl, partials, ctx)
|
||||
home_html := render_home_html(home, site, home_tpl, partials, ctx, &errors)
|
||||
if .Minify in site.features {
|
||||
home_html = minify_html(home_html)
|
||||
}
|
||||
@@ -265,14 +284,13 @@ render_page_html :: proc(
|
||||
content_tpl: mustache.Template,
|
||||
partials: map[string]mustache.Template,
|
||||
ctx: Template_Context,
|
||||
seen: ^map[string]bool,
|
||||
) -> string {
|
||||
ctx := ctx
|
||||
ctx.title = fmt.tprintf("%s | %s", page.title, site.title)
|
||||
ctx.page_title = page.title
|
||||
ctx.content = page.content
|
||||
ctx.date = page.date
|
||||
ctx.page = page
|
||||
ctx.og = og_for_page(site.og, page)
|
||||
return render_template(content_tpl, ctx, partials)
|
||||
ctx.params = merge_params(site.params, page.params)
|
||||
return render_template(content_tpl, ctx, partials, seen)
|
||||
}
|
||||
|
||||
render_home_html :: proc(
|
||||
@@ -281,23 +299,22 @@ render_home_html :: proc(
|
||||
content_tpl: mustache.Template,
|
||||
partials: map[string]mustache.Template,
|
||||
ctx: Template_Context,
|
||||
seen: ^map[string]bool,
|
||||
) -> string {
|
||||
list_pages := make([dynamic]Page_Context)
|
||||
defer delete(list_pages)
|
||||
list_pages := make([dynamic]Page, 0, 8, context.temp_allocator)
|
||||
for page in site.pages {
|
||||
if page._is_index {
|
||||
continue
|
||||
}
|
||||
append(&list_pages, build_page_context(page))
|
||||
append(&list_pages, page)
|
||||
}
|
||||
|
||||
ctx := ctx
|
||||
ctx.title = site.title
|
||||
ctx.content = home.content
|
||||
ctx.pages = list_pages
|
||||
ctx.og = og_for_page(site.og, home)
|
||||
ctx.params = merge_params(site.params, home.params)
|
||||
|
||||
return render_template(content_tpl, ctx, partials)
|
||||
return render_template(content_tpl, ctx, partials, seen)
|
||||
}
|
||||
|
||||
render_section :: proc(
|
||||
@@ -308,33 +325,35 @@ render_section :: proc(
|
||||
content_tpl: mustache.Template,
|
||||
partials: map[string]mustache.Template,
|
||||
ctx: Template_Context,
|
||||
seen: ^map[string]bool,
|
||||
) -> string {
|
||||
posts := make([dynamic]Page_Context)
|
||||
defer delete(posts)
|
||||
alloc := site_allocator(site)
|
||||
posts := make([dynamic]Page, 0, len(site.pages) / 2, context.temp_allocator)
|
||||
for page in site.pages {
|
||||
if page.section != section || page._is_index {
|
||||
continue
|
||||
}
|
||||
append(&posts, build_page_context(page))
|
||||
append(&posts, page)
|
||||
}
|
||||
|
||||
ctx := ctx
|
||||
if has_index {
|
||||
ctx.content = section_index.content
|
||||
ctx.page_title = section_index.title
|
||||
ctx.title = fmt.tprintf("%s | %s", section_index.title, site.title)
|
||||
ctx.page = section_index
|
||||
ctx.og = og_for_page(site.og, section_index)
|
||||
} else {
|
||||
ctx.page_title = capitalize(section)
|
||||
ctx.title = fmt.tprintf("%s | %s", capitalize(section), site.title)
|
||||
ctx.og.title = capitalize(section)
|
||||
title := to_title_case(section, alloc)
|
||||
ctx.page = Page {
|
||||
title = title,
|
||||
}
|
||||
ctx.og.title = title
|
||||
ctx.og.description = ""
|
||||
ctx.og.url = fmt.tprintf("%s/%s/", site.base_url, section)
|
||||
ctx.og.type = "website"
|
||||
ctx.og.is_article = false
|
||||
}
|
||||
ctx.posts = posts
|
||||
return render_template(content_tpl, ctx, partials)
|
||||
ctx.params = merge_params(site.params, ctx.page.params)
|
||||
return render_template(content_tpl, ctx, partials, seen)
|
||||
}
|
||||
|
||||
load_partials :: proc(vfs: ^VFS) -> map[string]mustache.Template {
|
||||
@@ -380,11 +399,12 @@ get_year :: proc(iso: string) -> string {
|
||||
return iso[:4]
|
||||
}
|
||||
|
||||
sort_pages_by_date :: proc(pages: #soa[]Page) {
|
||||
// Weight primary (ascending). Date secondary (descending) for equal weights.
|
||||
sort_pages :: proc(pages: #soa[]Page) {
|
||||
for i in 1 ..< len(pages) {
|
||||
key := pages[i]
|
||||
j := i - 1
|
||||
for j >= 0 && pages.date[j] < key.date {
|
||||
for j >= 0 && compare_pages(pages, j, key) > 0 {
|
||||
pages[j + 1] = pages[j]
|
||||
j -= 1
|
||||
}
|
||||
@@ -392,6 +412,16 @@ sort_pages_by_date :: proc(pages: #soa[]Page) {
|
||||
}
|
||||
}
|
||||
|
||||
compare_pages :: proc(pages: #soa[]Page, j: int, key: Page) -> int {
|
||||
wj := pages.weight[j].? or_else DEFAULT_WEIGHT
|
||||
wk := key.weight.? or_else DEFAULT_WEIGHT
|
||||
if wj != wk do return wj - wk
|
||||
// Equal weight → date descending
|
||||
if pages.date[j] < key.date do return 1
|
||||
if pages.date[j] > key.date do return -1
|
||||
return 0
|
||||
}
|
||||
|
||||
write_page :: proc(output_dir: string, permalink: string, html: string) {
|
||||
rel := permalink
|
||||
if len(rel) > 0 && rel[0] == '/' {
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
package main
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:flags"
|
||||
import "core:fmt"
|
||||
import "core:log"
|
||||
import "core:mem"
|
||||
@@ -13,24 +12,32 @@ import "core:time/timezone"
|
||||
import md "markdown"
|
||||
|
||||
|
||||
// Site is the primary workhorse.
|
||||
// Site_Context holds the site date that is accessible in templates.
|
||||
Site_Context :: struct {
|
||||
title: string,
|
||||
description: string,
|
||||
base_url: string,
|
||||
params: json.Object,
|
||||
og: Open_Graph,
|
||||
menus: map[string][]Menu_Entry,
|
||||
}
|
||||
|
||||
// Site is the primary workhorse, containing everything needed to build the site,
|
||||
// including an arena allocator, the pages, and all the various directories and
|
||||
// enabled features.
|
||||
Site :: struct {
|
||||
using site_context: Site_Context,
|
||||
arena: mem.Dynamic_Arena,
|
||||
pages: #soa[dynamic]Page,
|
||||
modules: [dynamic]string,
|
||||
vfs: VFS,
|
||||
title: string,
|
||||
description: string,
|
||||
base_url: string,
|
||||
config_path: string,
|
||||
content_dir: string,
|
||||
assets_dir: string,
|
||||
output_dir: string,
|
||||
layouts_dir: string,
|
||||
params: json.Object,
|
||||
features: bit_set[Feature],
|
||||
markdown_extensions: bit_set[md.Extension],
|
||||
og: Open_Graph,
|
||||
date: Date_Preferences,
|
||||
tz: ^datetime.TZ_Region,
|
||||
grammars: string,
|
||||
@@ -61,6 +68,7 @@ Config_File :: struct {
|
||||
markdown_extensions: json.Value,
|
||||
params: json.Value,
|
||||
modules: json.Value,
|
||||
menus: json.Value,
|
||||
og: Open_Graph,
|
||||
date: Date_Preferences,
|
||||
grammars: string,
|
||||
@@ -79,11 +87,13 @@ Flags :: struct {
|
||||
drafts: bool `args:"name=drafts" usage:"Include draft pages in the build"`,
|
||||
watch: bool `usage:"Rebuild on file changes (polls every 5 seconds)"`,
|
||||
minify: bool `args:"name=minify" usage:"Minify HTML output and CSS assets"`,
|
||||
verbose: bool `usage:"Enable debug logging"`,
|
||||
quiet: bool `usage:"Suppress info logging (warnings and errors only)"`,
|
||||
md_enable: string `args:"name=ext" usage:"Enable markdown extensions (comma-separated: emoji,sidenotes,alerts,highlight,sections)"`,
|
||||
md_disable: string `args:"name=no-ext" usage:"Disable markdown extensions (comma-separated: emoji,sidenotes,alerts,highlight,sections)"`,
|
||||
}
|
||||
|
||||
init_site :: proc(site: ^Site, args: []string) {
|
||||
init_site :: proc(site: ^Site, flags: Flags) {
|
||||
mem.dynamic_arena_init(&site.arena)
|
||||
alloc := site_allocator(site)
|
||||
|
||||
@@ -91,10 +101,7 @@ init_site :: proc(site: ^Site, args: []string) {
|
||||
site.base_url = "http://localhost:8080"
|
||||
site.markdown_extensions = md.DEFAULT_EXTENSIONS
|
||||
|
||||
_flags: Flags
|
||||
flags.parse_or_exit(&_flags, args, .Odin, alloc)
|
||||
|
||||
path := _flags.config_path
|
||||
path := flags.config_path
|
||||
if path == "" {
|
||||
found, ok := find_config("thor.json")
|
||||
if ok {
|
||||
@@ -119,7 +126,7 @@ init_site :: proc(site: ^Site, args: []string) {
|
||||
site_apply_path_defaults(site, config_dir)
|
||||
}
|
||||
|
||||
site_apply_cli_flags(site, _flags)
|
||||
site_apply_cli_flags(site, flags)
|
||||
site.config_path = path
|
||||
|
||||
site.og = og_for_site(site)
|
||||
@@ -193,6 +200,15 @@ site_apply_config :: proc(site: ^Site, config: Config_File, config_dir: string)
|
||||
site.og = config.og
|
||||
site.date = config.date
|
||||
|
||||
// Parse config menus if present (nil = absent, non-nil = present)
|
||||
if config.menus != nil {
|
||||
site.menus = parse_config_menus(config.menus, site_allocator(site))
|
||||
if site.menus == nil {
|
||||
// Present but empty ({}) — explicit opt-out
|
||||
site.menus = make(map[string][]Menu_Entry, site_allocator(site))
|
||||
}
|
||||
}
|
||||
|
||||
site.grammars = expand_path(config.grammars, site_allocator(site))
|
||||
site.queries = expand_path(config.queries, site_allocator(site))
|
||||
}
|
||||
@@ -227,6 +243,7 @@ site_apply_cli_flags :: proc(site: ^Site, flags: Flags) {
|
||||
|
||||
site.markdown_extensions += md.parse_extension_list(flags.md_enable)
|
||||
site.markdown_extensions -= md.parse_extension_list(flags.md_disable)
|
||||
md.resolve_extension_conflicts(&site.markdown_extensions)
|
||||
}
|
||||
|
||||
site_allocator :: proc(site: ^Site) -> mem.Allocator {
|
||||
@@ -255,4 +272,3 @@ find_config :: proc(filename: string) -> (path: string, ok: bool) {
|
||||
dir = dir[:idx]
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
public
|
||||
@@ -0,0 +1,24 @@
|
||||
# Docs Site AGENTS.md
|
||||
|
||||
## DO NOT EDIT — BY HUMANS, FOR HUMANS
|
||||
|
||||
All content under `thor/site/` is handwritten by humans. No AI, agent,
|
||||
bot, or assistant may edit, rewrite, or generate any file here. AI may
|
||||
be consulted as a sanity check, but the prose stays human.
|
||||
|
||||
## Implicit limits
|
||||
|
||||
The mustache engine imposes limits that template authors should be aware of.
|
||||
These values are defined in `thor/mustache/pipes.odin`, and `thor/mustache/mustache.odin`
|
||||
and must be kept in sync with the source code if they ever change.
|
||||
|
||||
- **Nested partials** — deeply nested partial chains (partial-in-partial)
|
||||
consume context stack frames during rendering. The limit is defined by
|
||||
`MAX_CONTEXT_STACK` (16) in the mustache engine. In practice, 3–4 levels of
|
||||
nesting is typical and safe.
|
||||
|
||||
- **`MAX_PIPES` (8)** — a single tag may chain up to 8 pipe filters:
|
||||
`{{key | op1 | op2 | ... | op8}}`. Exceeding this is a parse error.
|
||||
|
||||
- **`MAX_PIPE_ARGS` (2)** — each pipe filter accepts at most 2 arguments:
|
||||
`{{key | op arg1 arg2}}`.
|
||||
@@ -0,0 +1,38 @@
|
||||
[TOC]
|
||||
|
||||
Thor is a simple Static Sire Generator designed for personal blogs and other small websites.
|
||||
|
||||
Its core principals are simplicity and minimal configuration, so you can get started as quickly as possible.
|
||||
|
||||
It is based on Hugo, and gingerbill's SSG. Templating is done with (extended?) Mustache templates.
|
||||
|
||||
## What it does
|
||||
|
||||
- Syntax Highlighting server-side (with Tree Sitter) or client-side with highlight.js
|
||||
- Mustache Templating
|
||||
- OpenGraph Tags
|
||||
- Menus (WIP)
|
||||
- Extended Markdown ([See below](#extended-markdown))
|
||||
- Basic (whitespace) minification.
|
||||
|
||||
## What it doesn't do
|
||||
- Internationalization
|
||||
- Pagination (Yet)
|
||||
- Themes
|
||||
- Union File System (Yet)
|
||||
- Image Manipulation
|
||||
- TailwindCSS integration
|
||||
|
||||
## Getting Started
|
||||
|
||||
Run `thor`
|
||||
Check out `public/index.html`
|
||||
|
||||
Then follow [The Guide]()
|
||||
For a more complete setup, run `thor new site`.
|
||||
|
||||
## Extended Markdown
|
||||
- Emoji expansion
|
||||
- margin style footnotes
|
||||
- Guthub style alerts
|
||||
- [and more]
|
||||
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"title": "AI",
|
||||
"date": "2026-07-28T12:16:00-04:00"
|
||||
}
|
||||
## Prose
|
||||
|
||||
This site, and all the documentation on it was written for humans, by humans.
|
||||
AI gets consulted as a sanity check, i.e. "Did I miss anything here?", "Does the documentation match the code's behaviour?", etc. All prose is hand-written by a human being.
|
||||
|
||||
## Code
|
||||
|
||||
The code is a completely different story. The majority of code is written with GLM-4.2 via OpenCode under programmer direction. I focus on architectural decisions, and I read the code emitted by the AI with varying degrees of scrutiny based on whether it touches a major part of the architecture, is in a hotpath, or holds some other high level of significance.
|
||||
@@ -0,0 +1,384 @@
|
||||
{
|
||||
"title": "Docs",
|
||||
"date": "2026-07-22T08:54:00-04:00",
|
||||
"toc": true
|
||||
}
|
||||
|
||||
## Introduction
|
||||
|
||||
This guide assumes you have either read [The Guide](../guide), or have built a [Hugo](https://gohugo.io) site before. It also assumes you have a basic knowledge of HTML and CSS.
|
||||
|
||||
## Features
|
||||
|
||||
Thor has many features and content processors available. In an effort to provide the best out of the box experience, most of them are enabled by default.
|
||||
|
||||
### Opt-In Features
|
||||
|
||||
TODO: #### deflist syntax
|
||||
|
||||
TODO: #### footnotes
|
||||
|
||||
#### Minify
|
||||
|
||||
If enabled, `minify` will perform simple whitespace removal on all your output `.html` files, and any `.css` files in your `assets` directories. Minifying JavaScript is not supported (yet).
|
||||
|
||||
TODO: Do we minify inline css?
|
||||
|
||||
### Opt-Out Features
|
||||
|
||||
- emoji
|
||||
- sidenotes/marginnotes
|
||||
- syntax highlighting
|
||||
- heading ids
|
||||
- TODO: Table Of Contents Generation
|
||||
- [GitHub style alerts](https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax#alerts)
|
||||
|
||||
|
||||
## Directories
|
||||
Like Hugo, a Thor project is a collection of specially named directories, plus an optional config file. All directories are optional, but it is recommended to at least have a `content` directory.
|
||||
|
||||
content
|
||||
|
||||
: `content` holds your pages and page bundles. See [Content](#content). If not found, thor will look for content files in the root of your current working directory.
|
||||
|
||||
assets
|
||||
: `assets` contains any static files for your site (favicon.ico, etc.), as well as files you want to send through the asset pipeline (CSS or JS files).
|
||||
|
||||
layouts
|
||||
|
||||
: `layouts` holds your templates and partials. See [Templates](#templates).
|
||||
|
||||
public
|
||||
: `public` will contain your completed site.
|
||||
|
||||
All of these names can be remapped in `thor.json`.[^remap]
|
||||
|
||||
[^remap]: While directories can be remapped at the site level, modules must (currently) adhere to the defaults.
|
||||
|
||||
### Assets
|
||||
|
||||
Currently, there is only one asset processor, and that is [the minifier](#minify), though more are planned (i.e. Image processing).
|
||||
|
||||
TODO: Expand
|
||||
|
||||
## Content
|
||||
### Pages & Page Bundles
|
||||
|
||||
Page content can either be defined in a single file (`contact.md`), or in a directory (`contact/index.md` + `contact/our-team.jpg`). Single file pages are preferred to page bundles.[^1]
|
||||
|
||||
[^1]: That's not you say you shouldn't use bundles, but if you have no additional resources on your page, there's no benefit to using a bundle.
|
||||
|
||||
Thor currently supports 2 formats for page files: MarkDown (`.md`), and HTML (`.html`).
|
||||
|
||||
TODO: Content
|
||||
|
||||
### Frontmatter
|
||||
|
||||
TODO: Frontmatter
|
||||
|
||||
## Menus
|
||||
|
||||
TODO: Describe
|
||||
|
||||
TODO: Don't forget to highlight differences from Hugo.
|
||||
|
||||
## Templates
|
||||
|
||||
Sites are built using one or more template files written in an extended version of [mustache](https://mustache.github.io) templates. The [mustache manual](https://mustache.github.io/mustache.5.html) has great explainations and a lot of examples if you want to know more, but I'll summarize them for you here.
|
||||
|
||||
The beauty of Mustache is that there is very little syntax; there are just 10 symbols you need learn: `{{`, `{{&`, `{{^`, `{{>`, `{{<`, `{{#`, `/}}`, `{{$`, `{{!`, and `|`.
|
||||
|
||||
TODO: ^^ Badly worded sentence ^^
|
||||
|
||||
TODOS: Gotta describe base templates somewhere. (the same way we describe the partials)
|
||||
|
||||
### Tags
|
||||
|
||||
#### Variables
|
||||
|
||||
In order to display a scalar (not-list) value in your template, simply wrap it in double curly braces. e.g. `{{ page.title }}`.
|
||||
|
||||
This content will be HTML escaped (for safety), so if the value you're rendering contains HTML, you'll need to use the raw syntex instead `{{& page.title}}`[^raw] which will output the value without stripping or re-writing content.
|
||||
|
||||
[^raw]: Official Triple brace syntax (`{{{ raw }}}`) is also supported, but `{{& raw }}` is preferred, as it's easy to accidentily insert too many braces.
|
||||
|
||||
In most[^most] cases, invalid keys will be silently ignored (nothing between the braces will appear), in keeping with the official mustache spec.
|
||||
|
||||
[^most]: TODO: in what cases won't it? spelllcheck + strict mode?
|
||||
|
||||
Leading and trailing whitespace(s) are ignored by the parser, so the folllowing are all equivilent: `{{& title }}`, `{{&title}}`, `{{& title}}`.
|
||||
|
||||
#### Sections
|
||||
TODO:
|
||||
|
||||
#### Inverted Sections
|
||||
TODO:
|
||||
|
||||
#### Partials
|
||||
TODO:
|
||||
|
||||
TODO: Talk about MAX_CONTEXT_DEPTH (currently 16) and how it limits the total number of nested templates to 13. (3 for `[site, page, ctx]`)
|
||||
|
||||
##### Dynamic Partials
|
||||
|
||||
#### Blocks
|
||||
TODO:
|
||||
|
||||
#### Parents
|
||||
TODO:
|
||||
|
||||
#### Summary
|
||||
|
||||
`{{page.title}}` for normal values
|
||||
`{{&page.title}}` for values that contain HTML.
|
||||
`<ul>{{#pages}}<li>{{title}}</li>{{/pages}}</ul>` for list values.
|
||||
`{{#params.is_starred}}<i class="fas fa-star"></i>{{/params.is_starred}}` for conditional content.
|
||||
`{{^pages}}No pages yet!{{/pages}}` to draw content when the value is empty.
|
||||
|
||||
### Section Names
|
||||
|
||||
When building your own templates, you are of course free to pick whatever names you choose for your partials and content slots. However, sticking to conventions helps create consistency in the ecosystem, and reduces friction when relying on built-in templates.
|
||||
|
||||
`{{$main}}...{{/main}}`
|
||||
|
||||
: the `main` section should be used in templates to denote the part of the page that is unique to that page. For the most part, your templates should look like this
|
||||
|
||||
```mustache
|
||||
{{<base}}
|
||||
{{$main}}
|
||||
<main>
|
||||
<h1>Actual page content goes here</h1>
|
||||
{{&page.content}}
|
||||
</main>
|
||||
{{/main}}
|
||||
{{/base}}
|
||||
|
||||
```
|
||||
|
||||
### Context
|
||||
|
||||
When building your page(s), the following keys are accessible to your template files:
|
||||
|
||||
`now`
|
||||
|
||||
: The Current `DateTime`. see [DateTime](#datetimes)
|
||||
|
||||
`date_format`
|
||||
|
||||
: The default format to use for dates. Configured in `thor.json:date.format`.
|
||||
|
||||
`timezone`
|
||||
|
||||
: The timezone to convert dates to when using `{{ date | format }}`. Configured in `thor.json:date.timezone`.
|
||||
|
||||
`site`
|
||||
|
||||
: The site parameters that are usable in templates, see [site](#site).
|
||||
|
||||
`pages`
|
||||
|
||||
: Returns all regular pages, sorted by `?`. Regular pages exclude index pages like home and section roots.
|
||||
|
||||
`posts`
|
||||
: TODO: Section groupings
|
||||
|
||||
`og`
|
||||
|
||||
: Contains the [Open Graph](https://ogp.me/) metadata for the current page.
|
||||
|
||||
`menus`
|
||||
|
||||
: The site's constructed menus. See [menus](#menus).
|
||||
#### Page
|
||||
|
||||
`page.content`
|
||||
|
||||
: The HTML rendered page content
|
||||
|
||||
TODO: write
|
||||
|
||||
#### Site
|
||||
|
||||
TODO: Write
|
||||
|
||||
#### Params
|
||||
|
||||
`site.params` and `page.params` are an escape hatch to let users inject arbetrary data into their website. The following are some conventional examples theme developers may want to use to ensure a consistent experience across themes.
|
||||
|
||||
`author`
|
||||
|
||||
: The author of the current page or site. See [schema.org](https://schema.org/author) for the recommended format.
|
||||
|
||||
`stylesheets`
|
||||
|
||||
: A list of paths to css files the users wants to include globally. These are rendered by the `{{> styles }}` partial, and can be omitted on sites that use custom templates.
|
||||
|
||||
TODO: ^^ Bad sentence? ^^
|
||||
|
||||
#### The Context Stack
|
||||
When building your page(s), each template is fed a Context[^ctx] stack that contains all the data should you need to build your page.
|
||||
|
||||
[^ctx]: (for developers) `Template_Context` is not the same as Odin's implicit `context` parameter.
|
||||
|
||||
The context stack is initialized[^init] as `[site, page, context]`, meaning if you use a key like `{{title}}` in your template, it will look up `context.title`, `page.title`, and then finally `site.title`, using the first available value it can find.
|
||||
|
||||
[^init]: TODO: Don't use programmer speak.
|
||||
|
||||
TODO: As you descend into sections/partials, new contexts are placed onto the stack, so they resolve first. (LIFO order)
|
||||
|
||||
```mustache
|
||||
{{<base}}
|
||||
{{$main}}
|
||||
<main>
|
||||
{{&page.content}}
|
||||
<!-- Is the same as -->
|
||||
{{&content}}
|
||||
<!-- Or -->
|
||||
{{$page}}{{&content}}{{/page}}
|
||||
<!-- Or -->
|
||||
{{$page.content}}{{&.}}{{/page.content}}
|
||||
</main>
|
||||
{{/main}}
|
||||
{{/base}}
|
||||
```
|
||||
|
||||
|
||||
### Filters
|
||||
|
||||
Because mustache is a logic-less language, (there are no `for` or `if` tags), Thor extends mustache to include a handful of data filters in the form of pipes. Bash users will feel right at home here.
|
||||
|
||||
`group_by <field>`
|
||||
|
||||
: Allows you to group pages by the given field. e.g.
|
||||
```mustache
|
||||
{{#pages | group_by year }}
|
||||
<section>
|
||||
<header>{{$key}} {{! The group's year}}</header>
|
||||
<ul>
|
||||
{{#items}}<li>
|
||||
{{title}} {{! The title of each of that year's pages }}
|
||||
{{/items}}</li>
|
||||
</ul>
|
||||
</section>
|
||||
{{/pages}}
|
||||
```
|
||||
|
||||
`sort_by <field> <asc|desc>`
|
||||
|
||||
: Allows you to sort pages by the given field.
|
||||
|
||||
`first <n>`
|
||||
|
||||
: Given a list, returns only the first `n` items. `n` defaults to 1. Given a string, returns the first `n` runes of the string. To help catch mistakes, `n` is required for strings.
|
||||
|
||||
`last <n>`
|
||||
|
||||
: Given a list, returns only the last `n` items. `n` defaults to 1. Given a string, returns the first `n` runes of the string. To help catch mistakes, `n` is required for strings.
|
||||
|
||||
`format "<format string>"`
|
||||
|
||||
: Used to display a date in a particular format. See [DateTimes](#datetimes). If no format is given, the default will be used.
|
||||
|
||||
#### Chaining Pipes
|
||||
|
||||
Thor allows you to chain two or more pipes, to allow complex data manipulation. However for performance and stylistic reasons, you are limited to no more than **8 pipes**[^pipes] for any given tag. If you believe you need more than 8 pipes, please [open an issue](../issues) with a **concrete example** of the problem you are facing.
|
||||
|
||||
[^pipes]: TODO: This number must be kept in-sync with `MAX_PIPES`.
|
||||
|
||||
**Examples:**
|
||||
|
||||
```mustache
|
||||
{{ pages | group_by year | first }} {{! All pages from the current year }}
|
||||
```
|
||||
|
||||
```mustache
|
||||
{{ pages | sort_by date desc | first }} {{! The latest page }}
|
||||
```
|
||||
|
||||
|
||||
### Partials
|
||||
|
||||
Partials are templates that render a portion of a page. To include a partial, the standard [mustache syntax](https://mustache.github.io/mustache.5.html#Partials) is used. All partials are resolved relative to the root partial's directory, so to include a partial at `layouts/partials/my_partial.html`, you would use `{{> my_partial}}`.
|
||||
|
||||
Users can create or override as many partials as they want; several are included for convienience:
|
||||
|
||||
`{{> title }}`
|
||||
|
||||
: The title of the current page. It should be placed inside the `<title>` tag. By default, it will appear as `{{ page.title }} | {{ site.title }}`.
|
||||
|
||||
`{{> nav }}`
|
||||
|
||||
: Nav renders the main menu for the site (`menus.main`). It should be placed inside the `<body>` tag, just before the `<main>` block.
|
||||
|
||||
`{{> home-link }}`
|
||||
|
||||
: This partial is rendered inside the home anchor in `{{> nav }}`. By default, it wil display the name of the site, or `Home` if no name is set.
|
||||
|
||||
`{{> styles }}`
|
||||
|
||||
: This partial renders any stylesheets specified either by the user (via `params.stylesheets`) or by the theme author (specified directly in the template). `params.stylesheets` is intended as an escape hatch for users that want to add CSS to their site, but don't want to customize any templates. Styles should be placed inside the `<head>` tags.
|
||||
|
||||
`{{> scripts }}`
|
||||
|
||||
: This partial renders any script tags specified either by the user (via `params.scripts`) or by the theme author (specified directly in the template). `params.scripts` is intended as an escape hatch for users that want to add javascript to their site, but don't want to customize any templates. Scripts should be placed at the end of the `<head>` block.
|
||||
|
||||
TODO: Scripts must currently be given in raw form, whereas styles are just paths/urs.
|
||||
|
||||
`{{> opengraph }}`
|
||||
|
||||
: This partial willl render the Open Graph meta tags for your page. It should be placed inside the `<head>` tag. See the [Open Graph](#open-graph) section for more details.
|
||||
|
||||
`{{> footer }}`
|
||||
|
||||
: This partial will render content on every page after the main page content. It should be placed at the end of the `<body>` tag. By default, it displays a simple copyright line with the current year and author's name (configured in `params.author.name`).
|
||||
|
||||
TODO: `styles`/`css`?
|
||||
|
||||
TODO: `comments`?
|
||||
|
||||
TODO: `toc`?
|
||||
|
||||
TODO: We keep referring to blocks and tags, should probably use consistent language.
|
||||
|
||||
### DateTimes
|
||||
|
||||
TODO: Should this be a subsection of a "Data Types" section?
|
||||
|
||||
Dates are strings in one of the following formats:
|
||||
|
||||
| Format | Time zone |
|
||||
| --------------------------- | ---------------------------- |
|
||||
| `2023-10-15T13:18:50-07:00` | `America/Los_Angeles` |
|
||||
| `2023-10-15T13:18:50-0700` | `America/Los_Angeles` |
|
||||
| `2023-10-15T13:18:50Z` | `Etc/UTC` |
|
||||
| `2023-10-15T13:18:50` | Default is local system time |
|
||||
| `2023-10-15` | Default is local system time |
|
||||
| `15 Oct 2023` | Default is local system time |
|
||||
|
||||
If you want to display a date in a different format, you can use the `| format` Filter. With no argument, it will default to formatting the date with `site.date_format`.
|
||||
|
||||
## Open Graph
|
||||
|
||||
TODO: default Template support
|
||||
TODO: Template override support
|
||||
TODO: how to overwrite defaults (in page frontmatter / thor.json)
|
||||
|
||||
## Zen
|
||||
|
||||
> The guding principals behind Thor.
|
||||
|
||||
Simpler is Better
|
||||
|
||||
: We are [Grug](https://grugbrain.dev/) developers. We do everything the "dumb" way first, then optimize the **measured** bottlenecks.
|
||||
|
||||
Zero is Beautiful
|
||||
|
||||
: The less configuration needed, the better. A site can be built from multiple modules or a single `index.md`.
|
||||
|
||||
On By Default
|
||||
|
||||
: Users shouldn't have to enable features, unless those features significantly impact performance or mangle regular content.
|
||||
|
||||
Fault Tolerant
|
||||
|
||||
: When possible, mistakes should be recovered with warnings. When fatal errors occur, the user should have all the information they need to fix it quickly.
|
||||
@@ -0,0 +1,28 @@
|
||||
{
|
||||
"title": "Guide",
|
||||
"date": "2026-07-12T08:55:00-04:00"
|
||||
}
|
||||
[TOC]
|
||||
|
||||
Thank you for choosing thor for your site building needs. This guide attempts to make it as easy as possible to get started.
|
||||
|
||||
> [!NOTE]: This guide assumes you have a basic knowledge of using command line interfaces, file systems, and text editors.
|
||||
|
||||
|
||||
Start by creating a new directory called `my-site`. This will be the home of your first Thor site.
|
||||
|
||||
If you haven't already, running `thor` in your new directory should create this guide in `./public/index.html`.
|
||||
|
||||
At the heart of any good SSG is the content files. Thor supports markdown (`.md`) and `.html` files.
|
||||
|
||||
Let's make a home page - copy the following text and place it into `./index.md`
|
||||
|
||||
```md
|
||||
# Hello, World!
|
||||
|
||||
Welcome to your new site.
|
||||
```
|
||||
|
||||
Run `thor` again, and you should see new content in `public/index.html`.
|
||||
|
||||
|
||||
@@ -0,0 +1,4 @@
|
||||
{
|
||||
"title": "Home"
|
||||
}
|
||||
TODO: Add content here
|
||||
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"markdown_extensions": {
|
||||
// "footnotes": true
|
||||
},
|
||||
"params": {
|
||||
"author": {
|
||||
"name": "Spencer Brower"
|
||||
}
|
||||
}
|
||||
}
|
||||
+130
-15
@@ -3,10 +3,13 @@
|
||||
package main
|
||||
|
||||
import "core:encoding/json"
|
||||
import "core:flags"
|
||||
import "core:fmt"
|
||||
import "core:log"
|
||||
import "core:os"
|
||||
import "core:testing"
|
||||
import "core:time/datetime"
|
||||
import "core:time/timezone"
|
||||
|
||||
write_temp_config :: proc(name: string, content: string) -> string {
|
||||
path := fmt.tprintf("./test_thor_%s.json", name)
|
||||
@@ -103,9 +106,12 @@ test_load_config_file_partial :: proc(t: ^testing.T) {
|
||||
|
||||
@(test)
|
||||
test_init_site_defaults_no_config :: proc(t: ^testing.T) {
|
||||
context.logger = log.nil_logger()
|
||||
|
||||
site: Site
|
||||
args := []string{"thor", "-config:./nonexistent.json"}
|
||||
init_site(&site, args)
|
||||
_flags: Flags
|
||||
flags.parse_or_exit(&_flags, []string{"thor", "-config:./nonexistent.json"}, .Odin)
|
||||
init_site(&site, _flags)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect_value(t, site.content_dir, "./content")
|
||||
@@ -120,9 +126,12 @@ test_init_site_defaults_no_config :: proc(t: ^testing.T) {
|
||||
|
||||
@(test)
|
||||
test_init_site_config_dir_relative :: proc(t: ^testing.T) {
|
||||
context.logger = log.nil_logger()
|
||||
|
||||
site: Site
|
||||
args := []string{"thor", "-config:./sub/nonexistent.json"}
|
||||
init_site(&site, args)
|
||||
_flags: Flags
|
||||
flags.parse_or_exit(&_flags, []string{"thor", "-config:./sub/nonexistent.json"}, .Odin)
|
||||
init_site(&site, _flags)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect_value(t, site.content_dir, "./sub/content")
|
||||
@@ -133,9 +142,12 @@ test_init_site_config_dir_relative :: proc(t: ^testing.T) {
|
||||
|
||||
@(test)
|
||||
test_init_site_flag_overrides_default :: proc(t: ^testing.T) {
|
||||
context.logger = log.nil_logger()
|
||||
|
||||
site: Site
|
||||
args := []string{"thor", "-config:./nonexistent.json", "-drafts", "-base-url:https://flag.com"}
|
||||
init_site(&site, args)
|
||||
_flags: Flags
|
||||
flags.parse_or_exit(&_flags, []string{"thor", "-config:./nonexistent.json", "-drafts", "-base-url:https://flag.com"}, .Odin)
|
||||
init_site(&site, _flags)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect(t, .Drafts in site.features)
|
||||
@@ -144,6 +156,8 @@ test_init_site_flag_overrides_default :: proc(t: ^testing.T) {
|
||||
|
||||
@(test)
|
||||
test_init_site_full_pipeline :: proc(t: ^testing.T) {
|
||||
context.logger = log.nil_logger()
|
||||
|
||||
path := write_temp_config(
|
||||
"pipeline",
|
||||
`{"title":"Pipeline Test","description":"Full","base_url":"https://config.com"}`,
|
||||
@@ -151,8 +165,9 @@ test_init_site_full_pipeline :: proc(t: ^testing.T) {
|
||||
defer os.remove(path)
|
||||
|
||||
site: Site
|
||||
args := []string{"thor", fmt.tprintf("-config:%s", path), "-drafts"}
|
||||
init_site(&site, args)
|
||||
_flags: Flags
|
||||
flags.parse_or_exit(&_flags, []string{"thor", fmt.tprintf("-config:%s", path), "-drafts"}, .Odin)
|
||||
init_site(&site, _flags)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect_value(t, site.title, "Pipeline Test")
|
||||
@@ -163,14 +178,17 @@ test_init_site_full_pipeline :: proc(t: ^testing.T) {
|
||||
|
||||
@(test)
|
||||
test_init_site_md_enable_disable :: proc(t: ^testing.T) {
|
||||
context.logger = log.nil_logger()
|
||||
|
||||
site: Site
|
||||
args := []string {
|
||||
_flags: Flags
|
||||
flags.parse_or_exit(&_flags, []string {
|
||||
"thor",
|
||||
"-config:./nonexistent.json",
|
||||
"-ext:highlight,sections",
|
||||
"-no-ext:emoji",
|
||||
}
|
||||
init_site(&site, args)
|
||||
}, .Odin)
|
||||
init_site(&site, _flags)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect(t, .Highlight in site.markdown_extensions)
|
||||
@@ -181,17 +199,23 @@ test_init_site_md_enable_disable :: proc(t: ^testing.T) {
|
||||
|
||||
@(test)
|
||||
test_init_site_config_paths :: proc(t: ^testing.T) {
|
||||
path := write_temp_config("paths", `{
|
||||
context.logger = log.nil_logger()
|
||||
|
||||
path := write_temp_config(
|
||||
"paths",
|
||||
`{
|
||||
"content_dir": "/custom/content",
|
||||
"assets_dir": "/custom/assets",
|
||||
"output_dir": "/custom/output",
|
||||
"layouts_dir": "/custom/layouts"
|
||||
}`)
|
||||
}`,
|
||||
)
|
||||
defer os.remove(path)
|
||||
|
||||
site: Site
|
||||
args := []string{"thor", fmt.tprintf("-config:%s", path)}
|
||||
init_site(&site, args)
|
||||
_flags: Flags
|
||||
flags.parse_or_exit(&_flags, []string{"thor", fmt.tprintf("-config:%s", path)}, .Odin)
|
||||
init_site(&site, _flags)
|
||||
defer destroy_site(&site)
|
||||
|
||||
testing.expect_value(t, site.content_dir, "/custom/content")
|
||||
@@ -200,3 +224,94 @@ test_init_site_config_paths :: proc(t: ^testing.T) {
|
||||
testing.expect_value(t, site.layouts_dir, "/custom/layouts")
|
||||
}
|
||||
|
||||
// --- merge_params tests ---
|
||||
|
||||
@(test)
|
||||
test_merge_params_both_present :: proc(t: ^testing.T) {
|
||||
site_params, _ := json.parse_string(
|
||||
`{"social": [], "author": "Tester"}`,
|
||||
spec = .JSON,
|
||||
allocator = context.temp_allocator,
|
||||
)
|
||||
page_params, _ := json.parse_string(
|
||||
`{"starred": true}`,
|
||||
spec = .JSON,
|
||||
allocator = context.temp_allocator,
|
||||
)
|
||||
|
||||
merged_val := merge_params(site_params, page_params)
|
||||
merged, ok := merged_val.(json.Object)
|
||||
testing.expect(t, ok, "merged should be a json.Object")
|
||||
|
||||
_, has_social := merged["social"]
|
||||
testing.expect(t, has_social, "site param 'social' should survive merge")
|
||||
|
||||
_, has_author := merged["author"]
|
||||
testing.expect(t, has_author, "site param 'author' should survive merge")
|
||||
|
||||
starred, has_starred := merged["starred"]
|
||||
testing.expect(t, has_starred, "page param 'starred' should be present")
|
||||
starred_bool, _ := starred.(json.Boolean)
|
||||
testing.expect(t, bool(starred_bool), "starred should be true")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_merge_params_nil_page :: proc(t: ^testing.T) {
|
||||
site_params, _ := json.parse_string(
|
||||
`{"author": "Tester"}`,
|
||||
spec = .JSON,
|
||||
allocator = context.temp_allocator,
|
||||
)
|
||||
|
||||
merged_val := merge_params(site_params, nil)
|
||||
merged, ok := merged_val.(json.Object)
|
||||
testing.expect(t, ok, "should return site params when page is nil")
|
||||
|
||||
_, has_author := merged["author"]
|
||||
testing.expect(t, has_author, "site param should survive")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_merge_params_nil_site :: proc(t: ^testing.T) {
|
||||
page_params, _ := json.parse_string(
|
||||
`{"starred": true}`,
|
||||
spec = .JSON,
|
||||
allocator = context.temp_allocator,
|
||||
)
|
||||
|
||||
merged_val := merge_params(nil, page_params)
|
||||
merged, ok := merged_val.(json.Object)
|
||||
testing.expect(t, ok, "should return page params when site is nil")
|
||||
|
||||
_, has_starred := merged["starred"]
|
||||
testing.expect(t, has_starred, "page param should survive")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_merge_params_both_nil :: proc(t: ^testing.T) {
|
||||
merged_val := merge_params(nil, nil)
|
||||
testing.expect(t, merged_val == nil, "both nil should return nil")
|
||||
}
|
||||
|
||||
@(test)
|
||||
test_merge_params_page_overrides_site :: proc(t: ^testing.T) {
|
||||
site_params, _ := json.parse_string(
|
||||
`{"key": "site_value"}`,
|
||||
spec = .JSON,
|
||||
allocator = context.temp_allocator,
|
||||
)
|
||||
page_params, _ := json.parse_string(
|
||||
`{"key": "page_value"}`,
|
||||
spec = .JSON,
|
||||
allocator = context.temp_allocator,
|
||||
)
|
||||
|
||||
merged_val := merge_params(site_params, page_params)
|
||||
merged, ok := merged_val.(json.Object)
|
||||
testing.expect(t, ok)
|
||||
|
||||
val := merged["key"]
|
||||
str, _ := val.(json.String)
|
||||
testing.expect_value(t, string(str), "page_value")
|
||||
}
|
||||
|
||||
|
||||
@@ -1 +1,8 @@
|
||||
{"params":{"social":[{"name":"github","url":"https://github.com/test"},{"name":"rss","url":"/index.xml"}]}}
|
||||
{
|
||||
"params": {
|
||||
"social": [
|
||||
{ "name": "github", "url": "https://github.com/test" },
|
||||
{ "name": "rss", "url": "/index.xml" }
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
@@ -146,7 +146,7 @@ init_persistent :: proc() {
|
||||
}
|
||||
|
||||
when SPALL {
|
||||
_thread_init: proc() = nil
|
||||
_thread_init: proc() = nil
|
||||
_thread_cleanup: proc() = nil
|
||||
|
||||
set_thread_callbacks :: proc(init: proc() = nil, cleanup: proc() = nil) {
|
||||
@@ -249,7 +249,14 @@ ensure_parser :: proc(lang: string) -> ^Grammar_Cache {
|
||||
return gc
|
||||
}
|
||||
|
||||
compile_query :: proc(lang: string, language: Language) -> (query: Query, cursor: Query_Cursor, ok: bool) {
|
||||
compile_query :: proc(
|
||||
lang: string,
|
||||
language: Language,
|
||||
) -> (
|
||||
query: Query,
|
||||
cursor: Query_Cursor,
|
||||
ok: bool,
|
||||
) {
|
||||
query_src, query_path, qok := load_query(lang)
|
||||
if !qok {
|
||||
return
|
||||
@@ -262,7 +269,7 @@ compile_query :: proc(lang: string, language: Language) -> (query: Query, cursor
|
||||
if query == nil {
|
||||
tok := extract_query_token(transmute([]byte)query_src, err_offset)
|
||||
cause := fmt.tprintf("query error at byte %d (type %v)", err_offset, err_type)
|
||||
#partial switch err_type {
|
||||
switch err_type {
|
||||
case .NodeType:
|
||||
if tok != "" {
|
||||
cause = fmt.tprintf(
|
||||
@@ -288,6 +295,7 @@ compile_query :: proc(lang: string, language: Language) -> (query: Query, cursor
|
||||
cause = fmt.tprintf("query has an illegal pattern structure at byte %d", err_offset)
|
||||
case .Language:
|
||||
cause = "grammar language is null (broken grammar .so)"
|
||||
case .None:
|
||||
}
|
||||
log.errorf("treesitter: %s query failed: %s", lang, cause)
|
||||
|
||||
@@ -451,4 +459,3 @@ helix_version_from_path :: proc(path: string) -> string {
|
||||
if end <= start do return ""
|
||||
return path[start:end]
|
||||
}
|
||||
|
||||
|
||||
@@ -49,7 +49,7 @@ mount_recursive :: proc(vfs: ^VFS, current_dir: string, target_prefix: string) {
|
||||
defer os.file_info_slice_delete(entries, context.allocator)
|
||||
|
||||
for entry in entries {
|
||||
#partial switch entry.type {
|
||||
switch entry.type {
|
||||
case .Regular:
|
||||
virtual := fmt.tprintf("%s/%s", target_prefix, entry.name)
|
||||
vfs.files[virtual] = VFS_Entry {
|
||||
@@ -58,7 +58,7 @@ mount_recursive :: proc(vfs: ^VFS, current_dir: string, target_prefix: string) {
|
||||
case .Directory:
|
||||
sub_prefix := fmt.tprintf("%s/%s", target_prefix, entry.name)
|
||||
mount_recursive(vfs, entry.fullpath, sub_prefix)
|
||||
case:
|
||||
case .Undetermined, .Symlink, .Named_Pipe, .Socket, .Block_Device, .Character_Device:
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -109,4 +109,3 @@ vfs_entry_data :: proc(entry: VFS_Entry) -> ([]byte, bool) {
|
||||
}
|
||||
return data, true
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user