Skip to content

Commit dbb697c

Browse files
committed
Add attribute checks
1 parent 3eb53a3 commit dbb697c

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

src/resources/filters/main.lua

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,8 @@ import("./quarto-init/metainit.lua")
196196

197197
-- [/import]
198198

199+
_quarto.modules.attribcheck.enable_attribute_checks()
200+
199201
initCrossrefIndex()
200202

201203
initShortcodeHandlers()
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
local io = require 'io'
2+
local pandoc = require 'pandoc'
3+
local utils = require 'pandoc.utils'
4+
local ptype = utils.type
5+
6+
local InlinesMT = debug.getmetatable(pandoc.Inlines{})
7+
local BlocksMT = debug.getmetatable(pandoc.Blocks{})
8+
9+
local function warn_conversion (expected, actual)
10+
-- local dbginfo = debug.getinfo(5, 'Sln')
11+
-- warn(actual .. ' instead of ' .. expected .. ': ' ..
12+
-- dbginfo.name .. ' in ' .. dbginfo.source .. ':' ..
13+
-- dbginfo.currentline)
14+
return
15+
end
16+
17+
local function ensure_inlines(obj)
18+
local pt = ptype(obj)
19+
if pt == 'Inlines' then
20+
return obj
21+
elseif pt == 'List' or pt == 'table' then
22+
warn_conversion('Inlines', pt)
23+
return setmetatable(obj, InlinesMT)
24+
else
25+
warn_conversion('Inlines', pt)
26+
return pandoc.Inlines(obj)
27+
end
28+
end
29+
30+
local function ensure_blocks(obj)
31+
local pt = ptype(obj)
32+
if pt == 'Blocks' then
33+
return obj
34+
elseif pt == 'List' or pt == 'table' then
35+
warn_conversion('Blocks', pt)
36+
return setmetatable(obj, BlocksMT)
37+
elseif pt == 'Inlines' then
38+
warn_conversion('Blocks', pt)
39+
return setmetatable({pandoc.Plain(obj)}, BlocksMT)
40+
else
41+
warn_conversion('Blocks', pt)
42+
return pandoc.Blocks(obj)
43+
end
44+
end
45+
46+
local InlineMT = debug.getmetatable(pandoc.Space())
47+
local BlockMT = debug.getmetatable(pandoc.HorizontalRule())
48+
local default_setter = InlineMT.setters.content
49+
50+
local function enable_attribute_checks()
51+
InlineMT.setters.content = function (obj, key, value)
52+
if obj.tag == 'Note' then
53+
default_setter(obj, key, ensure_blocks(value))
54+
else
55+
default_setter(obj, key, ensure_inlines(value))
56+
end
57+
end
58+
BlockMT.setters.content = function (obj, key, value)
59+
local tag = obj.tag
60+
if tag == 'Para' or tag == 'Plain' or tag == 'Header' then
61+
default_setter(obj, key, ensure_inlines(value))
62+
elseif tag == 'Div' or tag == 'BlockQuote' or tag == 'Figure' then
63+
default_setter(obj, key, ensure_blocks(value))
64+
else
65+
default_setter(obj, key, value)
66+
end
67+
end
68+
end
69+
70+
return {
71+
enable_attribute_checks = enable_attribute_checks
72+
}

src/resources/filters/modules/import_all.lua

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
_quarto.modules = {
55
astshortcode = require("modules/astshortcode"),
6+
attribcheck = require("modules/attribcheck"),
67
authors = require("modules/authors"),
78
brand = require("modules/brand/brand"),
89
callouts = require("modules/callouts"),

0 commit comments

Comments
 (0)