pandoc / lua-filters

A collection of lua filters for pandoc
MIT License
602 stars 165 forks source link

Add an include-meta LUA filter #196

Closed mfhepp closed 2 years ago

mfhepp commented 2 years ago

I am moving this from the discussion on pandoc-groups to here:

It would be great to be able to define the set of design meta-data in the YAML header of an document. The external filter pandoc-include provides this, but adds a new dependency and requires the include directive to be outside the YAML header block (which I think is suboptimal).

Ideally, we would have a LUA filter like include-meta that does something similar, i.e. allow specifying a single YAML file path or a list of those and adding the respective meta-data to the document meta-data.

So for instance, one would use include-meta in the YAML header of the document, like so:

title: The title
include-meta: acme-design-settings.yaml

or

title: The title
include-meta: 
  - acme-design-settings.yaml
  - my-personal-meta-data.yaml

That would nicely complement the two other include filters:

The LUA filter for include-meta would basically follow this algorithm:

# pseudo-code
meta = document.meta
if meta.include-meta is not nil:
   for each filename in meta.include-meta:
      additional_meta = load_and_parse_yaml(filename)
       for each entry in additional_meta:
           if meta[entry.key] is nil:
              meta[entry.key] = entry.value
                # recursion not shown in here, needed for nested YAML                
           else:
              if len(meta[entry.key]) == 1:
                  meta[entry.key] = list(meta[entry.key])
                  if len(entry.key) == 1:
                     entry.key = list(entry.key)
                  meta[entry.key].extend(entry.key)

The pseudocode is very sketchy in that the recursive addition of nested meta-data is not shown. Maybe there is an easy way to merge two or more Pandoc Meta objects and observe priority rules.

I have never used LUA before; I started sketching this out at

https://github.com/mfhepp/lua-filters/tree/master/include-meta

If anybody fluent with LUA is willing to work on this or help with advice, please do not hesitate :-)

tarleb commented 2 years ago

Reason that this was closed: https://github.com/pandoc/lua-filters/pull/197#issuecomment-1049137859