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 15:41, 21 June 2018. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

local p = {}

local function getTitle(...)
	local success, titleObj = pcall(mw.title.new, ...)
	if success then return titleObj
	else return nil end
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 templateCount = 0
	local parameterCount = 0
	local templateMatch = tonumber(args[3] or 1)
	local parameterMatch = tonumber(args[5] or 1)
	
	local titleObj = getTitle(args[1])
	local articleContent = string.gsub(titleObj:getContent() or "", "\n", "")
	
	while templateCount ~= templateMatch do
		if articleContent == nil then return "" end
		articleContent = string.match(articleContent, '{{' .. template .. "(.+)")
		templateCount = templateCount + 1
	end
	
	while parameterCount ~= parameterMatch do
		if articleContent == nil then return "" end
		articleContent = string.match(articleContent, '|%s*' .. parameter .. '%s*=(.+)')
		parameterCount = parameterCount + 1
	end
	
	articleContent = frame:preprocess(articleContent)
	articleContent = string.match(articleContent, '([^|}]+)')
	
	return articleContent
end

return p