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
            -- Create sanitization options
             local continue = false
             local sanitizeOptions = {
                preserveWikiLinks = field.autoWikiLink or field.preserveWikiLinks
            }
              
              
             -- Get property name for this field if available (case-insensitive)
             -- Handle declarative lists first
            local propertyName = nil
            if field.list then
            for propName, fieldName in pairs(propertyMappings) do
                local items = NormalizationText.splitMultiValueString(value)
                if key and (fieldName == key or tostring(fieldName):lower() == tostring(key):lower()) then
                if #items > 0 then
                     propertyName = propName
                    local options = {}
                     break
                    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
              
 
            -- Get tooltip text if property exists
             if not continue then
            local tooltipText = ""
                -- Create sanitization options
            if propertyName then
                local sanitizeOptions = {
                tooltipText = require('Module:SemanticCategoryHelpers').getPropertyDescription(propertyName) or ""
                    preserveWikiLinks = field.autoWikiLink or field.preserveWikiLinks
            end
                }
           
               
            -- Prepare tooltip attributes if tooltip text exists
                -- Get property name for this field if available (case-insensitive)
            local tooltipClass = ""
                local propertyName = nil
            local tooltipAttr = ""
                for propName, fieldName in pairs(propertyMappings) do
            if tooltipText and tooltipText ~= "" then
                    if key and (fieldName == key or tostring(fieldName):lower() == tostring(key):lower()) then
                 -- Escape quotes in tooltip text to prevent HTML attribute issues
                        propertyName = propName
                 local escapedTooltip = tooltipText:gsub('"', '"')
                        break
                tooltipClass = " has-tooltip"
                    end
                 tooltipAttr = string.format('data-tooltip="%s"', escapedTooltip)
                end
            end
               
           
                 -- Get tooltip text if property exists
            -- Apply processor if available for this field
                 local tooltipText = ""
            if key and processors[key] and type(processors[key]) == "function" then
                 if propertyName then
                 local processedValue = processors[key](value, args)
                    tooltipText = require('Module:SemanticCategoryHelpers').getPropertyDescription(propertyName) or ""
                 end
                  
                  
                 -- Preserve wiki links if needed
                 -- Prepare tooltip attributes if tooltip text exists
                 processedValue = linkParser.preserveWikiLinks(
                 local tooltipClass = ""
                     value,  
                local tooltipAttr = ""
                     processedValue,
                if tooltipText and tooltipText ~= "" then
                     sanitizeOptions.preserveWikiLinks
                    -- 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 type(processedValue) == "table" and processedValue.isCompleteHtml then
                if key and processors[key] and type(processors[key]) == "function" then
                    -- Add the complete HTML as is
                    local processedValue = processors[key](value, args)
                    out[outIndex] = processedValue.html
                   
                    outIndex = outIndex + 1
                    -- Preserve wiki links if needed
                elseif processedValue ~= nil and processedValue ~= false then
                    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
                     processedValue = linkParser.applyWikiLinkHandling(processedValue, field)
                     finalValue = linkParser.applyWikiLinkHandling(finalValue, field)
                      
                      
                     -- Standard field rendering with tooltip
                     -- Use format with tooltip
                     out[outIndex] = string.format(FIELD_FORMAT_WITH_TOOLTIP,  
                     out[outIndex] = string.format(FIELD_FORMAT_WITH_TOOLTIP,  
                         tooltipClass, tooltipAttr, field.label, processedValue)
                         tooltipClass, tooltipAttr, field.label, finalValue)
                     outIndex = outIndex + 1
                     outIndex = outIndex + 1
                 end
                 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
                finalValue = linkParser.applyWikiLinkHandling(finalValue, field)
               
                -- Use format with tooltip
                out[outIndex] = string.format(FIELD_FORMAT_WITH_TOOLTIP,
                    tooltipClass, tooltipAttr, field.label, finalValue)
                outIndex = outIndex + 1
             end
             end
         end
         end