Module:CIA World Factbook/sandbox
Appearance
| This is the module sandbox page for Module:CIA World Factbook (diff). |
| This module is rated as ready for general use. It has reached a mature state, is considered relatively stable and bug-free, and may be used wherever appropriate. It can be mentioned on help pages and other Wikipedia resources as an option for new users. To minimise server load and avoid disruptive output, improvements should be developed through sandbox testing rather than repeated trial-and-error editing. |
| This module depends on the following other modules: |
Used by {{CIA World Factbook}} and {{Cite CIA World Factbook}}
Usage
[edit]Country
[edit]{{#invoke:CIA World Factbook|country|country=|section=}}
- Generates the URL for a country entry in the CIA World Factbook.
|country=is the topic country (optional)|section=is the section anchor to link to (e.g., "People and Society") (optional)|year=the year of the articlee|date=the date of the article in the Factbook (either on the article itself, or the access date of the article)
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