From 05aa18420ff83ccdd03d9956b0ff65e659198826 Mon Sep 17 00:00:00 2001 From: Albert Krewinkel Date: Sat, 21 Sep 2024 08:54:05 +0200 Subject: Lua: update List module The module now comes with a method `:at(index[, def])` that allows to access indices, accepts negative indices to count from the end, and will return the `def` value as a default if the list has no item at the given position. Furthermore, the list constructor `pandoc.List` now accepts iterators. E.g., `pandoc.List(text:gmatch '%S+')` returns the list of words in `text`. Closes: #9835 --- doc/lua-filters.md | 25 +++++++++++++++++++++++++ pandoc-lua-engine/pandoc-lua-engine.cabal | 2 +- stack.yaml | 1 + 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/doc/lua-filters.md b/doc/lua-filters.md index 9de10cfbf..f445f6327 100644 --- a/doc/lua-filters.md +++ b/doc/lua-filters.md @@ -4578,6 +4578,28 @@ Returns: ## Methods +### `pandoc.List:at` {#pandoc.list:at} + +`:at (index[, default])` + +Returns the element at the given index, or `default` if the list +contains no item at the given position. + +Negative integers count back from the last item in the list. + +Parameters: + +`index` +: element position (integer) + +`default` +: the default value that is returned if the index is out of + range (any) + +Returns: + +- the list item at `index`, or `default`. + ### `pandoc.List:clone ()` {#pandoc.list:clone} Returns a (shallow) copy of the list. (To get a deep copy @@ -4683,6 +4705,9 @@ Parameters: Create a new List. If the optional argument `table` is given, set the metatable of that value to `pandoc.List`. +The function also accepts an iterator, in which case it creates a +new list from the return values of the iterator function. + Parameters: `table` diff --git a/pandoc-lua-engine/pandoc-lua-engine.cabal b/pandoc-lua-engine/pandoc-lua-engine.cabal index 44461adf4..34a045bca 100644 --- a/pandoc-lua-engine/pandoc-lua-engine.cabal +++ b/pandoc-lua-engine/pandoc-lua-engine.cabal @@ -125,7 +125,7 @@ library , lpeg >= 1.1 && < 1.2 , mtl >= 2.2 && < 2.4 , pandoc >= 3.4 && < 3.5 - , pandoc-lua-marshal >= 0.2.7 && < 0.3 + , pandoc-lua-marshal >= 0.2.8 && < 0.3 , pandoc-types >= 1.22 && < 1.24 , parsec >= 3.1 && < 3.2 , text >= 1.1.1 && < 2.2 diff --git a/stack.yaml b/stack.yaml index 5b126f5be..69c90d348 100644 --- a/stack.yaml +++ b/stack.yaml @@ -22,6 +22,7 @@ extra-deps: - typst-0.6 # even though the resolver has this version, we need the Hackage revision: - hslua-module-doclayout-1.1.1.2 +- hslua-list-1.1.2 - git: https://github.com/jgm/commonmark-hs subdirs: [commonmark, commonmark-pandoc] commit: 7130ce27089b47588f64946263defc05fd72ab29 -- cgit v1.2.3