Documentation

Template version selector for editcopy pages.


local p = {}

-- Editcopy suffixes in priority order (first match wins)
local EDITCOPY_SUFFIXES = {
	"editcopy", -- English
	"Копия", -- Russian
}

function p.templateHelper(frame)
	local args = frame:getParent().args
	local templateName = mw.text.trim(args[1] or "")
	if templateName == "" then
		return ""
	end

	-- Add namespace prefix if not present
	if not templateName:match("^Template:") then
		templateName = "Template:" .. templateName
	end

    
	-- If on editcopy page, try to find editcopy version of template
	local isEditcopyPage = false

	local title = mw.title.getCurrentTitle()
	for _, suffix in ipairs(EDITCOPY_SUFFIXES) do
		if title.subpageText == suffix then
			isEditcopyPage = true
			break
		end
	end

	-- Try to find existing editcopy template
	if isEditcopyPage then
		for _, suffix in ipairs(EDITCOPY_SUFFIXES) do
			local testTitle = mw.title.new(templateName .. "/" .. suffix)
			if testTitle ~= nil and testTitle.exists then
				templateName = templateName .. "/" .. suffix
				break
			end
		end
	end

	local templateArgs = {}

	for k, v in pairs(args) do
		-- Skip the first argument (template name), pass others
		local numK = tonumber(k)
		if numK and numK > 1 then
			templateArgs[numK - 1] = v

		-- Pass named arguments as-is
		else
			templateArgs[k] = v
		end
	end

	return frame:expandTemplate({ title = templateName, args = templateArgs })
end

return p