Jump to content

Module:Sandbox/Alex 21

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Alex 21 (talk | contribs) at 10:55, 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, {frameOnly = true})
	
	local template = args[2]
	local parameter = args[4]
	
	local templateCount = 0
	local parameterCount = 0
	local templateMatch = tonumber(args[3])
	local parameterMatch = tonumber(args[5])
	
	local titleObj = getTitle(args[1])
	local articleContent = titleObj:getContent() or ""
	local articleContentSplit = mw.text.split(articleContent, '\n')
	
	for k,v in pairs(articleContentSplit) do
		if string.match(v, '%{%{' .. template) ~= nil then
			templateCount = templateCount + 1
		end
		if templateCount == templateMatch then
			local parameterStringMatch = string.match(v, '%|%s*' .. parameter .. "%s*=(.+)")
			if parameterStringMatch ~= nil then
				parameterCount = parameterCount + 1
				if parameterCount == parameterMatch then
					return parameterStringMatch[1]
				end
			end
		end
	end
	
	return ""
end

return p