Jump to content

Module:Template parameter value

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alex 21 (talk | contribs) at 14:28, 17 July 2018. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

function trimspaces(s)
	return string.gsub(s, "^%s*(.-)%s*$", "%1")
end

function p.main(frame)
	local args = require('Module:Arguments').getArgs(frame, {
		wrappers = 'Template:Template parameter value'
	})
	
	local template = args[2]
	local parameter = args[4]
	local numberedParameter = (tonumber(parameter) ~= nil)
	
	local templateCount = 0
	local parameterCount = 0
	local templateMatch = tonumber(args[3] or 1)
	local parameterMatch = tonumber(args[5] or 1)*(numberedParameter and parameter or 1)
	
	local target = args[1]
	local targettitle = mw.title.new(target)
    local content = string.gsub(targettitle:getContent() or "", "[\r\n]", "")
	
	while templateCount ~= templateMatch do
		if content == nil then return "" end
		content = string.match(content, '{{' .. template .. "(.+)")
		templateCount = templateCount + 1
	end
	
	while parameterCount ~= parameterMatch do
		if content == nil then return "" end
		content = string.match(content, '|%s*' .. (numberedParameter and "" or parameter .. '%s*=%s*') .. '(.+)')
		parameterCount = parameterCount + 1
	end
	
	content = string.gsub(content, "</?%a*include%a*>", "")
	content = string.match(content, '^([^|}]*{{[^}]+}}[^|}]*)|') or string.match(content, '([^|}]+)')
	content = frame:preprocess{text = content}
	content = trimspaces(content)
	
	return content
end

return p