Module:TemplateStarter: Difference between revisions

// via Wikitext Extension for VSCode
 
// via Wikitext Extension for VSCode
Line 64: Line 64:
     -- For testing, return the generated content in a pre block
     -- For testing, return the generated content in a pre block
     return string.format('<pre>Page: %s\n\n%s</pre>', articleName, content)
     return string.format('<pre>Page: %s\n\n%s</pre>', articleName, content)
end
-- Generate a create/edit link with preloaded 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)
   
    -- URL encode the content for use in URL
    local encodedContent = mw.uri.encode(content)
   
    -- Create edit URL with preloaded text
    local editUrl = mw.uri.fullUrl(articleName, {
        action = 'edit',
        preload = 'Template:TemplateStarter/Preload',
        preloadparams = encodedContent,
        editintro = 'Template:TemplateStarter/EditIntro',
        summary = 'Creating new ' .. templateType .. ' page'
    })
   
    -- Return a button/link to create the page
    return string.format(
        '<div class="template-starter-create">' ..
        '<p>Ready to create <b>%s</b> as a %s page.</p>' ..
        '<span class="plainlinks">[%s <span class="mw-ui-button mw-ui-progressive">Create Page</span>]</span>' ..
        '</div>',
        articleName,
        templateType,
        tostring(editUrl)
    )
end
end