feat: Added weight field to Page.

This commit is contained in:
Spencer Brower
2026-07-29 12:52:15 -04:00
parent 040c462bba
commit a420803b3e
6 changed files with 176 additions and 6 deletions
+14 -3
View File
@@ -136,7 +136,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)
@@ -373,11 +373,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
}
@@ -385,6 +386,16 @@ sort_pages_by_date :: proc(pages: #soa[]Page) {
}
}
compare_pages :: proc(pages: #soa[]Page, j: int, key: Page) -> int {
wj := pages.weight[j]
wk := key.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] == '/' {