Jump to content

Module:TemplateStarter: Difference between revisions

// via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 66: Line 66:
end
end


-- Generate a create/edit link with preloaded content
-- Generate a form with copy-paste content
function p.createLink(frame)
function p.createLink(frame)
     local args = frame.args
     local args = frame.args
Line 93: Line 93:
     end
     end
      
      
     -- Create a simple edit URL with the content in the URL
     -- Create edit URL (without preload since that doesn't work via URL)
    -- MediaWiki has limits on URL length, so we'll use a simpler approach
     local editUrl = mw.uri.fullUrl(articleName, {
     local editUrl = mw.uri.fullUrl(articleName, {
         action = 'edit',
         action = 'edit',
         summary = 'Creating new ' .. templateType .. ' page',
         summary = 'Creating new ' .. templateType .. ' page'
        preloadtext = content
     })
     })
      
      
     -- Return a button/link to create the page
    -- Generate a unique ID for the textarea
    local textareaId = "template-content-" .. mw.hash.hashValue('md5', articleName .. templateType)
   
     -- Return a form with the content and a button
     return string.format(
     return string.format(
         '<div class="template-starter-create">' ..
         '<div class="template-starter-create" style="border: 1px solid #a2a9b1; padding: 15px; margin: 10px 0; background: #f8f9fa;">' ..
         '<p>Ready to create <b>%s</b> as a %s page.</p>' ..
         '<h4 style="margin-top: 0;">Create: %s</h4>' ..
         '<span class="plainlinks">[%s <span class="mw-ui-button mw-ui-progressive">Create Page</span>]</span>' ..
         '<p>Template type: <b>%s</b></p>' ..
         '<div style="margin-top: 10px; padding: 10px; background: #f8f9fa; border: 1px solid #a2a9b1;">' ..
         '<div style="margin: 10px 0;">' ..
         '<small><b>Preview:</b></small>' ..
         '<label for="%s" style="display: block; margin-bottom: 5px;"><b>Step 1:</b> Copy this template code:</label>' ..
         '<pre style="margin-top: 5px;">%s</pre>' ..
         '<textarea id="%s" readonly style="width: 100%%; height: 200px; font-family: monospace; font-size: 12px;">%s</textarea>' ..
         '</div>' ..
         '</div>' ..
        '<p><b>Step 2:</b> Click the button below to create the page, then paste the template code:</p>' ..
        '<span class="plainlinks">[%s <span class="mw-ui-button mw-ui-progressive">Create Page "%s"</span>]</span>' ..
         '</div>',
         '</div>',
         articleName,
         mw.text.nowiki(articleName),
         templateType,
         templateType,
        textareaId,
        textareaId,
        mw.text.nowiki(content),
         tostring(editUrl),
         tostring(editUrl),
         mw.text.nowiki(content)
         mw.text.nowiki(articleName)
     )
     )
end
-- Generate a dynamic preload template
function p.preload(frame)
    local args = frame.args
    local templateType = args.templateType or args[1]
   
    if not templateType then
        return "<!-- No template type specified -->"
    end
   
    return p.generateTemplate(templateType)
end
end



Revision as of 00:02, 10 June 2025

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

-- Generate a form with copy-paste content
function p.createLink(frame)
    local args = frame.args
    local parent = frame:getParent()
    local pargs = parent and parent.args or {}
    
    -- Get parameters
    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)
    
    -- Check if content is an error
    if content:match("^Error:") then
        return content
    end
    
    -- Create edit URL (without preload since that doesn't work via URL)
    local editUrl = mw.uri.fullUrl(articleName, {
        action = 'edit',
        summary = 'Creating new ' .. templateType .. ' page'
    })
    
    -- Generate a unique ID for the textarea
    local textareaId = "template-content-" .. mw.hash.hashValue('md5', articleName .. templateType)
    
    -- Return a form with the content and a button
    return string.format(
        '<div class="template-starter-create" style="border: 1px solid #a2a9b1; padding: 15px; margin: 10px 0; background: #f8f9fa;">' ..
        '<h4 style="margin-top: 0;">Create: %s</h4>' ..
        '<p>Template type: <b>%s</b></p>' ..
        '<div style="margin: 10px 0;">' ..
        '<label for="%s" style="display: block; margin-bottom: 5px;"><b>Step 1:</b> Copy this template code:</label>' ..
        '<textarea id="%s" readonly style="width: 100%%; height: 200px; font-family: monospace; font-size: 12px;">%s</textarea>' ..
        '</div>' ..
        '<p><b>Step 2:</b> Click the button below to create the page, then paste the template code:</p>' ..
        '<span class="plainlinks">[%s <span class="mw-ui-button mw-ui-progressive">Create Page "%s"</span>]</span>' ..
        '</div>',
        mw.text.nowiki(articleName),
        templateType,
        textareaId,
        textareaId,
        mw.text.nowiki(content),
        tostring(editUrl),
        mw.text.nowiki(articleName)
    )
end

-- Generate a dynamic preload template
function p.preload(frame)
    local args = frame.args
    local templateType = args.templateType or args[1]
    
    if not templateType then
        return "<!-- No template type specified -->"
    end
    
    return p.generateTemplate(templateType)
end

-- Create an InputBox-style form
function p.createForm(frame)
    local args = frame.args
    local templateType = args.templateType or args[1] or "Person"
    
    -- Generate a unique ID for this form instance
    local formId = "templatestarter-" .. os.time()
    
    return string.format([[
<div class="template-starter-form">
<inputbox>
type=create
default=%s/
buttonlabel=Create %s Page
placeholder=Enter page name...
width=40
preload=Template:TemplateStarter/%s
editintro=Template:TemplateStarter/EditIntro
</inputbox>
</div>
]], templateType, templateType, templateType)
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