summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaul Buetow <paul@buetow.org>2026-04-26 19:22:29 +0300
committerPaul Buetow <paul@buetow.org>2026-04-26 19:22:29 +0300
commit16b1ed918f4294302cf854c8041e1feb90da88bb (patch)
tree984eaab47ef7c6e6f4b5f92088e6248153d3537a
parenta38561d466f71bd5a683aa0c601e2331de3c3120 (diff)
Add template::dynamic directive for always-regenerate templates
Templates that fetch data from external sources (e.g. via curl) need to bypass the mtime-based freshness check. Adding << template::dynamic to a .gmi.tpl file marks it for regeneration on every run without affecting the generated output. Amp-Thread-ID: https://ampcode.com/threads/T-019dca93-8625-7134-bd59-8fc52e3a4b9c Co-authored-by: Amp <amp@ampcode.com>
-rw-r--r--README.md15
-rw-r--r--lib/template.source.sh12
2 files changed, 26 insertions, 1 deletions
diff --git a/README.md b/README.md
index 8db3e5d..4c93025 100644
--- a/README.md
+++ b/README.md
@@ -220,6 +220,21 @@ Table of contents generation can be done in any template file. Just add:
<< template::inline::toc
```
+### Dynamic templates
+
+By default, Gemtexter skips template regeneration when the output `.gmi` file is newer than all `.gmi.tpl` files in the same directory. Templates that fetch data from external sources (e.g. via `curl`) need to be regenerated on every run regardless of file timestamps. Add `<< template::dynamic` to such a template (typically as the first line) to mark it as always-regenerate:
+
+```
+<< template::dynamic
+# My dynamic page
+
+<<<
+ curl -fsS https://example.com/data
+>>>
+```
+
+The directive produces no output and does not affect the generated content.
+
### Alternative configuration file path
If you don't want to mess with `gemtexter.conf`, you can use an alternative config file path in `~/.config/gemtexter.conf`, which takes precedence if it exists. Another way is to set the `CONFIG_FILE_PATH` environment variable, e.g.:
diff --git a/lib/template.source.sh b/lib/template.source.sh
index 0e34a87..80d1fc8 100644
--- a/lib/template.source.sh
+++ b/lib/template.source.sh
@@ -51,8 +51,14 @@ template::_generate_file () {
local -r tpl="$(basename "$tpl_path")"
local -r dest="${tpl/.tpl/}"
+ # Templates containing template::dynamic are always regenerated (e.g. they
+ # fetch data from external sources whose freshness cannot be determined by
+ # file mtime alone).
+ local -i is_dynamic=0
+ $GREP -q 'template::dynamic' "$tpl_path" && is_dynamic=1
+
# Skip if output is newer than the template and all relevant siblings
- if [[ "$FORCE_REBUILD" != yes ]] && [[ -f "$tpl_dir/$dest" ]]; then
+ if (( ! is_dynamic )) && [[ "$FORCE_REBUILD" != yes ]] && [[ -f "$tpl_dir/$dest" ]]; then
local -r dest_mtime=$(stat -c '%Y' "$tpl_dir/$dest")
local -r dir_newest=$(template::_dir_newest_mtime "$tpl_dir")
if (( dest_mtime >= dir_newest )); then
@@ -182,6 +188,10 @@ template::inline::toc () {
'
}
+template::dynamic () {
+ :
+}
+
template::test () {
assert::equals "$(template::_line '<< echo -n foo')" 'foo'
assert::equals "$(template::_line '<< echo foo')" 'foo'