htdebeer / pandocomatic

Automate the use of pandoc
https://heerdebeer.org/Software/markdown/pandocomatic/
GNU General Public License v3.0
158 stars 14 forks source link

feature: specify lua-filter-dir via environment variable #103

Closed ppenguin closed 2 years ago

ppenguin commented 2 years ago

I found myself unable to use lua-filters in a nix-shell, because the location of the lua-filters is not fixed.

I didn't find a way to refer to env vars within the yaml (am I correct that there is no way to have these substituted?), this didn't work:

lua-filter:
  - ${LUA_FILTER_DIR}/diagram-generator.lua

The only feasible way to pass such dynamic env info to pandocomatic would probably be to define an env var that can be used to specify the path here the lua-filters are installed, e.g. LUA_FILTER_DIR, otherwise I found that only the default data dir is considered.

(A workaround to symlink specified lua filters into the data dir is not feasible, because on every new instance of the env there is a high chance the path has changed (e.g. in case of a new version)).

htdebeer commented 2 years ago

I didn't find a way to refer to env vars within the yaml

Correct. This feature is not implemented in pandocomatic.

However, pandoc has implemented something like this with their "defaults" feature. Could you use that somehow to work around this limitation in pandocomatic?

Anyway, adding resolution for environment variables might be a nice addition.

ppenguin commented 2 years ago

defaults didn't work for me for expanding env vars, but a workaround that did is writing a lua-wrapper that "forwards" execution to the lua file found using env var expansion:

-- lua-filter-wrap.lua
-- symlink this to any filters you want to wrap, e.g.:
--   ln -s lua-filter-wrap.lua diagram-generator.lua
dofile(table.concat({os.getenv("LUA_FILTER_DIR"),debug.getinfo(1,'S').source:match("[^/]*.lua$")},"/"))

Placing this in ~/.pandoc/filters/ (along with the desired symlinks to it) and calling with lua-filter: filters/diagram-generator.lua now forwards execution to ${LUA_FILTER_DIR}/diagram-generator.lua

htdebeer commented 2 years ago

I've finished adding environment variable substitution to pandocomatic templates. It'll be available in the next version of pandocomatic. You can already test it by checking out the master branch, and use test/pandocomatic.rb as the pandocomatic command.

You can use environment variables in your templates to make them more flexible. For example, if you want to vary the output format depending on the value of environment variable OUTPUT_FORMAT, add the following format property to your template:

pandocomatic_:
  # ...
  pandoc:
    # ...
    format: $(OUTPUT_FORMAT)$

When pandocomatic reads this template, it replaces $(OUTPUT_FORMAT)$ with the value of the environment variable OUTPUT_FORMAT. If no such variable exists, pandocomatic stops after printing an error message. Pandocomatic tells you in which template it encountered this non-existing environment variable so you can easily investigate and resolve the issue.

In other words, all occurrences of $(X)$ in an internal or external template are replaced by the the value of environment variable X. You can use environment variables anywhere in your templates, as property names, keys, or values. You can even use it to add YAML snippets to your templates.

ppenguin commented 1 year ago

A very late but heartfelt thanks (I just stumled over this issue through an old reference, and noticed that I used this functionality without remembering any of this a few months back when completely moving my pandocomatic workflow to a nix flakes based env.)