Module:OpenHistoricalMap/Events

From OpenStreetMap Wiki
Jump to navigation Jump to search
[Edit] [Purge] Documentation

Usage

This module populates {{OpenHistoricalMap/Events}} with a list of OpenHistoricalMap-related mapping events from Module:OpenHistoricalMap/Events/data.json.

main

|date_format =
The date format for the header of each event in the list.
|historical = yes
Formats the list for a historical archive. The list includes past events and is sorted chronologically (instead of reverse-chronologically).

See also

local p = {}
local languages = require("Module:Languages")

function p.main(frame)
	local title = mw.title.getCurrentTitle()
	local lang = mw.getLanguage(languages.languageFromTitle(title))
	local langCode = lang:getCode()
	-- MediaWiki abnormally lowercases ISO 3166 and ISO 15924 codes in locales.
	langCode = langCode:gsub("-(%w%w%w%w)", function (script)
		return "-" .. script:sub(1, 1):upper() .. script:sub(2)
	end):gsub("-(%w%w)$", function (country)
		return "-" .. country:upper()
	end)
	
	local dateFormat = frame.args.date_format and #frame.args.date_format > 0 and frame.args.date_format or "F j, Y"
	local historical = frame.args.historical == "yes"

	local events = mw.loadJsonData("Module:OpenHistoricalMap/Events/data.json")
	if not historical then
		-- Clone the read-only JSON data in order to manipulate it; mw.clone() requires write permissions.
		events = mw.text.jsonDecode(mw.text.jsonEncode(events))
		-- Sort the data reverse-chronologically.
		table.sort(events, function (a, b)
			return a.start_date > b.start_date
		end)
	end
	
	local tomorrow = lang:formatDate("c", "+1 day")
	local list = mw.html.create("dl")
	for i, event in ipairs(events) do
		-- Omit past events.
		if not historical and (event.end_date or event.start_date) < tomorrow then
			break
		end
		
		local dateRange = lang:formatDate(dateFormat, event.start_date)
		if event.end_date and event.end_date > event.start_date then
			-- TODO: Make the range format localizable.
			-- TODO: Optimize the range format for ranges within the same year.
			dateRange = dateRange .. " – " .. lang:formatDate(dateFormat, event.end_date)
		end
		list:tag("dt")
			:wikitext(dateRange)
		list:tag("dd")
			:wikitext(frame:preprocess(event.description[langCode] or event.description.en))
	end
	
	return list
end

return p