Module:TemplateOrCopy: Difference between revisions

From Structorica Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
Line 46: Line 46:
for k, v in pairs(args) do
for k, v in pairs(args) do
-- Skip the first argument (template name), pass others
-- Skip the first argument (template name), pass others
if k > 1 and type(k) == "number" then
local numK = tonumber(k)
templateArgs[k - 1] = v
if numK and numK > 1 then
templateArgs[numK - 1] = v


-- Pass named arguments as-is
-- Pass named arguments as-is
elseif type(k) ~= "number" then
else
templateArgs[k] = v
templateArgs[k] = v
end
end

Latest revision as of 16:16, 16 January 2026

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