Module:TemplateOrCopy: Difference between revisions

From Structorica Wiki
Jump to navigation Jump to search
Ivan created the page Module:TemplateOrCopy using a non-default content model "Scribunto module"
 
No edit summary
Line 1: Line 1:
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
if k > 1 and type(k) == "number" then
templateArgs[k - 1] = v
-- Pass named arguments as-is
elseif type(k) ~= "number" then
templateArgs[k] = v
end
end
return frame:expandTemplate({ title = templateName, args = templateArgs })
end
return p

Revision as of 12:26, 15 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
		if k > 1 and type(k) == "number" then
			templateArgs[k - 1] = v

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

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

return p