Skip to content

Commit

Permalink
Use Inlines metatable for "list of Inline" return values
Browse files Browse the repository at this point in the history
  • Loading branch information
tarleb committed Nov 27, 2024
1 parent 9794066 commit 48bb28b
Showing 1 changed file with 11 additions and 12 deletions.
23 changes: 11 additions & 12 deletions src/resources/filters/common/pandoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ function inlinesToString(inlines)
return pandoc.utils.stringify(pandoc.Span(inlines))
end

local InlinesMT = getmetatable(pandoc.Inlines{})

-- lua string to pandoc inlines
function stringToInlines(str)
if str then
return pandoc.Inlines({pandoc.Str(str)})
return setmetatable({pandoc.Str(str)}, InlinesMT)
else
return pandoc.Inlines({})
return setmetatable({}, InlinesMT)
end
end

Expand All @@ -98,27 +100,24 @@ end
function markdownToInlines(str)
if str then
local doc = pandoc.read(str)
if #doc.blocks == 0 then
return pandoc.List({})
else
return doc.blocks[1].content
end
return pandoc.utils.blocks_to_inlines(doc.blocks)
else
return pandoc.List()
return setmetatable({}, InlinesMT)
end
end


function stripTrailingSpace(inlines)
-- we always convert to pandoc.List to ensure a uniform
-- we always convert to pandoc.Inlines to ensure a uniform
-- return type (and its associated methods)
if #inlines > 0 then
if inlines[#inlines].t == "Space" then
return pandoc.List(tslice(inlines, 1, #inlines - 1))
return setmetatable(tslice(inlines, 1, #inlines - 1), InlinesMT)
else
return pandoc.List(inlines)
return setmetatable(inlines, InlinesMT)
end
else
return pandoc.List(inlines)
return setmetatable(inlines, InlinesMT)
end
end

Expand Down

0 comments on commit 48bb28b

Please sign in to comment.