Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:CIA World Factbook/sandbox

From Wikipedia, the free encyclopedia
local p = {}
local getArgs = require('Module:Arguments').getArgs

-- prefix for Factbook archive
local prefix = 'https://worldfactbookarchive.org/archive/'

-- Function to turn country string into ISO 3166 Alpha-2 code

local function countryCode(country)
	if not country then
		return ''
	end
	local s = nil
	for fragment in country:gmatch("([^,]+)") do
		local trimmed = fragment:gsub("^%s*(.-)%s*$", "%1")
		s = trimmed..(s and ' '..s or '')
	end
	local alpha2 = require('Module:ISO_3166').luacode
	local code = alpha2({s})
	return code == 'GB' and 'UK' or code
end

local monthMap = {january=1,february=2,march=3,april=4,may=5,june=6,
	              july=7,august=8,september=9,october=10,november=11,december=12,
	              jan=1,feb=2,mar=3,apr=4,may=5,jun=6,jul=7,aug=8,sep=9,oct=10,nov=11,dec=12}

local function parseDate(date)
	local year, month, day
	if not date then return nil, nil, nil end
	_, _, year, month, day = mw.ustring.find(date,"(20%d%d)-(%d+)-(%d+)")
	if year then return year, month, day end
	_, _, day, month, year = mw.ustring.find(date,"(%d+)%s+(%a+)%s+(20%d%d)")
	month = month and monthMap[mw.ustring.lower(month)]
	if year then return year, month, day end
	_, _, month, day, year = mw.ustring.find(date,"(%a+)%s(%d+),%s+(20%d%d)")
	month = month and monthMap[mw.ustring.lower(month)]
	if year then return year, month, day end
	_, _, year = mw.ustring.find(date,"(20%d%d)")
	if year then return year, nil, nil end
	return nil, nil, nil
end

local function archiveDate(args)
	local year, month, day = parseDate(args.date)
	if not args.year or args.year == year then return tonumber(year), month, day end
	return tonumber(args.year), nil, nil
end

-- Function to fill in factbook link:
-- Arguments:
--    args.country: topic of page (optional)
--    args.section: section of page (optional)
-- Returns:
--    link to World Factbook page about country, with section anchor
function p._country(args)
	local year, month, day = archiveDate(args)
	if not year or year > 2025 then
		year = 2025
	end

	local cc = countryCode(args.country)
	if #cc == 0 then
		return prefix..year
	end
	return prefix..year..'/'..cc
end


function p.country(frame)
	local args = getArgs(frame)
	return p._country(args)
end

return p