Skip to content

Commit

Permalink
Use functions supplied by pandoc when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
tarleb committed Dec 12, 2024
1 parent 6de2ca7 commit ab7963e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 44 deletions.
12 changes: 4 additions & 8 deletions src/resources/filters/ast/emulatedfilter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,9 @@ make_wrapped_user_filters = function(filterListName)
end

inject_user_filters_at_entry_points = function(filter_list)
local function find_index_of_entry_point(entry_point)
for i, filter in ipairs(filter_list) do
if filter.name == entry_point then
return i
end
end
return nil
local find_index_of_entry_point = function (entry_point)
return select(2, pandoc.List.find_if(filter_list,
function (f) return f.name == entry_point end))
end
local entry_point_counts = {}
for _, v in ipairs(param("quarto-filters").entryPoints) do
Expand Down Expand Up @@ -76,4 +72,4 @@ inject_user_filters_at_entry_points = function(filter_list)
end
table.insert(filter_list, index, filter)
end
end
end
11 changes: 1 addition & 10 deletions src/resources/filters/common/list.lua
Original file line number Diff line number Diff line change
@@ -1,13 +1,4 @@
-- list.lua
-- Copyright (C) 2020-2022 Posit Software, PBC

function filter(list, test)
local result = {}
for index, value in ipairs(list) do
if test(value, index) then
result[#result + 1] = value
end
end
return result
end

filter = pandoc.List.filter
36 changes: 10 additions & 26 deletions src/resources/filters/common/table.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,14 @@
-- Copyright (C) 2020-2022 Posit Software, PBC

-- append values to table
function tappend(t, values)
for i,value in pairs(values) do
table.insert(t, value)
end
end
tappend = pandoc.List.extend

-- prepend values to table
function tprepend(t, values)
for i=1, #values do
table.insert(t, 1, values[#values + 1 - i])
end
local nvals = #values
table.move(t, 1, #t, nvals + 1) -- shift elements to make space
table.move(values, 1, nvals, 1, t) -- copy values into t
return t
end

-- slice elements out of a table
Expand All @@ -39,41 +36,28 @@ function tisarray(t)
end

-- map elements of a table
function tmap(tbl, f)
local t = {}
for k,v in pairs(tbl) do
t[k] = f(v)
end
return t
end
tmap = pandoc.List.map

-- does the table contain a value
function tcontains(t,value)
if t and type(t)=="table" and value then
for _, v in ipairs (t) do
if v == value then
return true
end
end
return false
return pandoc.List.includes(t, value)
end
return false
end

-- clear a table
function tclear(t)
for k,v in pairs(t) do
for k,_ in pairs(t) do
t[k] = nil
end
end

-- get keys from table
function tkeys(t)
local keyset=pandoc.List({})
local n=0
for k,v in pairs(t) do
n=n+1
keyset[n]=k
for key in pairs(t) do
keyset:insert(key)
end
return keyset
end
Expand Down

0 comments on commit ab7963e

Please sign in to comment.