Jump to content

Module:TemplateStarter

Revision as of 23:58, 9 June 2025 by MarkWD (talk | contribs) (// via Wikitext Extension for VSCode)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Documentation for this module may be created at Module:TemplateStarter/doc

-- Module:TemplateStarter
-- Generates empty template structures for new pages using ConfigRepository

local p = {}

-- Required modules
local ConfigRepository = require('Module:ConfigRepository')

-- Generate empty template wikitext from template type
function p.generateTemplate(templateType)
    -- Get configuration for the template type
    local config = ConfigRepository.getConfig(templateType)
    
    -- Check if template exists
    if not config or not config.fields then
        return string.format("Error: Template type '%s' not found in ConfigRepository", templateType)
    end
    
    -- Start building the template
    local lines = {"{{" .. templateType}
    
    -- Process each field
    for _, field in ipairs(config.fields) do
        -- Skip hidden fields
        if not field.hidden then
            -- Handle fields with multiple keys
            local fieldKey = field.key or (field.keys and field.keys[1])
            
            if fieldKey then
                table.insert(lines, string.format("|%s = ", fieldKey))
            end
        end
    end
    
    -- Close the template
    table.insert(lines, "}}")
    
    -- Join with newlines
    return table.concat(lines, "\n")
end

-- Main function to be called from wiki
function p.main(frame)
    local args = frame.args
    local parent = frame:getParent()
    local pargs = parent and parent.args or {}
    
    -- Get parameters (check both direct and parent args)
    local articleName = args.articleName or pargs.articleName or args[1] or pargs[1]
    local templateType = args.templateType or pargs.templateType or args[2] or pargs[2]
    
    -- Validate inputs
    if not articleName or articleName == "" then
        return "Error: Article name is required"
    end
    
    if not templateType or templateType == "" then
        return "Error: Template type is required"
    end
    
    -- Generate the template content
    local content = p.generateTemplate(templateType)
    
    -- For testing, return the generated content in a pre block
    return string.format('<pre>Page: %s\n\n%s</pre>', articleName, content)
end

-- Get list of available templates
function p.getAvailableTemplates()
    local templates = {}
    for templateName, _ in pairs(ConfigRepository.templates) do
        table.insert(templates, templateName)
    end
    table.sort(templates)
    return templates
end

-- Test function to list available templates
function p.listTemplates(frame)
    local templates = p.getAvailableTemplates()
    return "Available templates: " .. table.concat(templates, ", ")
end

return p