Modul:Hendelser

Fra Wikipedia, den frie encyklopedi
Moduldokumentasjon
local significant_years = {1, 2, 3, 4, 5, 10, 15, 20, 25, 30, 40, 50, 60, 70, 75, 80, 90, 100, 110, 120, 125, 130, 140, 150, 160, 170, 175, 180, 190, 200, 225, 250, 275, 300, 325, 350, 375, 400, 425, 450, 475, 500, 550, 600, 650, 700, 750, 800, 850, 900, 950, 1000, 1100, 1200, 1300, 1400, 1500, 1600, 1700, 1800, 1900, 2000, 2500, 3000, 3500, 4000, 4500, 5000}
local current_year = os.date("%Y")

local p = {}

function aaa(line)
	if string.find(line, "<ref") then
		line = string.gsub(line, "<ref[%s%S]+ref>", "")
	end
	
	local l = mw.text.split(line,"–",true)
	local num = mw.ustring.gsub(l[1],"[^%d]","")

	-- if there also was a "–" in the text this recombines the sentence
	text = l[2]
	for i,txt in ipairs(l) do
		if i > 2 then
			text = text .. "–" .. txt
		end
	end
	return num,text
end

function get_historie(lines)
	local tbl = {}
	local category = ""
	for i,line in ipairs(lines) do
		if string.find(line, "==") then
			if string.find(line, "istorie") then     -- Historie eller Norsk historie
				category = "historie"
			else
				category = ""
			end
		end
		if category == "historie" and mw.ustring.sub(line,1,1) =="*" then
			local ll,kk = aaa(line)
			if ll and kk and string.len(ll) > 0 and string.len(kk)>0 then
				if not tbl[ll] then
					tbl[ll] = {}
				end
				table.insert(tbl[ll],kk)
			end
		end
	end
	return tbl
end

function get_merkedager(lines)
	local tbl = {}
	local category = ""
	for i,line in ipairs(lines) do
		if string.find(line, "==") then
			if string.find(line, "Merkedager") or string.find(line, "Helligdager") or string.find(line, "Høytidsdager") then
				category = "merkedager"
			else
				category = ""
			end
		end
		if category == "merkedager" and mw.ustring.sub(line,1,1) =="*" then
			table.insert(tbl,line)
		end
	end
	return tbl
end

function merkedager_format(merkedager_hendelser, display_str)
	if not merkedager_hendelser then
		return display_str
	end

	for j, event in pairs(merkedager_hendelser) do
		display_str = display_str .. event .. '\10'
	end
	return display_str
end

function p.read_information_from_date_page(frame, page)
	local title = mw.title.new(page, 0 )
	local kaltext = title:getContent()
	local lines = mw.text.split(kaltext,"\n",true)
	local tbl = {}
	historie_events = get_historie(lines)
	merkedager_events = get_merkedager(lines)
	return historie_events, merkedager_events
end

function p.main(frame, current_day)
	local args=frame.args
	current_day = args[1]
	historie_events, merkedager_events = p.read_information_from_date_page(frame, current_day)

	display_str = ""
	display_str = merkedager_format(merkedager_events, display_str)
	
	for i, sign_year in ipairs(significant_years) do
		for event_year, event_list in pairs(historie_events) do
	     	diff = (event_year + sign_year) - current_year
	     	if diff == 0 and event_list then
		     	display_str = display_str .. "* For '''" .. sign_year .. "''' år siden (i [[" .. event_year .. "]]): \10" 
	     		for j, event in pairs(event_list) do
	    			display_str = display_str .. "**" .. event .. "\10"
				end
			end
    	end
	end
	
	if display_str == "" then
		display_str = "* Ingen hendelser eller merkedager funnet.\10"
	end
	
	return display_str
end
 
return p