Module:TemplateHelpers: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 14: | Line 14: | ||
-- String Processing Functions | -- String Processing Functions | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- Normalizes template arguments to be case-insensitive | |||
-- Returns a new table with lowercase keys while preserving original keys as well | |||
function p.normalizeArgumentCase(args) | |||
local normalized = {} | |||
-- Process all keys | |||
for key, value in pairs(args) do | |||
-- Add lowercase version | |||
normalized[key:lower()] = value | |||
-- Preserve original key as well | |||
normalized[key] = value | |||
end | |||
return normalized | |||
end | |||
-- Trims leading and trailing whitespace from a string | -- Trims leading and trailing whitespace from a string | ||
| Line 41: | Line 57: | ||
-- Retrieves a field value from args using either multiple possible keys or a single key | -- Retrieves a field value from args using either multiple possible keys or a single key | ||
-- Now supports case-insensitive lookup by default | |||
function p.getFieldValue(args, field) | function p.getFieldValue(args, field) | ||
if field.keys then | if field.keys then | ||
for _, key in ipairs(field.keys) do | for _, key in ipairs(field.keys) do | ||
-- First try exact match to maintain backward compatibility | |||
if args[key] and args[key] ~= "" then | if args[key] and args[key] ~= "" then | ||
return key, args[key] | return key, args[key] | ||
end | |||
-- Then try lowercase version | |||
local lowerKey = key:lower() | |||
if args[lowerKey] and args[lowerKey] ~= "" and lowerKey ~= key then | |||
return lowerKey, args[lowerKey] | |||
end | end | ||
end | end | ||
return nil, nil | return nil, nil | ||
end | end | ||
return field.key, | |||
-- First try exact match | |||
if args[field.key] and args[field.key] ~= "" then | |||
return field.key, args[field.key] | |||
end | |||
-- Then try lowercase version | |||
local lowerKey = field.key:lower() | |||
if args[lowerKey] and args[lowerKey] ~= "" and lowerKey ~= field.key then | |||
return lowerKey, args[lowerKey] | |||
end | |||
return field.key, nil | |||
end | end | ||