Skip to content

Commit 6b2028b

Browse files
committed
lua - fix conditional content for divs with repeated attributes
1 parent 6e5b32f commit 6b2028b

File tree

1 file changed

+41
-20
lines changed

1 file changed

+41
-20
lines changed

src/resources/filters/customnodes/content-hidden.lua

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ _quarto.ast.add_handler({
3636
local behavior = div.classes:find(constants.kContentVisible) or div.classes:find(constants.kContentHidden)
3737
local condition = pandoc.List({})
3838
local remaining_attributes = pandoc.List({})
39+
quarto.utils.dump(div.attributes)
3940
for i, v in ipairs(div.attributes) do
4041
if kConditions:find(v[1]) ~= nil then
4142
condition:insert(v)
@@ -79,7 +80,10 @@ _quarto.ast.add_handler({
7980
error("Ignoring invalid condition in conditional block: " .. v[1])
8081
-- luacov: enable
8182
else
82-
result.condition[v[1]] = v[2]
83+
if result.condition[v[1]] == nil then
84+
result.condition[v[1]] = pandoc.List({})
85+
end
86+
result.condition[v[1]]:insert(v[2])
8387
end
8488
end
8589

@@ -152,30 +156,47 @@ end
152156
-- "properties" here will come either from "conditions", in the case of a custom AST node
153157
-- or from the attributes of the element itself in the case of spans or codeblocks
154158
function propertiesMatch(properties, profiles)
155-
local match = true
156-
if properties[constants.kWhenMeta] ~= nil then
157-
local v = properties[constants.kWhenMeta]
158-
v = split(v, ".") or { v }
159-
local r = get_meta(v)
160-
match = match and (type(r) == "boolean" and r)
161-
end
162-
if properties[constants.kUnlessMeta] ~= nil then
163-
local v = properties[constants.kUnlessMeta]
159+
local function check_meta(value)
160+
local v = properties[value]
164161
v = split(v, ".") or { v }
165162
local r = get_meta(v)
166-
match = match and not (type(r) == "boolean" and r)
167-
end
168-
if properties[constants.kWhenFormat] ~= nil then
169-
match = match and _quarto.format.isFormat(properties[constants.kWhenFormat])
163+
return type(r) == "boolean" and r
170164
end
171-
if properties[constants.kUnlessFormat] ~= nil then
172-
match = match and not _quarto.format.isFormat(properties[constants.kUnlessFormat])
165+
local function check_profile(value)
166+
return profiles:includes(value)
173167
end
174-
if properties[constants.kWhenProfile] ~= nil then
175-
match = match and profiles:includes(properties[constants.kWhenProfile])
168+
local function check_property(key, f)
169+
local v = properties[key]
170+
if type(v) == "string" then
171+
return f(key)
172+
elseif type(v) == "table" then
173+
local r = false
174+
for _, value in ipairs(v) do
175+
r = r or f(value)
176+
end
177+
return r
178+
else
179+
-- luacov: disable
180+
error("Invalid value type for condition: " .. type(v))
181+
-- luacov: enable
182+
end
176183
end
177-
if properties[constants.kUnlessProfile] ~= nil then
178-
match = match and not profiles:includes(properties[constants.kUnlessProfile])
184+
local tests = {
185+
{ constants.kWhenMeta, check_meta, false },
186+
{ constants.kUnlessMeta, check_meta, true },
187+
{ constants.kWhenFormat, _quarto.format.isFormat, false },
188+
{ constants.kUnlessFormat, _quarto.format.isFormat, true },
189+
{ constants.kWhenProfile, check_profile, false },
190+
{ constants.kUnlessProfile, check_profile, true }
191+
}
192+
local match = true
193+
for _, test in ipairs(tests) do
194+
local key = test[1]
195+
local f = test[2]
196+
local invert = test[3]
197+
if properties[key] ~= nil then
198+
match = match and (invert ~= check_property(key, f))
199+
end
179200
end
180201
return match
181202
end

0 commit comments

Comments
 (0)