Module:TemplateHelpers: Difference between revisions
// via Wikitext Extension for VSCode |
// via Wikitext Extension for VSCode |
||
| Line 80: | Line 80: | ||
local CanonicalForms = require('Module:CanonicalForms') | local CanonicalForms = require('Module:CanonicalForms') | ||
local NormalizationText = require('Module:NormalizationText') | local NormalizationText = require('Module:NormalizationText') | ||
local ListGeneration = require('Module:ListGeneration') | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Line 453: | Line 454: | ||
local key, value = p.getFieldValue(args, field) | local key, value = p.getFieldValue(args, field) | ||
if value then | if value then | ||
local continue = false | |||
local | |||
-- | -- Handle declarative lists first | ||
if field.list then | |||
local items = NormalizationText.splitMultiValueString(value) | |||
if #items > 0 then | |||
local options = {} | |||
if type(field.list) == 'string' then | |||
options.mode = field.list | |||
elseif type(field.list) == 'table' then | |||
for k, v in pairs(field.list) do | |||
options[k] = v | |||
end | |||
end | |||
if field.autoWikiLink then | |||
local originalHook = options.itemHook | |||
options.itemHook = function(item) | |||
if type(originalHook) == 'function' then | |||
item = originalHook(item) | |||
end | |||
local itemContent = type(item) == 'table' and item.content or item | |||
if itemContent and itemContent ~= '' then | |||
local link = '[[' .. itemContent .. ']]' | |||
if type(item) == 'table' then | |||
item.content = link | |||
return item | |||
else | |||
return link | |||
end | |||
end | |||
return item | |||
end | |||
end | |||
local listOutput = ListGeneration.createList(items, options) | |||
out[outIndex] = string.format(FIELD_FORMAT, field.label, listOutput) | |||
outIndex = outIndex + 1 | |||
continue = true | |||
end | end | ||
end | end | ||
if not continue then | |||
-- Create sanitization options | |||
local sanitizeOptions = { | |||
preserveWikiLinks = field.autoWikiLink or field.preserveWikiLinks | |||
} | |||
-- Get property name for this field if available (case-insensitive) | |||
local propertyName = nil | |||
for propName, fieldName in pairs(propertyMappings) do | |||
if key and (fieldName == key or tostring(fieldName):lower() == tostring(key):lower()) then | |||
-- | propertyName = propName | ||
local | break | ||
end | |||
end | |||
-- Get tooltip text if property exists | |||
local tooltipText = "" | |||
if propertyName then | |||
tooltipText = require('Module:SemanticCategoryHelpers').getPropertyDescription(propertyName) or "" | |||
end | |||
-- | -- Prepare tooltip attributes if tooltip text exists | ||
local tooltipClass = "" | |||
local tooltipAttr = "" | |||
if tooltipText and tooltipText ~= "" then | |||
-- Escape quotes in tooltip text to prevent HTML attribute issues | |||
local escapedTooltip = tooltipText:gsub('"', '"') | |||
tooltipClass = " has-tooltip" | |||
tooltipAttr = string.format('data-tooltip="%s"', escapedTooltip) | |||
end | |||
-- Handle the case where a processor returns complete HTML | -- Apply processor if available for this field | ||
if key and processors[key] and type(processors[key]) == "function" then | |||
local processedValue = processors[key](value, args) | |||
-- Preserve wiki links if needed | |||
processedValue = linkParser.preserveWikiLinks( | |||
value, | |||
processedValue, | |||
sanitizeOptions.preserveWikiLinks | |||
) | |||
-- Handle the case where a processor returns complete HTML | |||
if type(processedValue) == "table" and processedValue.isCompleteHtml then | |||
-- Add the complete HTML as is | |||
out[outIndex] = processedValue.html | |||
outIndex = outIndex + 1 | |||
elseif processedValue ~= nil and processedValue ~= false then | |||
-- Apply wiki link handling | |||
processedValue = linkParser.applyWikiLinkHandling(processedValue, field) | |||
-- Standard field rendering with tooltip | |||
out[outIndex] = string.format(FIELD_FORMAT_WITH_TOOLTIP, | |||
tooltipClass, tooltipAttr, field.label, processedValue) | |||
outIndex = outIndex + 1 | |||
end | |||
else | |||
-- Standard field rendering without processor | |||
-- Apply sanitization with preserveWikiLinks option if needed | |||
local finalValue | |||
if sanitizeOptions.preserveWikiLinks then | |||
finalValue = value | |||
else | |||
finalValue = p.sanitizeUserInput(value, nil, nil, sanitizeOptions) | |||
end | |||
-- Apply wiki link handling | -- Apply wiki link handling | ||
finalValue = linkParser.applyWikiLinkHandling(finalValue, field) | |||
-- | -- Use format with tooltip | ||
out[outIndex] = string.format(FIELD_FORMAT_WITH_TOOLTIP, | out[outIndex] = string.format(FIELD_FORMAT_WITH_TOOLTIP, | ||
tooltipClass, tooltipAttr, field.label, | tooltipClass, tooltipAttr, field.label, finalValue) | ||
outIndex = outIndex + 1 | outIndex = outIndex + 1 | ||
end | end | ||
end | end | ||
end | end | ||