diff options
Diffstat (limited to 'scripts')
-rw-r--r-- | scripts/lib/common.lua | 23 | ||||
-rw-r--r-- | scripts/metadata.lua | 39 | ||||
-rw-r--r-- | scripts/page.lua | 181 |
3 files changed, 147 insertions, 96 deletions
diff --git a/scripts/lib/common.lua b/scripts/lib/common.lua index 5ed3e31..ee7c6aa 100644 --- a/scripts/lib/common.lua +++ b/scripts/lib/common.lua | |||
@@ -39,6 +39,20 @@ function pandoc.List:flatMap(fn) | |||
39 | return result | 39 | return result |
40 | end | 40 | end |
41 | 41 | ||
42 | function pandoc.List:filterMap(fn) | ||
43 | local mapped = self:map(fn) | ||
44 | local result = pandoc.List() | ||
45 | |||
46 | for i = 1, #mapped do | ||
47 | local val = mapped[i] | ||
48 | if val then | ||
49 | result:insert(val) | ||
50 | end | ||
51 | end | ||
52 | |||
53 | return result | ||
54 | end | ||
55 | |||
42 | function pandoc.List:take(n) | 56 | function pandoc.List:take(n) |
43 | if n >= #self then return self end | 57 | if n >= #self then return self end |
44 | 58 | ||
@@ -59,6 +73,15 @@ function pandoc.List:skip(n) | |||
59 | return result | 73 | return result |
60 | end | 74 | end |
61 | 75 | ||
76 | function pandoc.List:shuffle() | ||
77 | for i = #self, 2, -1 do | ||
78 | local j = math.random(i) | ||
79 | self[i], self[j] = self[j], self[i] | ||
80 | end | ||
81 | |||
82 | return self | ||
83 | end | ||
84 | |||
62 | return { | 85 | return { |
63 | dump = dump | 86 | dump = dump |
64 | } | 87 | } |
diff --git a/scripts/metadata.lua b/scripts/metadata.lua index afd0ca5..5d372fc 100644 --- a/scripts/metadata.lua +++ b/scripts/metadata.lua | |||
@@ -71,13 +71,11 @@ function prep_menu(active_id, main_menu) | |||
71 | return { items = items, active = active_item } | 71 | return { items = items, active = active_item } |
72 | end | 72 | end |
73 | 73 | ||
74 | function process_pages(global, build, meta) | 74 | function process_pages(meta) |
75 | local pages_all = pandoc.List() | 75 | local pages_all = pandoc.List() |
76 | local pages_date_desc = pandoc.List() | 76 | local pages_date_desc = pandoc.List() |
77 | 77 | ||
78 | for id, p in pairs(meta.pages) do | 78 | for id, p in pairs(meta.pages) do |
79 | process(global, build, p, meta) | ||
80 | |||
81 | if not p.unlisted then | 79 | if not p.unlisted then |
82 | pages_all:insert(id) | 80 | pages_all:insert(id) |
83 | if p.last_update then pages_date_desc:insert(id) end | 81 | if p.last_update then pages_date_desc:insert(id) end |
@@ -86,8 +84,14 @@ function process_pages(global, build, meta) | |||
86 | 84 | ||
87 | pages_all:sort(sort.page_sort(meta.list_order, meta.pages)) | 85 | pages_all:sort(sort.page_sort(meta.list_order, meta.pages)) |
88 | pages_date_desc:sort(sort.page_sort("date_desc", meta.pages)) | 86 | pages_date_desc:sort(sort.page_sort("date_desc", meta.pages)) |
87 | local pages_random = pages_all:clone():shuffle() | ||
89 | 88 | ||
90 | meta.pages = { all = pages_all, date_desc = pages_date_desc, by_id = meta.pages } | 89 | meta.pages = { |
90 | all = pages_all, | ||
91 | date_desc = pages_date_desc, | ||
92 | random = pages_random, | ||
93 | by_id = meta.pages | ||
94 | } | ||
91 | end | 95 | end |
92 | 96 | ||
93 | function find_depth(meta) | 97 | function find_depth(meta) |
@@ -126,7 +130,7 @@ function resolve_dates(meta) | |||
126 | meta.was_updated = meta.date and meta.last_update and meta.date.yyyy_mm_dd ~= meta.last_update.yyyy_mm_dd | 130 | meta.was_updated = meta.date and meta.last_update and meta.date.yyyy_mm_dd ~= meta.last_update.yyyy_mm_dd |
127 | end | 131 | end |
128 | 132 | ||
129 | function process_base(build, meta, parent) | 133 | function process_base(build, meta) |
130 | meta.namespace = prep_namespace(meta.namespace) | 134 | meta.namespace = prep_namespace(meta.namespace) |
131 | meta.file_out = utils.stringify(meta.file_out) | 135 | meta.file_out = utils.stringify(meta.file_out) |
132 | meta.unlisted = meta.unlisted or (meta.draft and build.mode ~= "dev") | 136 | meta.unlisted = meta.unlisted or (meta.draft and build.mode ~= "dev") |
@@ -140,16 +144,33 @@ function process_base(build, meta, parent) | |||
140 | meta.position = meta.position and tonumber(utils.stringify(meta.position)) | 144 | meta.position = meta.position and tonumber(utils.stringify(meta.position)) |
141 | meta.thumbnail = meta.thumbnail and make_absolute("thumbnail." .. utils.stringify(meta.thumbnail), meta.file_out) | 145 | meta.thumbnail = meta.thumbnail and make_absolute("thumbnail." .. utils.stringify(meta.thumbnail), meta.file_out) |
142 | meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth)) | 146 | meta.layout = prep_layout(meta.layout or (meta.redirect and "redirect") or resolve_layout(meta.depth)) |
143 | meta.show_date = parent and parent.list_order == "date_desc" | 147 | |
148 | meta.description = (meta.description and pandoc.MetaBlocks(pandoc.Para(meta.description))) or | ||
149 | (not meta.no_description and meta.content) | ||
150 | meta.show_date = meta.parent and meta.parent.list_order == "date_desc" | ||
151 | meta.category = meta.parent and meta.parent.title | ||
152 | meta.icon = meta.icon or (meta.parent and meta.parent.icon) | ||
153 | meta.post_icon = meta.post_icon or (meta.parent and meta.parent.list_post_icon) or | ||
154 | (meta.parent and meta.parent.parent and meta.parent.parent.list_post_icon) | ||
155 | meta.indicator = meta.parent and meta.parent.list_read_indicators | ||
144 | end | 156 | end |
145 | 157 | ||
146 | function process(global, build, meta, parent) | 158 | function process(global, build, meta) |
147 | process_base(build, meta, parent) | 159 | process_base(build, meta) |
148 | process_pages(global, build, meta) | 160 | |
161 | for _, p in pairs(meta.pages) do | ||
162 | p.parent = meta | ||
163 | process(global, build, p) | ||
164 | p.parent = nil | ||
165 | end | ||
166 | |||
167 | process_pages(meta) | ||
149 | resolve_dates(meta) | 168 | resolve_dates(meta) |
150 | end | 169 | end |
151 | 170 | ||
152 | function Meta(meta) | 171 | function Meta(meta) |
172 | math.randomseed(os.time()) | ||
173 | |||
153 | local build = {} | 174 | local build = {} |
154 | 175 | ||
155 | for key, value in pairs(meta) do | 176 | for key, value in pairs(meta) do |
diff --git a/scripts/page.lua b/scripts/page.lua index 417e4a0..fd12230 100644 --- a/scripts/page.lua +++ b/scripts/page.lua | |||
@@ -35,93 +35,60 @@ function prep_menu(namespace, main_menu) | |||
35 | return { items = items, active = active_item } | 35 | return { items = items, active = active_item } |
36 | end | 36 | end |
37 | 37 | ||
38 | function process_pages(global, build, meta) | 38 | function d1_page_to_list_item(meta) |
39 | for _, p in pairs(meta.pages.by_id) do | ||
40 | process_pages(global, build, p) | ||
41 | end | ||
42 | |||
43 | meta.pages = { | ||
44 | all = meta.pages.all:map(function (ref) return meta.pages.by_id[utils.stringify(ref)] end), | ||
45 | date_desc = meta.pages.date_desc:map(function (ref) return meta.pages.by_id[utils.stringify(ref)] end), | ||
46 | by_id = meta.pages.by_id | ||
47 | } | ||
48 | end | ||
49 | |||
50 | function resolve_urls(global, build, meta) | ||
51 | meta.url = resolve_url(global.site.url, build.file_out, meta.url) | ||
52 | |||
53 | if meta.feed then | ||
54 | if meta.file_out:match(".html$") then | ||
55 | meta.feed = { | ||
56 | url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".html$", ".xml")), | ||
57 | } | ||
58 | else | ||
59 | meta.page = { | ||
60 | url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".xml$", ".html")), | ||
61 | } | ||
62 | end | ||
63 | end | ||
64 | |||
65 | if meta.thumbnail then | ||
66 | meta.thumbnail = resolve_url(global.site.url, build.file_out, meta.thumbnail) | ||
67 | end | ||
68 | end | ||
69 | |||
70 | function d1_page_to_list_item(meta, p) | ||
71 | return { | 39 | return { |
72 | title = p.title, | 40 | title = meta.title, |
73 | subtitle = p.subtitle, | 41 | subtitle = meta.subtitle, |
74 | date = p.date, | 42 | date = meta.date, |
75 | last_update = p.last_update, | 43 | last_update = meta.last_update, |
76 | show_date = p.show_date, | 44 | show_date = meta.show_date, |
77 | schema_type = p.schema_type, | 45 | schema_type = meta.schema_type, |
78 | draft = p.draft, | 46 | draft = meta.draft, |
79 | position = p.position, | 47 | position = meta.position, |
80 | url = p.url, | 48 | url = meta.url, |
81 | rel = p.rel, | 49 | rel = meta.rel, |
82 | slug = p.slug, | 50 | slug = meta.slug, |
83 | thumbnail = p.thumbnail, | 51 | thumbnail = meta.thumbnail, |
84 | icon = p.icon or meta.icon, | 52 | icon = meta.icon, |
85 | post_icon = p.post_icon or meta.list_post_icon, | 53 | post_icon = meta.post_icon, |
86 | indicator = meta.list_read_indicators, | 54 | indicator = meta.indicator, |
87 | } | 55 | } |
88 | end | 56 | end |
89 | 57 | ||
90 | function d2_page_to_list_item(meta, cat, p, set_cat_title) | 58 | function d2_page_to_list_item(meta, with_category) |
91 | return { | 59 | return { |
92 | title = p.title, | 60 | title = meta.title, |
93 | subtitle = p.subtitle, | 61 | subtitle = meta.subtitle, |
94 | category = set_cat_title and cat.title, | 62 | category = with_category and meta.category, |
95 | date = p.date, | 63 | date = meta.date, |
96 | last_update = p.last_update, | 64 | last_update = meta.last_update, |
97 | show_date = p.show_date, | 65 | show_date = meta.show_date, |
98 | schema_type = p.schema_type, | 66 | schema_type = meta.schema_type, |
99 | draft = p.draft, | 67 | draft = meta.draft, |
100 | position = p.position, | 68 | position = meta.position, |
101 | url = p.url, | 69 | url = meta.url, |
102 | rel = p.rel, | 70 | rel = meta.rel, |
103 | slug = p.slug, | 71 | slug = meta.slug, |
104 | thumbnail = p.thumbnail, | 72 | thumbnail = meta.thumbnail, |
105 | icon = p.icon or cat.icon, | 73 | icon = meta.icon, |
106 | post_icon = p.post_icon or cat.list_post_icon or meta.list_post_icon, | 74 | post_icon = meta.post_icon, |
107 | indicator = cat.list_read_indicators, | 75 | indicator = meta.indicator, |
108 | } | 76 | } |
109 | end | 77 | end |
110 | 78 | ||
111 | function cat_to_list_cat(cat, allItems) | 79 | function cat_to_list_cat(meta, allItems) |
112 | local limit = cat.list_limit or 9999 | 80 | local limit = meta.list_limit or 9999 |
113 | local items = allItems:take(limit) | 81 | local items = allItems:take(limit) |
114 | local omitted = #allItems - #items | 82 | local omitted = #allItems - #items |
115 | 83 | ||
116 | return { | 84 | return { |
117 | title = cat.title, | 85 | title = meta.title, |
118 | description = (cat.description and pandoc.MetaBlocks(pandoc.Para(cat.description))) or | 86 | description = meta.description, |
119 | (not cat.no_description and cat.content), | 87 | last_update = meta.last_update, |
120 | last_update = cat.last_update, | 88 | schema_type = meta.schema_type, |
121 | schema_type = cat.schema_type, | 89 | url = meta.url, |
122 | url = cat.url, | 90 | slug = meta.slug, |
123 | slug = cat.slug, | 91 | layout = meta.list_layout, |
124 | layout = cat.list_layout, | ||
125 | items = items, | 92 | items = items, |
126 | total = tostring(#allItems), | 93 | total = tostring(#allItems), |
127 | omitted = omitted ~= 0 and tostring(omitted), | 94 | omitted = omitted ~= 0 and tostring(omitted), |
@@ -132,13 +99,13 @@ function generate_list(meta) | |||
132 | if meta.depth < 1 then return pandoc.List() end | 99 | if meta.depth < 1 then return pandoc.List() end |
133 | 100 | ||
134 | if meta.depth == 1 then | 101 | if meta.depth == 1 then |
135 | return meta.pages.all:map(function(p) return d1_page_to_list_item(meta, p) end) | 102 | return meta.pages.all |
136 | end | 103 | end |
137 | 104 | ||
138 | if meta.depth == 2 then | 105 | if meta.depth == 2 then |
139 | return meta.pages.all | 106 | return meta.pages.all |
140 | :map(function(cat) | 107 | :map(function(cat) |
141 | local allItems = cat.pages.all:map(function(p) return d2_page_to_list_item(meta, cat, p, false) end) | 108 | local allItems = cat.pages.all:map(function(p) return d2_page_to_list_item(p, false) end) |
142 | 109 | ||
143 | return cat_to_list_cat(cat, allItems) | 110 | return cat_to_list_cat(cat, allItems) |
144 | end) | 111 | end) |
@@ -150,9 +117,9 @@ function generate_list(meta) | |||
150 | :map(function(cat) | 117 | :map(function(cat) |
151 | local allItems = cat.pages.all:flatMap(function(c) | 118 | local allItems = cat.pages.all:flatMap(function(c) |
152 | if cat.list_flatten and c.depth ~= 0 then | 119 | if cat.list_flatten and c.depth ~= 0 then |
153 | return c.pages.all:map(function(p) return d2_page_to_list_item(cat, c, p, true) end) | 120 | return c.pages.all:map(function(p) return d2_page_to_list_item(p, true) end) |
154 | else | 121 | else |
155 | return pandoc.List({ d1_page_to_list_item(cat, c) }) | 122 | return pandoc.List({ d1_page_to_list_item(c) }) |
156 | end | 123 | end |
157 | end) | 124 | end) |
158 | 125 | ||
@@ -166,6 +133,38 @@ function generate_list(meta) | |||
166 | end | 133 | end |
167 | end | 134 | end |
168 | 135 | ||
136 | function deref_page(pages) | ||
137 | return function(ref) | ||
138 | return pages[utils.stringify(ref)] | ||
139 | end | ||
140 | end | ||
141 | |||
142 | function process_pages(meta) | ||
143 | meta.pages.all = meta.pages.all:filterMap(deref_page(meta.pages.by_id)) | ||
144 | meta.pages.date_desc = meta.pages.date_desc:filterMap(deref_page(meta.pages.by_id)) | ||
145 | -- meta.pages.random = meta.pages.random:filterMap(deref_page(meta.pages.by_id)) | ||
146 | end | ||
147 | |||
148 | function resolve_urls(global, build, meta) | ||
149 | meta.url = resolve_url(global.site.url, build.file_out, meta.url) | ||
150 | |||
151 | if meta.feed then | ||
152 | if meta.file_out:match(".html$") then | ||
153 | meta.feed = { | ||
154 | url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".html$", ".xml")), | ||
155 | } | ||
156 | else | ||
157 | meta.page = { | ||
158 | url = resolve_url(global.site.url, build.file_out, meta.file_out:gsub(".xml$", ".html")), | ||
159 | } | ||
160 | end | ||
161 | end | ||
162 | |||
163 | if meta.thumbnail then | ||
164 | meta.thumbnail = resolve_url(global.site.url, build.file_out, meta.thumbnail) | ||
165 | end | ||
166 | end | ||
167 | |||
169 | function process_base(meta) | 168 | function process_base(meta) |
170 | meta.depth = tonumber(utils.stringify(meta.depth)) | 169 | meta.depth = tonumber(utils.stringify(meta.depth)) |
171 | meta.file_out = utils.stringify(meta.file_out) | 170 | meta.file_out = utils.stringify(meta.file_out) |
@@ -177,13 +176,25 @@ function process_base(meta) | |||
177 | meta.thumbnail = meta.thumbnail and utils.stringify(meta.thumbnail) | 176 | meta.thumbnail = meta.thumbnail and utils.stringify(meta.thumbnail) |
178 | end | 177 | end |
179 | 178 | ||
180 | function process(global, build, meta) | 179 | function process(global, build, meta, dir) |
181 | process_base(meta) | 180 | process_base(meta) |
182 | resolve_urls(global, build, meta) | 181 | resolve_urls(global, build, meta) |
183 | 182 | ||
184 | for _, p in pairs(meta.pages.by_id) do | 183 | if dir >= 0 then |
185 | process(global, build, p) | 184 | for _, p in pairs(meta.pages.by_id) do |
185 | p.parent = meta | ||
186 | process(global, build, p, 1) | ||
187 | p.parent = nil | ||
188 | end | ||
186 | end | 189 | end |
190 | |||
191 | process_pages(meta) | ||
192 | |||
193 | meta.list = generate_list(meta) | ||
194 | |||
195 | -- if dir <= 0 and meta.parent then | ||
196 | -- process(global, build, meta.parent, -1) | ||
197 | -- end | ||
187 | end | 198 | end |
188 | 199 | ||
189 | function Meta(meta) | 200 | function Meta(meta) |
@@ -206,8 +217,6 @@ function Meta(meta) | |||
206 | 217 | ||
207 | meta = meta.tree | 218 | meta = meta.tree |
208 | 219 | ||
209 | process(global, build, meta) | ||
210 | |||
211 | local parts = utils.stringify(build.namespace):split('/'):skip(1) | 220 | local parts = utils.stringify(build.namespace):split('/'):skip(1) |
212 | for i = 1, #parts do | 221 | for i = 1, #parts do |
213 | local part = parts[i] | 222 | local part = parts[i] |
@@ -217,9 +226,7 @@ function Meta(meta) | |||
217 | meta = p | 226 | meta = p |
218 | end | 227 | end |
219 | 228 | ||
220 | process_pages(global, build, meta) | 229 | process(global, build, meta, 0) |
221 | |||
222 | meta.list = generate_list(meta) | ||
223 | 230 | ||
224 | if global.menus and global.menus.main then | 231 | if global.menus and global.menus.main then |
225 | global.menus.main = prep_menu(meta.namespace.full, global.menus.main) | 232 | global.menus.main = prep_menu(meta.namespace.full, global.menus.main) |