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:51, 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 = args[3]
	local parameterMatch = 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
			return '|[%s]*' .. parameter
			--[[if string.match(v, '|[%s]*' .. parameter) ~= nil then
				parameterCount = parameterCount + 1
				if parameterCount == parameterMatch then
					return v
				end
			end--]]
		end
	end
	
	return parameterCount
end

return p