Module:TemplateHelpers: Difference between revisions

// via Wikitext Extension for VSCode
Tag: Manual revert
// via Wikitext Extension for VSCode
Line 73: Line 73:
p.FIELD_FORMAT = FIELD_FORMAT
p.FIELD_FORMAT = FIELD_FORMAT
p.FIELD_FORMAT_WITH_TOOLTIP = FIELD_FORMAT_WITH_TOOLTIP
p.FIELD_FORMAT_WITH_TOOLTIP = FIELD_FORMAT_WITH_TOOLTIP
-- Registry for declarative list post-processors
local listPostProcessors = {
    language = function(itemContent)
        local NormalizationLanguage = require('Module:NormalizationLanguage')
        local langName = NormalizationLanguage.normalize(itemContent)
        local nativeName = NormalizationLanguage.getNativeForm(langName)
        if nativeName and langName ~= "English" then
            return string.format('%s<br/><span style="display:inline-block; width:0.1em; visibility:hidden;">*</span><span style="font-size:75%%;">%s</span>', langName, nativeName)
        else
            return langName
        end
    end,
    website = function(itemContent)
        local linkUrl = itemContent
        if not linkUrl:match("^%a+://") then
            linkUrl = "https://" .. linkUrl
        end
        return string.format("[%s %s]", linkUrl, require('Module:LinkParser').strip(itemContent))
    end,
    autoWikiLink = function(itemContent)
        return '[[' .. itemContent .. ']]'
    end
}


-- Dependencies
-- Dependencies
Line 469: Line 493:
                     end
                     end


                    -- Chain post-processors declaratively
                    local processorsToApply = {}
                     if field.autoWikiLink then
                     if field.autoWikiLink then
                         local originalHook = options.itemHook
                         table.insert(processorsToApply, "autoWikiLink")
                        options.itemHook = function(item)
                    end
                            if type(originalHook) == 'function' then
                    if options.postProcess then
                                item = originalHook(item)
                        table.insert(processorsToApply, options.postProcess)
                            end
                    end
                            local itemContent = type(item) == 'table' and item.content or item
 
                            if itemContent and itemContent ~= '' then
                    for _, processorName in ipairs(processorsToApply) do
                                 local link = '[[' .. itemContent .. ']]'
                        local processorFunc = listPostProcessors[processorName]
                        if processorFunc 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
                                 local processedContent = processorFunc(itemContent)
 
                                 if type(item) == 'table' then
                                 if type(item) == 'table' then
                                     item.content = link
                                     item.content = processedContent
                                     return item
                                     return item
                                 else
                                 else
                                     return link
                                     return processedContent
                                 end
                                 end
                             end
                             end
                            return item
                         end
                         end
                     end
                     end