diff options
| author | Kévin Le Gouguec <kevin.legouguec@gmail.com> | 2020-08-19 00:10:08 +0200 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@gmail.com> | 2020-08-19 00:10:08 +0200 |
| commit | 30e831c9602ea5d4d0603f5ad03baff481771b4b (patch) | |
| tree | 7548cd29ed5ecf3e975df5a2e6ae130ae488f36f | |
| parent | 338f941477ef6815bd4638bf166f1fddb5a4b3d3 (diff) | |
| download | memory-leaks-30e831c9602ea5d4d0603f5ad03baff481771b4b.tar.xz | |
Put extensions in Lua module instead of sneaking them in environment
Note that neither .cache/config.lua nor .cache/site-tree.json get
updated when EXTENSIONS changes. This could be hacked as follows:
config = EXTENSIONS="$(EXTENSIONS)"
ifneq "$(shell test -f $(config_token) && cat $(config_token))" \
"$(shell ./generate-config-token.py $(config))"
.PHONY: $(lua_config) $(site_tree) $(config_token)
endif
Plus a recipe for config_token, and some dependencies on it.
| -rw-r--r-- | repo/www/Makefile | 11 | ||||
| -rw-r--r-- | repo/www/convert-internal-links.lua | 10 | ||||
| -rwxr-xr-x | repo/www/generate-lua-config.py | 34 | ||||
| -rw-r--r-- | repo/www/helpers.py | 3 |
4 files changed, 46 insertions, 12 deletions
diff --git a/repo/www/Makefile b/repo/www/Makefile index f88672c..aa48971 100644 --- a/repo/www/Makefile +++ b/repo/www/Makefile @@ -23,6 +23,8 @@ cache = .cache title = $(cache)/title # Maps folders to their contents (files and subfolders). site_tree = $(cache)/site-tree.json +# Lua module to let filters know about the configuration. +lua_config = $(cache)/config.lua # Defines $(pages) and $(indices). dependencies = $(cache)/deps.mk @@ -32,6 +34,9 @@ $(title): $(top_readme) | $(cache) $(site_tree): $(page_folders) | $(cache) ./generate-tree.py -o $@ $(EXTENSIONS) +$(lua_config): | $(cache) + ./generate-lua-config.py EXTENSIONS="$(EXTENSIONS)" > $@ + $(dependencies): $(site_tree) | $(cache) ./generate-deps.py $< $@ $(OUT_DIR) @@ -49,10 +54,10 @@ $(html_folders) $(cache): $(pages) $(subindices): $(title) -$(pages) $(indices): $(html_template) +$(pages) $(indices): $(html_template) $(lua_filters) $(lua_config) $(pages): $(OUT_DIR)/%.html: - $(call v,PAGE,$*) EXTENSIONS="$(EXTENSIONS)" \ + $(call v,PAGE,$*) \ ./generate-page.py --site-title="$$(cat $(title))" --title="$*" \ $(foreach f,$(lua_filters),--lua-filter $(f)) \ --template=$(html_template) $< $@ @@ -66,7 +71,7 @@ $(subindices): index_options = --site-title="$$(cat $(title))" # ⚠ When tweaking this rule, check whether it still works for the # top-level index.html. $(indices): $(OUT_DIR)/%index.html: - $(call v,INDEX,$*) EXTENSIONS="$(EXTENSIONS)" \ + $(call v,INDEX,$*) \ ./generate-index.py $(index_options) --template=$(html_template) \ $(foreach f,$(lua_filters),--lua-filter $(f)) \ $(site_tree) "$(patsubst %/,%,$*)" $@ diff --git a/repo/www/convert-internal-links.lua b/repo/www/convert-internal-links.lua index e50352a..21054d1 100644 --- a/repo/www/convert-internal-links.lua +++ b/repo/www/convert-internal-links.lua @@ -1,10 +1,4 @@ -EXTENSIONS = {} - -string.gsub( - os.getenv("EXTENSIONS"), - "[^%s]+", - function (ext) EXTENSIONS[#EXTENSIONS+1] = ext end -) +local config = require 'config' function Link(link) if link.target:match("^[%w]+://") @@ -12,7 +6,7 @@ function Link(link) return link end - for _, ext in pairs(EXTENSIONS) + for _, ext in pairs(config.EXTENSIONS) do link.target = link.target:gsub("%."..ext.."$", ".html") end diff --git a/repo/www/generate-lua-config.py b/repo/www/generate-lua-config.py new file mode 100755 index 0000000..0a00443 --- /dev/null +++ b/repo/www/generate-lua-config.py @@ -0,0 +1,34 @@ +#!/usr/bin/env python3 + +from sys import argv + + +TEMPLATE = '''\ +local config = {{}} + +config.EXTENSIONS = {{ {EXTENSIONS} }} + +return config +''' + + +def _quote(s): + return f"'{s}'" + + +def main(arguments): + pairs = (arg.split('=') for arg in arguments) + + formatters = { + 'EXTENSIONS': lambda v: ', '.join(map(_quote, v.split())) + } + + parameters = { + key: formatters[key](value) for (key, value) in pairs + } + + print(TEMPLATE.format_map(parameters), end='') + + +if __name__ == '__main__': + main(argv[1:]) diff --git a/repo/www/helpers.py b/repo/www/helpers.py index 434ef6c..f76fa02 100644 --- a/repo/www/helpers.py +++ b/repo/www/helpers.py @@ -1,7 +1,7 @@ from collections import defaultdict from dataclasses import dataclass, field from itertools import chain -from os import path +from os import environ, path from subprocess import run from typing import Iterator @@ -67,4 +67,5 @@ def pandoc(page, output, template, filters, title=None, site_title=None): if site_title is not None: cmd += ('-T', site_title) + environ['LUA_PATH'] = '.cache/?.lua;;' run(cmd, check=True) |
