Module:TemplateStarter: Difference between revisions
// via Wikitext Extension for VSCode |
Maintenance update // via Wikitext Extension for VSCode |
||
| (14 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
-- | --[[ | ||
* Name: TemplateStarter | |||
* Author: Mark W. Datysgeld | |||
* Description: Generates configurable template structures for new pages using ConfigRepository with support for variants and placeholder replacement | |||
* Notes: Template generation from ConfigRepository configs; support for campaign variants; placeholder replacement in boilerplate text; creator field management; caching of template lists; includes error handling and validation | |||
]] | |||
local p = {} | local p = {} | ||
| Line 147: | Line 151: | ||
end | end | ||
templateType = mw.text.trim(tostring(templateType)) | |||
-- Handle "None" special case | |||
if templateType == "None" then | |||
return "" | |||
end | |||
-- 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 159: | Line 179: | ||
end | end | ||
local templates = {} | local templates = {} | ||
if ConfigRepository.templates then | if ConfigRepository.templates then | ||
local index = 1 | local index = 1 | ||
for templateName, | for templateName, config in pairs(ConfigRepository.templates) do | ||
if templateName and templateName ~= "" then | -- Include template if page_creator is not explicitly false | ||
if templateName and templateName ~= "" and (not config.meta or config.meta.page_creator ~= false) then | |||
templates[index] = templateName | templates[index] = templateName | ||
index = index + 1 | index = index + 1 | ||
| Line 173: | Line 192: | ||
table.sort(templates) | table.sort(templates) | ||
end | end | ||
templateListCache = templates | templateListCache = templates | ||
templateListCacheTime = currentTime | templateListCacheTime = currentTime | ||
return templates | return templates | ||
end | |||
-- Get list of available templates including campaign combinations | |||
function p.getAvailableTemplatesWithVariants() | |||
-- Get the filtered list of base templates that are allowed for page creation | |||
local baseTemplates = p.getAvailableTemplates() | |||
local templatesWithCampaigns = {} | |||
-- Use a map to ensure uniqueness before adding to the final list | |||
local templateMap = {} | |||
-- Add base templates to the map | |||
for _, templateName in ipairs(baseTemplates) do | |||
templateMap[templateName] = true | |||
end | |||
-- Iterate through campaigns to find applicable templates | |||
if ConfigRepository.campaigns then | |||
for campaignId, campaign in pairs(ConfigRepository.campaigns) do | |||
if campaign.applicable_templates then | |||
for _, templateName in ipairs(campaign.applicable_templates) do | |||
-- Only include if the base template is allowed for page creation | |||
if templateMap[templateName] then | |||
local campaignTemplateName = templateName .. " (" .. campaign.name .. ")" | |||
templateMap[campaignTemplateName] = true | |||
end | |||
end | |||
end | |||
end | |||
end | |||
-- Convert map keys to a list | |||
local index = 1 | |||
for templateName, _ in pairs(templateMap) do | |||
templatesWithCampaigns[index] = templateName | |||
index = index + 1 | |||
end | |||
-- Sort the final combined list | |||
table.sort(templatesWithCampaigns) | |||
-- Add "None" option at the beginning | |||
table.insert(templatesWithCampaigns, 1, "None") | |||
return templatesWithCampaigns | |||
end | |||
-- Parse campaign template name to get base template and campaign info | |||
function p.parseVariantTemplate(templateName) | |||
if not templateName or templateName == "" or templateName == "None" then | |||
return nil | |||
end | |||
-- Check if this is a campaign combination by looking for the pattern "Template (Campaign Name)" | |||
if ConfigRepository.campaigns then | |||
for campaignId, campaign in pairs(ConfigRepository.campaigns) do | |||
local expectedName = " (" .. campaign.name .. ")" | |||
if templateName:sub(-#expectedName) == expectedName then | |||
local baseTemplateName = templateName:sub(1, -#expectedName - 1) | |||
-- Verify this is a valid combination | |||
if campaign.applicable_templates then | |||
for _, applicableTemplate in ipairs(campaign.applicable_templates) do | |||
if applicableTemplate == baseTemplateName then | |||
return { | |||
baseTemplate = baseTemplateName, | |||
variantKey = campaignId, | |||
variant = { | |||
name = templateName, | |||
campaign_template = campaign.json_config | |||
} | |||
} | |||
end | |||
end | |||
end | |||
end | |||
end | |||
end | |||
-- Not a campaign combination, 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" | ||
| Line 193: | Line 333: | ||
-- Get creator fields for a specific template type | -- Get creator fields for a specific template type | ||
function p.getCreatorFields(templateType) | function p.getCreatorFields(templateType) | ||
if not templateType or templateType == "" then | if not templateType or templateType == "" or templateType == "None" then | ||
return {} | return {} | ||
end | end | ||
local config = ConfigRepository.getConfig( | -- Parse template to resolve variants to base template | ||
local parsedTemplate = p.parseVariantTemplate(templateType) | |||
local baseTemplateName = parsedTemplate and parsedTemplate.baseTemplate or templateType | |||
local config = ConfigRepository.getConfig(baseTemplateName) | |||
return config.creatorFields or {} | return config.creatorFields or {} | ||
end | end | ||
| Line 232: | Line 376: | ||
end | end | ||
end | end | ||
return result | |||
end | |||
-- Remove empty placeholders from text (for cases where no values are provided) | |||
function p.removeEmptyPlaceholders(text) | |||
if not text then | |||
return text | |||
end | |||
-- Remove any remaining $VARIABLE$ placeholders that weren't filled | |||
local result = text:gsub("%$[A-Z_]+%$", "") | |||
-- Clean up any resulting double spaces or awkward punctuation | |||
result = result:gsub("%s+", " ") -- Multiple spaces to single space | |||
result = result:gsub("^%s+", "") -- Leading whitespace | |||
result = result:gsub("%s+$", "") -- Trailing whitespace | |||
result = result:gsub("%s+%.", ".") -- Space before period | |||
result = result:gsub("is a%s+based", "is based") -- Fix "is a based" to "is based" | |||
result = result:gsub("is a%s+in", "is in") -- Fix "is a in" to "is in" | |||
return result | return result | ||