Module:TemplateStarter: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Tag: Reverted
Line 12: Line 12:
local templateListCacheTime = 0
local templateListCacheTime = 0
local CACHE_DURATION = 300 -- 5 minutes
local CACHE_DURATION = 300 -- 5 minutes
-- Generate wikitext for a template with a variant
function p.generateTemplateWithVariant(templateType, variantType)
    local errorContext = ErrorHandling.createContext("TemplateStarter_Variant")
    local TemplateVariantHelper = require('Module:TemplateVariantHelper')
    local DatasetLoader = require('Module:DatasetLoader')
    -- Get base template config
    local baseConfig = ConfigRepository.getConfig(templateType)
    if not baseConfig then
        return "Error: Base template type not found"
    end
    -- Get variant config path and load it
    local variantPath = baseConfig.variants and baseConfig.variants[variantType]
    if not variantPath then
        return "Error: Variant type not found for this template"
    end
   
    local variantConfig = DatasetLoader.load(variantPath)
    if not variantConfig then
        return "Error: Could not load variant configuration"
    end
    -- Get merged fields
    local mergedFields = TemplateVariantHelper.getVariantFields(baseConfig, variantConfig)
    local lines = {}
    lines[#lines + 1] = "{{" .. templateType
    lines[#lines + 1] = string.format("|variant = %s", variantType)
    for _, field in ipairs(mergedFields) do
        if not field.hidden then
            local fieldKey = field.key or (field.keys and field.keys[1])
            if fieldKey then
                lines[#lines + 1] = string.format("|%s = ", fieldKey)
            end
        end
    end
    lines[#lines + 1] = "}}"
    return table.concat(lines, "\n")
end


-- Generate empty template wikitext from template type
-- Generate empty template wikitext from template type
Line 142: Line 185:
     local args = frame.args
     local args = frame.args
     local templateType = args.templateType or args[1]
     local templateType = args.templateType or args[1]
      
     local variantType = args.variant or args.variantType
 
     if not templateType or mw.text.trim(tostring(templateType)) == "" then
     if not templateType or mw.text.trim(tostring(templateType)) == "" then
         return "<!-- No template type specified -->"
         return "<!-- No template type specified -->"
     end
     end
      
 
     return p.generateTemplate(mw.text.trim(tostring(templateType)))
     templateType = mw.text.trim(tostring(templateType))
 
     if variantType and mw.text.trim(tostring(variantType)) ~= "" then
        variantType = mw.text.trim(tostring(variantType))
        return p.generateTemplateWithVariant(templateType, variantType)
    else
        return p.generateTemplate(templateType)
    end
end
end