quarto-dev / quarto-shortcodes

GNU General Public License v2.0
8 stars 2 forks source link

Processing lists in lua filters [question] #2

Open retostauffer opened 2 years ago

retostauffer commented 2 years ago

Goal: I am currently working on a shortcode for bootstrap carousel. To allow to define images, alt, and captions I try to configure it via .yml list.

My test index.qmd looks as follows:

---
title: "My Website"
carousel:
    - image: _images/unsplash-1.jpg
      alt: foooo
    - image: _images/unsplash-2.jpg
      alt: foooo
reto: "Just a test string"
---

My lua script (carousel.lua; shortcode extension) in a minimalized version looks as follows:

carousel = function(args, kwargs, meta)
    -- extracting Str from meta
    print(pandoc.utils.stringify(meta["reto"]))
    -- extracting List from meta
    print(pandoc.utils.stringify(meta["carousel"]))
    -- dummy return
    return pandoc.RawInline("html", "test")
end

return { ["carousel"] = carousel }

The problem: Processing meta["reto"] works as expected, however, meta["carousel"] (List) gives a WARNING: Unsupported type 'List' for key carousel.

Question: Is it somehow possible to process lists or a better way to access the meta needed, or is this just not possible with quarto/lua?

Any hint would be warmly welcome. Thanks in advance!

dragonstyle commented 2 years ago

One suggestion- have you returned a pandoc.List

https://pandoc.org/lua-filters.html#module-pandoc.list

Let us know if that works if not I can take a closer look!

retostauffer commented 2 years ago

Hy @dragonstyle

thanks for your quick reply! I've tried to call pandoc.List(meta.carousel) already with the same result. The warning is thrown as soon as meta.carousel (the list) is accessed. Rendering still works, but no access to the meta info.

I have put together a minimal quarto website project which contains the code (slightly modified) as shown above if that helps.

Not urgent/important at all, but would be nice to be able to do this in the future. Thanks in advance!

dragonstyle commented 2 years ago

Thank you this is really helpful - I can reproduce the issue and will take a look!

nichtich commented 2 years ago

I encountered a similar problem: quarto shortcodes cannot access full metadata hierarchy unless the hierarchy of a nested key is known, so iterating all values of carousel is not possible. My solution was to not use shortcode library but a plain Pandoc lua filter.

retostauffer commented 2 years ago

Thanks @nichtich, I will have a look at your filter! Thanks. In the mean time I also wrote a filter which injects jQuery/HTML.