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 12:10, 21 June 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 trimtags(s)
  return string.gsub(s, "<([^>]-)>", "")
end

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 = string.lower(args[2])
	local parameter = string.lower(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 = string.lower(string.gsub(titleObj:getContent() or "", "\n", ""))
	
	while templateCount ~= templateMatch do
		articleContent = string.match(articleContent, '%{%{' .. template .. "(.+)")
		templateCount = templateCount + 1
	end
	
	while parameterCount ~= parameterMatch do
		articleContent = string.match(articleContent, '%|%s*' .. parameter .. '%s*=(.+)')
		parameterCount = parameterCount + 1
	end
	
	return string.match(articleContent, '([^%|%}]+)')
end

return p