Skip to content

Commit

Permalink
Fixed keyworded arguments in shortcodes
Browse files Browse the repository at this point in the history
Keyworded arguments are obtained as Pandoc Inlines objects, and we
parsed them to strings. However, in some cases (for example,
`first_use`, `insert_links`, ...), we want to get a boolean value.
When using a string, Lua often assumes a truthy value (if the string
is not empty, etc.), which is not what we want!

This commit adds a new helper function that converts common strings
(`"true"`, `"false"`, `"yes"`, `"no"`, `"y"`, `"n"`) into their
corresponding boolean value.
The shortcode functions are also updated to get their keyworded
arguments as boolean (by using this new helper) when necessary.

Also fixed the way we handle default values for such arguments:
we previously used `value = value or Options["value"]`, which works
fine with nil/non-nil values, but wrongly considers a `false` value
as an empty value (and replaces it with the default)!
We keep the `x = x or ...` for strings, but replace with an if for
boolean values.
  • Loading branch information
rchaput committed Feb 11, 2024
1 parent ec96053 commit e8b06bd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
24 changes: 24 additions & 0 deletions _extensions/acronyms/acronyms_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,28 @@ function Helpers.metadata_to_str(metadata)
end


-- Helper to convert a (case-insensitive) string to a boolean
-- Recognized values: `true`, `false`, `yes`, `no`, `y`, `n`
function Helpers.str_to_boolean(value)
local converts = {
["true"] = true,
["false"] = false,
["yes"] = true,
["no"] = false,
["y"] = true,
["n"] = false,
}
local result = converts[string.lower(value)]
if result == nil then
quarto.log.warning(
"[acronyms] Could not convert string to boolean, unrecognized value:",
value,
" ! Assuming `false`."
)
result = false
end
return result
end


return Helpers
2 changes: 1 addition & 1 deletion _extensions/acronyms/acronyms_pandoc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ function AcronymsPandoc.replaceExistingAcronym(acr_key, style, first_use, insert

-- Use default values from Options if not specified
style = style or Options["style"]
insert_links = insert_links or Options["insert_links"]
if insert_links == nil then insert_links = Options["insert_links"] end

-- Replace the acronym with the desired style
return replaceExistingAcronymWithStyle(
Expand Down
18 changes: 15 additions & 3 deletions _extensions/acronyms/shortcodes_acronyms.lua
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,18 @@ local function getOrNil (value)
end


--[[
Parse an optional value and convert it to a boolean if it was not `nil`.
--]]
local function getBooleanOrNil (value)
local value = getOrNil(value)
if value ~= nil then
value = Helpers.str_to_boolean(value)
end
return value
end


--[[
Define the "main" shortcode behaviour: replacing an acronym.
Expand All @@ -70,8 +82,8 @@ function replaceAcronym (args, kwargs, meta)
if Acronyms:contains(acronym_key) then
-- The acronym exists (and is recognized)
local style = getOrNil(kwargs["style"])
local first_use = getOrNil(kwargs["first_use"])
local insert_links = getOrNil(kwargs["insert_links"])
local first_use = getBooleanOrNil(kwargs["first_use"])
local insert_links = getBooleanOrNil(kwargs["insert_links"])
return AcronymsPandoc.replaceExistingAcronym(
acronym_key,
style,
Expand Down Expand Up @@ -102,7 +114,7 @@ function generateListOfAcronyms (args, kwargs, meta)

-- Parse (optional) keyworded-arguments
local sorting = getOrNil(kwargs["sorting"])
local include_unused = getOrNil(kwargs["include_unused"])
local include_unused = getBooleanOrNil(kwargs["include_unused"])
local title = getOrNil(kwargs["title"])
local header_classes = getOrNil(kwargs["header_classes"])

Expand Down

0 comments on commit e8b06bd

Please sign in to comment.