Jump to content

Module:User:Morwen/succession

From Wikipedia, the free encyclopedia
local p = {}

local getArgs = require('Module:Arguments').getArgs

local created = {}
local incumbent = {}

local function link(txt)
	if txt == created then
		return "'''Created'''"
	end
	if txt == incumbent then
		return "'''Incumbent'''"
	end
	return "[[" .. txt .. "]]"
end

function p._renderBox()
	local title = mw.title.getCurrentTitle()
	local pageName = title.fullText

	local title = mw.title.new("User:Morwen/succession/list")
	local wikitext = title:getContent()
	
	local links = {}
	local spans = {}
	local indexesAt = {}
	local office = ""
	
	local out = ""

	for line in mw.ustring.gmatch(wikitext, "([^\r\n]+)") do
    	if string.sub(line, 1, 1) == ";" then
			office = string.sub(line, 2)
		end
    	if string.sub(line, 1, 1) == "*" then
			local rest = string.sub(line, 2)
			if string.sub(rest, 1, 2) == "[[" then
				rest = string.sub(rest, 3)
				local sep = "]] "
				local startPos, endPos = string.find(rest, sep, 1, true)
				local span
				if startPos then
    				span = string.sub(rest, endPos + 1)
					rest = string.sub(rest, 1, startPos - 1)
				else
					rest = string.sub(rest, 1, string.len(rest) - 2)
					span = "n/a"
				end
				table.insert(links, rest)
				table.insert(spans, span)

				out = out .. rest .. "(" .. span .. ")\n"
				
				if rest == pageName then
					table.insert(indexesAt, #links)
				end
			end
		end
	end

	local out = [[{| class="wikitable"]]

	for i, thisIndex in ipairs(indexesAt) do
		local before = links[thisIndex - 1] or created
		local thisSpan = spans[thisIndex] or ""
		local after = links[thisIndex + 1] or incumbent

		out = out .. string.format([[
|-
|align=center| Preceeded by: <br /> %s 
|align=center| '''%s''' <br /> %s
|align=center| Succeeded by: <br /> %s 
	]], link(before), office, thisSpan, link(after))
	
	end

	out = out .. "|}"

	return out
	
end

function p.renderBox(frame)
    local args = getArgs(frame, {
        wrappers = 'User:Morwen/succession/template'
    })

    return p._renderBox(args)
end

return p