Moduł:Brudnopis/Paweł Ziemian/Tabela władców

Z Wikipedii, wolnej encyklopedii

Dokumentacja dla tego modułu może zostać utworzona pod nazwą Moduł:Brudnopis/Paweł Ziemian/Tabela władców/opis

local dataSources = {
	prefix = "Tabele władców/",
	ns = "Szablon",
	{ min =-9999, max = 9999, title="Polska" },
	{ min =-9999, max =    0, title="p.n.e.", },
	{ min =    1, max = 1000, title="I-X", },
	{ min = 1001, max = 1100, title="XI", },
	{ min = 1101, max = 1200, title="XII", },
	{ min = 1201, max = 1300, title="XIII", },
	{ min = 1301, max = 1400, title="XIV", },
	{ min = 1401, max = 1500, title="XV", },
	{ min = 1501, max = 1600, title="XVI", },
	{ min = 1601, max = 1700, title="XVII", },
	{ min = 1701, max = 1800, title="XVIII", },
	{ min = 1801, max = 1900, title="XIX", },
	{ min = 1901, max = 2000, title="XX", },
	{ min = 2001, max = 9999, title="XXI", },
}

function loadAndMerge(title, data)
	local map = {}
	for i, g in ipairs(data) do
		map[g.header] = g
	end
	
	local group = false
	local merge = false
	
	for line in mw.ustring.gmatch(mw.title.new(dataSources.prefix..title, dataSources.ns):getContent(), "[^\n]+") do
		local h = mw.ustring.match(line, "^==%s*([^=%s].-[^=%s])%s*==$")
		if h then
			if group and group.items and not map[group.header] then
				table.insert(data, group)
			end
	
			group = map[h] or { header = h }
			merge = group.items ~= nil
		elseif group then
			local s, e, n = mw.ustring.match(line, "^%s+(%-?[0-9]+)%s+(%-?[0-9]+)%s+(.-)%s*$")
			s = tonumber(s)
			e = tonumber(e)
			if s and e and n then
				if not group.items then
					group.items = {}
				end
				
				local add = true
				if merge then
					merge = false
					if #group.items > 0 then
						local last = group.items[#group.items]
						add = (last[1] ~= s) or (last[2] ~= e) -- or (last[3] ~= n)
					end
				end

				if add then
					table.insert(group.items, { s, e, n } )
				end
			end
		end
	end
	
	if group and group.items and not map[group.header] then
		table.insert(data, group)
	end
end

return {

["lista"] = function (frame)
	
	local allData = {}
	for i, v in ipairs(dataSources) do
		loadAndMerge(v.title, allData)
	end

	local yearToString = function(y, prefix)
		if y == 9999 then
			return ""
		end
		
		local text = y < 0 and tostring(-y).." p.n.e." or tostring(y)
		return prefix.."[["..text.."]]"
	end
	
	local textToString = function(t)
		local result, _ = mw.ustring.gsub(t, "\\n", " ")
		return result
	end
	
	local result = {}
	for _, v in ipairs(allData) do
		table.insert(result,"==")
		table.insert(result,textToString(v.header))
		table.insert(result,"==")
		for _, i in ipairs(v.items) do
			table.insert(result, "\n*")
			table.insert(result,textToString(i[3]))
			table.insert(result, " (")
			if i[1] == i[2] then
				table.insert(result,yearToString(i[1], "w "))
			else
				table.insert(result,yearToString(i[1], "od "))
				table.insert(result,yearToString(i[2], " do "))
			end
			table.insert(result, ")")
		end
		table.insert(result,"\n\n")
	end
	
	local text = table.concat(result, "")
	return text
end,
	
["na rok"] = function (frame)
	local args = require('Module:Arguments').getArgs(frame)
	local year = tostring(args.rok or args[1] or mw.title.getCurrentTitle().subpageText)
	mw.logObject(year, "year")
	local bc = mw.ustring.match(year, "^([0-9]+) p%.n%.e%.$")
	if bc then
		year = -tonumber(bc)
	else
		year = tonumber(year)
	end
	
	if not year then
		mw.log("brak roku")
		return
	end

	if year > tonumber(mw.language.getContentLanguage():formatDate("Y", "", true)) then
		mw.log("rok z przyszłości")
		return
	end
	
	local allData = {}
	for i, v in ipairs(dataSources) do
		if (v.min <= year) and (year <= v.max) then
			loadAndMerge(v.title, allData)
		end
	end
	
	local yearToString = function(y)
		if y == 9999 then
			return "&nbsp;"
		end
		
		local text = y < 0 and tostring(-y).." p.n.e." or tostring(y)
		return y == year and text or "[["..text.."]]"
	end
	
	local textToString = function(t)
		local result, _ = mw.ustring.gsub(t, "\\n", "<br />")
		return result
	end

	local data = {}
	for _, g in ipairs(allData) do
		local items = {}
		for _, v in ipairs(g.items) do
			local s = v[1]
			local e = v[2]
			local n = v[3]
			if s and e and n and (s <= year) and (year <= e) then
				table.insert(items, {
					yearToString(s),
					yearToString(e),
					textToString(n)
				})
			end
		end

		if #items > 0 then
			table.insert(data, { header = textToString(g.header), items = items })
		end
	end

	if #data <= 0 then
		mw.log("brak danych")
		return
	end
	
	local result = {}
	table.insert(result, '\n{|class="wikitable" align="right" style="float: right; width: 260px; margin: 0 0 1em 1em; background: #f9f9f9; border: 1px #aaa solid; border-collapse: collapse; font-size: 92%; text-align: center;"')
	table.insert(result, '\n|align=center colspan=2|<div class="noprint">&#91; [[Wikiprojekt:Tabele władców|Edytuj tabelę]] &#93;</div>')
	
	for _, v in ipairs(data) do
		table.insert(result, '\n|-')
		table.insert(result, '\n|align="center" bgcolor="#E0FFE0"|') table.insert(result, v.header) table.insert(result, '||align=center|')
		table.insert(result, '\n{|style="width:100%; text-align:center;"')
		
		for _, w in ipairs(v.items or {}) do
			table.insert(result, '\n|-')
			table.insert(result, '\n| style="width:10%;" | ') table.insert(result, w[1])
			table.insert(result, '\n| style="width:80%;" | ') table.insert(result, w[3])
			table.insert(result, '\n| ') table.insert(result, w[2])
		end
		
		table.insert(result, '\n|-')
		table.insert(result, '\n|}')
	end
	
	if year > 1582 then
		local newYear = string.format("%04d-01-01", year)
        local key = mw.language.getContentLanguage():formatDate("LN", newYear)
        local calendars = {
        	["01"] = "Kalendarz od poniedziałku",
        	["02"] = "Kalendarz od wtorku",
        	["03"] = "Kalendarz od środy",
        	["04"] = "Kalendarz od czwartku",
        	["05"] = "Kalendarz od piątku",
        	["06"] = "Kalendarz od soboty",
        	["07"] = "Kalendarz od niedzieli",
        	["11"] = "Kalendarz od poniedziałku przestępny",
        	["12"] = "Kalendarz od wtorku przestępny",
        	["13"] = "Kalendarz od środy przestępny",
        	["14"] = "Kalendarz od czwartku przestępny",
        	["15"] = "Kalendarz od piątku przestępny",
        	["16"] = "Kalendarz od soboty przestępny",
        	["17"] = "Kalendarz od niedzieli przestępny",
        }
        mw.logObject({ key, calendars[key] }, "kalendarz")
        local calendar = mw.getCurrentFrame():expandTemplate{title = calendars[key], args = {}}
		table.insert(result, '\n|-')
		table.insert(result, '\n|colspan="2" align="center"|<div class="center"><small>')
			table.insert(result, "'''Kalendarz na rok ")
			table.insert(result, tostring(year))
			table.insert(result, "'''</small></div><br />")
			table.insert(result, calendar)
    end

	table.insert(result, '\n|-')
	table.insert(result, '\n|}')
	
	local result = table.concat(result)
	return result
end,

}