Skip to content

Commit ab7963e

Browse files
committed
Use functions supplied by pandoc when possible
1 parent 6de2ca7 commit ab7963e

File tree

3 files changed

+15
-44
lines changed

3 files changed

+15
-44
lines changed

Diff for: src/resources/filters/ast/emulatedfilter.lua

+4-8
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,9 @@ make_wrapped_user_filters = function(filterListName)
3737
end
3838

3939
inject_user_filters_at_entry_points = function(filter_list)
40-
local function find_index_of_entry_point(entry_point)
41-
for i, filter in ipairs(filter_list) do
42-
if filter.name == entry_point then
43-
return i
44-
end
45-
end
46-
return nil
40+
local find_index_of_entry_point = function (entry_point)
41+
return select(2, pandoc.List.find_if(filter_list,
42+
function (f) return f.name == entry_point end))
4743
end
4844
local entry_point_counts = {}
4945
for _, v in ipairs(param("quarto-filters").entryPoints) do
@@ -76,4 +72,4 @@ inject_user_filters_at_entry_points = function(filter_list)
7672
end
7773
table.insert(filter_list, index, filter)
7874
end
79-
end
75+
end

Diff for: src/resources/filters/common/list.lua

+1-10
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,4 @@
11
-- list.lua
22
-- Copyright (C) 2020-2022 Posit Software, PBC
33

4-
function filter(list, test)
5-
local result = {}
6-
for index, value in ipairs(list) do
7-
if test(value, index) then
8-
result[#result + 1] = value
9-
end
10-
end
11-
return result
12-
end
13-
4+
filter = pandoc.List.filter

Diff for: src/resources/filters/common/table.lua

+10-26
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,14 @@
22
-- Copyright (C) 2020-2022 Posit Software, PBC
33

44
-- append values to table
5-
function tappend(t, values)
6-
for i,value in pairs(values) do
7-
table.insert(t, value)
8-
end
9-
end
5+
tappend = pandoc.List.extend
106

117
-- prepend values to table
128
function tprepend(t, values)
13-
for i=1, #values do
14-
table.insert(t, 1, values[#values + 1 - i])
15-
end
9+
local nvals = #values
10+
table.move(t, 1, #t, nvals + 1) -- shift elements to make space
11+
table.move(values, 1, nvals, 1, t) -- copy values into t
12+
return t
1613
end
1714

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

4138
-- map elements of a table
42-
function tmap(tbl, f)
43-
local t = {}
44-
for k,v in pairs(tbl) do
45-
t[k] = f(v)
46-
end
47-
return t
48-
end
39+
tmap = pandoc.List.map
4940

5041
-- does the table contain a value
5142
function tcontains(t,value)
5243
if t and type(t)=="table" and value then
53-
for _, v in ipairs (t) do
54-
if v == value then
55-
return true
56-
end
57-
end
58-
return false
44+
return pandoc.List.includes(t, value)
5945
end
6046
return false
6147
end
6248

6349
-- clear a table
6450
function tclear(t)
65-
for k,v in pairs(t) do
51+
for k,_ in pairs(t) do
6652
t[k] = nil
6753
end
6854
end
6955

7056
-- get keys from table
7157
function tkeys(t)
7258
local keyset=pandoc.List({})
73-
local n=0
74-
for k,v in pairs(t) do
75-
n=n+1
76-
keyset[n]=k
59+
for key in pairs(t) do
60+
keyset:insert(key)
7761
end
7862
return keyset
7963
end

0 commit comments

Comments
 (0)