Module:CIA World Factbook: Difference between revisions

From The Global Wiki
Jump to navigation Jump to search
en>Reaper Eternal
m (Changed protection settings for "Module:CIA World Factbook": Highly visible template ([Edit=Require template editor access] (indefinite)))
 
m (1 revision imported)
 
(No difference)

Latest revision as of 02:13, 18 January 2023

Template:Module rating Used by {{CIA World Factbook}} and {{Cite CIA World Factbook}}

Usage

Country

{{#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)

Archive

{{#invoke:CIA World Factbook|archive|year=|date=|archive=}}

Generates a link to the annual archives of the CIA World Factbook.
|year= the year of the archive
|date= the date of the article in the Factbook (either on the article itself, or the access date of the article)
|archive= the url or date of the archive of the article.

If |archive= is non-empty, then nothing is produced. Else, if |year= is non-empty, then the link to that year's archive is produced. Else, if |date= is non-empty, it is parsed for a year and that year's archive is produced.

If the year is either the current year or the previous year, no archive is returned.


local p = {}
local getArgs = require('Module:Arguments').getArgs

-- prefix of all World Factbook pages
local factbookPrefix = 'https://www.cia.gov/the-world-factbook/'

--  Format of archive link. Both %d represent the year of the archive
local archiveFormat = ' [https://www.cia.gov/the-world-factbook/about/archives/download/factbook-%d.zip (Archived %d edition)]'

-- Function to turn a string into a URL fragment appropriate for CIA website
local function parseFragment(s)
	if not s then
		return ''
	end
	s = mw.ustring.lower(s)
	s = mw.ustring.gsub(s,' ','-')
	s = mw.ustring.gsub(s,',','')
	return s
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)
	if not args.country then
		return factbookPrefix
	end
	local result = factbookPrefix..'countries/'..parseFragment(args.country)
	if args.section then
		return result..'/#'..parseFragment(args.section)
	end
	return result
end

-- Function to fill in archive link:
-- Arguments:
--    args.archive: if non-empty, return nil
--    args.year: else if this is non-empty, use it for year
--    args.date: else if this is non-empty, parse it for a year
-- Returns:
--    the link, above, filled in with the year, or nil
function p._archive(args)
	if args.archive then
		return nil
	end
	local year = nil
	if args.year then
		year = tonumber(args.year)
	elseif args.date then
		i, j = mw.ustring.find(args.date,'20%d%d')
		if i and j then
			year = tonumber(mw.ustring.sub(args.date,i,j))
		end
	end
	if not year then
		return year
	end
	if year >= tonumber(os.date('%Y'))-1 then
		return false
	end
	return mw.ustring.format(archiveFormat, year, year)
end

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

function p.archive(frame)
	local args = getArgs(frame)
	return p._archive(args) or ''
end

return p