Module:TemplateStarter: Difference between revisions
// via Wikitext Extension for VSCode Tag: Manual revert |
// via Wikitext Extension for VSCode |
||
| Line 147: | Line 147: | ||
end | end | ||
templateType = mw.text.trim(tostring(templateType)) | |||
-- Parse template to check if it's a variant | |||
local parsedTemplate = p.parseVariantTemplate(templateType) | |||
if parsedTemplate and parsedTemplate.variant then | |||
-- Generate combined template for variant | |||
return p.generateCombinedTemplate(parsedTemplate.baseTemplate, parsedTemplate.variant) | |||
else | |||
-- Generate regular template | |||
return p.generateTemplate(templateType) | |||
end | |||
end | end | ||
| Line 180: | Line 191: | ||
return templates | return templates | ||
end | |||
-- Get list of available templates including variants | |||
function p.getAvailableTemplatesWithVariants() | |||
local templates = p.getAvailableTemplates() | |||
local templatesWithVariants = {} | |||
local index = 1 | |||
-- Add base templates first | |||
for _, templateName in ipairs(templates) do | |||
templatesWithVariants[index] = templateName | |||
index = index + 1 | |||
end | |||
-- Add variants | |||
if ConfigRepository.templates then | |||
for templateName, config in pairs(ConfigRepository.templates) do | |||
if config.variants then | |||
for variantKey, variant in pairs(config.variants) do | |||
-- Only include active variants | |||
if variant.state and variant.name then | |||
templatesWithVariants[index] = variant.name | |||
index = index + 1 | |||
end | |||
end | |||
end | |||
end | |||
end | |||
-- Sort the combined list | |||
table.sort(templatesWithVariants) | |||
return templatesWithVariants | |||
end | |||
-- Parse variant template name to get base template and variant info | |||
function p.parseVariantTemplate(templateName) | |||
if not templateName or templateName == "" then | |||
return nil | |||
end | |||
-- Check if this is a variant by looking for the pattern in all templates | |||
if ConfigRepository.templates then | |||
for baseTemplateName, config in pairs(ConfigRepository.templates) do | |||
if config.variants then | |||
for variantKey, variant in pairs(config.variants) do | |||
if variant.state and variant.name == templateName then | |||
return { | |||
baseTemplate = baseTemplateName, | |||
variantKey = variantKey, | |||
variant = variant | |||
} | |||
end | |||
end | |||
end | |||
end | |||
end | |||
-- Not a variant, return as base template | |||
return { | |||
baseTemplate = templateName, | |||
variantKey = nil, | |||
variant = nil | |||
} | |||
end | |||
-- Generate combined template for variants | |||
function p.generateCombinedTemplate(baseTemplate, variant) | |||
if not baseTemplate or not variant then | |||
return "Error: Invalid template combination" | |||
end | |||
-- Generate base template | |||
local baseContent = p.generateTemplate(baseTemplate) | |||
if not baseContent or baseContent:match("^Error:") then | |||
return baseContent or "Error: Failed to generate base template" | |||
end | |||
-- Generate campaign template if specified | |||
local campaignContent = "" | |||
if variant.campaign_template then | |||
campaignContent = string.format("{{#invoke:T-Campaign|render|campaign_name=%s}}", variant.campaign_template) | |||
end | |||
-- Combine templates with blank line separator | |||
local combinedLines = {} | |||
local lineCount = 0 | |||
-- Add base template | |||
lineCount = lineCount + 1 | |||
combinedLines[lineCount] = baseContent | |||
-- Add separator | |||
lineCount = lineCount + 1 | |||
combinedLines[lineCount] = "" | |||
-- Add campaign template | |||
if campaignContent ~= "" then | |||
lineCount = lineCount + 1 | |||
combinedLines[lineCount] = campaignContent | |||
end | |||
return table.concat(combinedLines, "\n") | |||
end | end | ||
-- Test function to list available templates (clean output for JS consumption) | -- Test function to list available templates (clean output for JS consumption) | ||
function p.listTemplates(frame) | function p.listTemplates(frame) | ||
local templates = p. | local templates = p.getAvailableTemplatesWithVariants() | ||
if #templates == 0 then | if #templates == 0 then | ||
return "No templates available" | return "No templates available" | ||