-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.lua
More file actions
165 lines (144 loc) · 4.04 KB
/
Copy pathinit.lua
File metadata and controls
165 lines (144 loc) · 4.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
require('ssg.path')('ssg')
-- local dsl = require('ssg.dsl.init')
local util = require('ssg.util')
local router = require('ssg.router')
-- {{{ transforms
-- remove nil holes from a numeric table
-- if max_index is not provided, it's found using iteration via pairs
local function compact_table(t, max_index)
if not max_index then
max_index = 0
for k, _ in pairs(t) do
if type(k) == 'number' and k > max_index then
max_index = k
end
end
end
local write = 1
for read = 1, max_index do
if t[read] ~= nil then
if read ~= write then
t[write] = t[read]
t[read] = nil
end
write = write + 1
end
end
return t
end
-- collect pages into sorted groups
local function group_pages(manifest)
local nav = {}
local blog_chrono = {}
local blog_series = {}
local old_num_pages = #manifest.pages
for k, page in pairs(manifest.pages) do
if page.meta.skip then
-- skip this page, stop it from rendering
manifest.pages[k] = nil
else
if page.meta.nav then
-- rewrite path to toplevel instead of nav subdir
page.dst_rel_path = page.dst_rel_path:gsub('^nav/', '')
nav[#nav+1] = page
elseif page.meta.blog then
blog_chrono[#blog_chrono+1] = page
if page.meta.series then
local series = blog_series[page.meta.series] or {}
series[#series+1] = page
blog_series[page.meta.series] = series
end
end
page.url = '/' .. page.dst_rel_path:gsub('%.html$', '')
page.slug = page.url:match('^.*/([^/]-)$')
end
end
compact_table(manifest.pages, old_num_pages)
-- manual ordering for navbar entries
table.sort(nav, function(a, b) return a.meta.ord < b.meta.ord end)
-- sort all blog posts by date, newest first
table.sort(blog_chrono, function(a, b) return a.meta.date > b.meta.date end)
-- sort blog posts within a series by date, oldest first
for _, series in pairs(blog_series) do
table.sort(series, function(a, b) return a.meta.date < b.meta.date end)
end
manifest.groups.nav = nav
manifest.groups.blog = {
chrono = blog_chrono,
series = blog_series,
}
end
-- build tag slug/name mapping and tag counts
local function blog_collect_tags(manifest)
-- derive url-safe slug from human-readable tag name
local function tag_to_slug(name)
return name:lower():gsub('%s+', '-'):gsub('[^%w-]', '')
end
-- discover tag pages
local tag_pages = {} -- slug -> page entry
for _, page in ipairs(manifest.pages) do
local slug = page.rel_path:match('^blog/tags/(.+)%.html%.lua$')
if slug then
tag_pages[slug] = page
end
end
local counts = {}
local tag_slugs = {} -- human-readable name -> slug
local tag_names = {} -- slug -> human-readable name
for _, page in ipairs(manifest.groups.blog.chrono) do
if page.meta.tags then
for _, tag in ipairs(page.meta.tags) do
local slug = tag_to_slug(tag)
if tag_pages[slug] then
tag_slugs[tag] = slug
if not tag_names[slug] then
tag_names[slug] = tag
end
counts[slug] = (counts[slug] or 0) + 1
else
util.log('tag "' .. tag .. '" missing page!', 'warn', 'blog')
end
end
end
end
-- sort by count descending, alphabetical for ties
local sorted = {}
for slug, count in pairs(counts) do
sorted[#sorted+1] = { slug = slug, count = count }
end
table.sort(sorted, function(a, b)
if a.count ~= b.count then return a.count > b.count end
return a.slug < b.slug
end)
manifest.groups.blog.tags = sorted
manifest.groups.blog.tag_pages = tag_pages
manifest.groups.blog.tag_names = tag_names
manifest.groups.blog.tag_slugs = tag_slugs
end
-- add next/prev metadata fields to pages in the blog group
local function blog_add_next_prev(manifest)
local blog = manifest.groups.blog.chrono
for i = 1, #blog do
if i > 1 then
blog[i].meta.next = blog[i - 1]
end
if i < #blog then
blog[i].meta.prev = blog[i + 1]
end
end
end
-- }}}
router.register_handler('lua', 'svg', function(src)
return nil, loadfile(src)
end)
router.process({
in_dir = './src/site',
out_dir = './dist',
components_dir = './src/components',
ctx = {},
middlewares = {
group_pages,
blog_collect_tags,
blog_add_next_prev,
},
})