Skip to content

Commit

Permalink
Add attribute checks
Browse files Browse the repository at this point in the history
  • Loading branch information
tarleb committed Oct 24, 2024
1 parent 3eb53a3 commit dbb697c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/resources/filters/main.lua
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,8 @@ import("./quarto-init/metainit.lua")

-- [/import]

_quarto.modules.attribcheck.enable_attribute_checks()

initCrossrefIndex()

initShortcodeHandlers()
Expand Down
72 changes: 72 additions & 0 deletions src/resources/filters/modules/attribcheck.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
local io = require 'io'
local pandoc = require 'pandoc'
local utils = require 'pandoc.utils'
local ptype = utils.type

local InlinesMT = debug.getmetatable(pandoc.Inlines{})
local BlocksMT = debug.getmetatable(pandoc.Blocks{})

local function warn_conversion (expected, actual)
-- local dbginfo = debug.getinfo(5, 'Sln')
-- warn(actual .. ' instead of ' .. expected .. ': ' ..
-- dbginfo.name .. ' in ' .. dbginfo.source .. ':' ..
-- dbginfo.currentline)
return
end

local function ensure_inlines(obj)
local pt = ptype(obj)
if pt == 'Inlines' then
return obj
elseif pt == 'List' or pt == 'table' then
warn_conversion('Inlines', pt)
return setmetatable(obj, InlinesMT)
else
warn_conversion('Inlines', pt)
return pandoc.Inlines(obj)
end
end

local function ensure_blocks(obj)
local pt = ptype(obj)
if pt == 'Blocks' then
return obj
elseif pt == 'List' or pt == 'table' then
warn_conversion('Blocks', pt)
return setmetatable(obj, BlocksMT)
elseif pt == 'Inlines' then
warn_conversion('Blocks', pt)
return setmetatable({pandoc.Plain(obj)}, BlocksMT)
else
warn_conversion('Blocks', pt)
return pandoc.Blocks(obj)
end
end

local InlineMT = debug.getmetatable(pandoc.Space())
local BlockMT = debug.getmetatable(pandoc.HorizontalRule())
local default_setter = InlineMT.setters.content

local function enable_attribute_checks()
InlineMT.setters.content = function (obj, key, value)
if obj.tag == 'Note' then
default_setter(obj, key, ensure_blocks(value))
else
default_setter(obj, key, ensure_inlines(value))
end
end
BlockMT.setters.content = function (obj, key, value)
local tag = obj.tag
if tag == 'Para' or tag == 'Plain' or tag == 'Header' then
default_setter(obj, key, ensure_inlines(value))
elseif tag == 'Div' or tag == 'BlockQuote' or tag == 'Figure' then
default_setter(obj, key, ensure_blocks(value))
else
default_setter(obj, key, value)
end
end
end

return {
enable_attribute_checks = enable_attribute_checks
}
1 change: 1 addition & 0 deletions src/resources/filters/modules/import_all.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

_quarto.modules = {
astshortcode = require("modules/astshortcode"),
attribcheck = require("modules/attribcheck"),
authors = require("modules/authors"),
brand = require("modules/brand/brand"),
callouts = require("modules/callouts"),
Expand Down

0 comments on commit dbb697c

Please sign in to comment.