diff options
Diffstat (limited to 'scripts/metadata_filter.lua')
-rw-r--r-- | scripts/metadata_filter.lua | 79 |
1 files changed, 57 insertions, 22 deletions
diff --git a/scripts/metadata_filter.lua b/scripts/metadata_filter.lua index 61fe90f..5ed5382 100644 --- a/scripts/metadata_filter.lua +++ b/scripts/metadata_filter.lua | |||
@@ -92,11 +92,33 @@ function relative_to(dir, target) | |||
92 | return path | 92 | return path |
93 | end | 93 | end |
94 | 94 | ||
95 | function resolve_url(page_type, site_url, output_dir, base_dir_out, cur_file_out) | 95 | function apply_path_rewrites(rewrites, str) |
96 | local abs = cur_file_out:gsub("^" .. output_dir, ""):gsub("/index.html$", | 96 | for i = 1, #rewrites.path do |
97 | "/") | 97 | local r = rewrites.path[i] |
98 | str = str:gsub(pandoc.utils.stringify(r.from), | ||
99 | pandoc.utils.stringify(r.to)) | ||
100 | end | ||
101 | return str | ||
102 | end | ||
103 | |||
104 | function get_file_out(rewrites, content_dir, output_dir, file_in) | ||
105 | local file_out = file_in:gsub("^" .. content_dir, ""):gsub("%.md$", ".html") | ||
106 | |||
107 | if file_out:match(".html$") and not file_out:match("/index%.html$") then | ||
108 | file_out = file_out:gsub("/(.*)%.html$", "/%1/index.html") | ||
109 | end | ||
110 | |||
111 | file_out = apply_path_rewrites(rewrites, file_out) | ||
112 | |||
113 | return pandoc.MetaString(output_dir .. file_out) | ||
114 | end | ||
115 | |||
116 | function resolve_url(site_url, output_dir, ref_file, target_file) | ||
117 | local ref_base_dir = ref_file:gsub("^(.*)/.-$", "%1") | ||
118 | local abs = target_file:gsub("^" .. output_dir, ""):gsub("/index%.html$", | ||
119 | "/") | ||
98 | local rel = | 120 | local rel = |
99 | relative_to(base_dir_out, cur_file_out):gsub("/index.html$", "/") | 121 | relative_to(ref_base_dir, target_file):gsub("/index%.html$", "/") |
100 | 122 | ||
101 | return pandoc.MetaMap({ | 123 | return pandoc.MetaMap({ |
102 | abs = pandoc.MetaString(abs), | 124 | abs = pandoc.MetaString(abs), |
@@ -115,8 +137,9 @@ function resolve_layout(layout) | |||
115 | end | 137 | end |
116 | end | 138 | end |
117 | 139 | ||
118 | function resolve_section(abs_url) | 140 | function resolve_section(content_dir, file_in) |
119 | local section = abs_url:match("^/(.-)[/.]") or "index" | 141 | local section = file_in:gsub("^" .. content_dir, ""):match("^/(.-)[/.]") or |
142 | "index" | ||
120 | return pandoc.MetaMap({ | 143 | return pandoc.MetaMap({ |
121 | id = pandoc.MetaString(section), | 144 | id = pandoc.MetaString(section), |
122 | ["is_" .. section] = pandoc.MetaBool(true) | 145 | ["is_" .. section] = pandoc.MetaBool(true) |
@@ -133,13 +156,15 @@ function resolve_category(categories, category) | |||
133 | end | 156 | end |
134 | end | 157 | end |
135 | 158 | ||
136 | function create_main_menu_state(section, main_menu) | 159 | function prep_main_menu(rewrites, section, main_menu) |
137 | local active_item = nil | 160 | local active_item = nil |
138 | 161 | ||
139 | for i = 1, #main_menu do | 162 | for i = 1, #main_menu do |
140 | local item = main_menu[i] | 163 | local item = main_menu[i] |
141 | local active = pandoc.utils.stringify(item.id) == section.id | 164 | local active = pandoc.utils.stringify(item.id) == section.id |
142 | item.active = pandoc.MetaBool(active) | 165 | item.active = pandoc.MetaBool(active) |
166 | item.url = apply_path_rewrites(rewrites, | ||
167 | pandoc.utils.stringify(item.url)) | ||
143 | if active then active_item = item end | 168 | if active then active_item = item end |
144 | end | 169 | end |
145 | 170 | ||
@@ -151,7 +176,13 @@ function create_main_menu_state(section, main_menu) | |||
151 | }) | 176 | }) |
152 | end | 177 | end |
153 | 178 | ||
154 | function organize_subpages(pages) | 179 | function organize_subpages(site_url, output_dir, ref_file, pages) |
180 | for i = 1, #pages do | ||
181 | local page = pages[i] | ||
182 | page.url = resolve_url(site_url, output_dir, ref_file, | ||
183 | pandoc.utils.stringify(page.file_out)) | ||
184 | end | ||
185 | |||
155 | local grouped_pages = group_by(pages, function(p) return not p.date end) | 186 | local grouped_pages = group_by(pages, function(p) return not p.date end) |
156 | 187 | ||
157 | local pages_undated = grouped_pages[true] or pandoc.MetaList({}) | 188 | local pages_undated = grouped_pages[true] or pandoc.MetaList({}) |
@@ -201,13 +232,17 @@ function organize_subpages(pages) | |||
201 | end | 232 | end |
202 | 233 | ||
203 | function Meta(meta) | 234 | function Meta(meta) |
235 | meta.content_dir = meta.content_dir:gsub("/$", "") | ||
204 | meta.output_dir = meta.output_dir:gsub("/$", "") | 236 | meta.output_dir = meta.output_dir:gsub("/$", "") |
205 | meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") | 237 | meta.site.url = pandoc.utils.stringify(meta.site.url):gsub("/$", "") |
206 | meta.base_file_out = meta.base_file_out or meta.file_out | 238 | meta.rewrites = meta.rewrites or |
207 | meta.base_dir_out = meta.base_file_out:gsub("^(.*)/.-$", "%1") | 239 | pandoc.MetaMap({path = pandoc.MetaList({})}) |
208 | meta.page_type = meta.page_type or "page" | 240 | meta.page_type = meta.page_type or "page" |
209 | |||
210 | meta.layout = resolve_layout(meta.layout) | 241 | meta.layout = resolve_layout(meta.layout) |
242 | meta.section = resolve_section(meta.content_dir, meta.file_in) | ||
243 | |||
244 | meta.file_out = get_file_out(meta.rewrites, meta.content_dir, | ||
245 | meta.output_dir, meta.file_in) | ||
211 | 246 | ||
212 | meta.date = format_date(meta.date) | 247 | meta.date = format_date(meta.date) |
213 | if meta.last_update ~= nil then | 248 | if meta.last_update ~= nil then |
@@ -216,26 +251,26 @@ function Meta(meta) | |||
216 | meta.last_update = meta.date | 251 | meta.last_update = meta.date |
217 | end | 252 | end |
218 | 253 | ||
254 | meta.category = resolve_category(meta.categories[meta.section.id], | ||
255 | meta.category) | ||
256 | meta.categories = nil | ||
257 | |||
219 | if meta.page_type == "feed" then | 258 | if meta.page_type == "feed" then |
220 | meta.page = pandoc.MetaMap({ | 259 | meta.page = pandoc.MetaMap({ |
221 | url = resolve_url("page", meta.site.url, meta.output_dir, | 260 | url = resolve_url(meta.site.url, meta.output_dir, meta.file_out, |
222 | meta.base_dir_out, meta.file_out) | 261 | meta.file_out:gsub("%.xml$", ".html")) |
223 | }) | 262 | }) |
224 | end | 263 | end |
225 | 264 | ||
226 | meta.url = resolve_url(meta.page_type, meta.site.url, meta.output_dir, | ||
227 | meta.base_dir_out, meta.file_out) | ||
228 | meta.section = resolve_section(meta.url.abs) | ||
229 | meta.category = resolve_category(meta.categories[meta.section.id], | ||
230 | meta.category) | ||
231 | meta.categories = nil | ||
232 | |||
233 | if meta.menus and meta.menus.main then | 265 | if meta.menus and meta.menus.main then |
234 | meta.menus.main = create_main_menu_state(meta.section, meta.menus.main) | 266 | meta.menus.main = prep_main_menu(meta.rewrites, meta.section, |
267 | meta.menus.main) | ||
235 | end | 268 | end |
236 | 269 | ||
237 | if meta.pages then | 270 | if meta.pages then |
238 | local pages, categories = organize_subpages(meta.pages) | 271 | local pages, categories = organize_subpages(meta.site.url, |
272 | meta.output_dir, | ||
273 | meta.file_out, meta.pages) | ||
239 | meta.pages = pages | 274 | meta.pages = pages |
240 | meta.categories = categories | 275 | meta.categories = categories |
241 | end | 276 | end |