มอดูล:CIA World Factbook

จากวิกิพีเดีย สารานุกรมเสรี
Documentation icon คู่มือการใช้งานมอดูล[ดู] [แก้] [ประวัติ] [ล้างแคช]

ใช้ในแม่แบบ {{CIA World Factbook}} และ {{Cite CIA World Factbook}}

การใช้งาน[แก้]

Country[แก้]

{{#invoke:CIA World Factbook|country|country=|section=}}

สร้าง URL สำหรับรายการประเทศในซีไอเอเวิลด์แฟกต์บุ๊ก
|country= หัวข้อประเทศ (ตัวเลือก)
|section= สำหรับการเชื่อมโยงไปยังส่วนของบทความ (เช่น "People and Society") (ตัวเลือก)

Archive[แก้]

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

สร้างลิงก์ไปยังคลังเอกสารประจำปีของซีไอเอเวิลด์แฟกต์บุ๊ก
|year= ปีของคลังเอกสาร
|date= วันที่ของบทความในแฟกต์บุ๊ก (ทั้งของตัวบทความ หรือวันที่สืบค้นบทความ)
|archive= url หรือวันที่ของสำเนาในคลังเอกสารของบทความ

หากตัวแปรเสริม |archive= ไม่ว่างเปล่า จะไม่มีการสร้างผลลัพธ์ แต่หากตัวแปรเสริม |ปี= ไม่ว่างเปล่า ระบบจะสร้างลิงก์ไปยังที่เก็บถาวรของปีนั้น และหากตัวแปรเสริม |date= ไม่ว่างเปล่า ระบบจะแยกวิเคราะห์ว่าเป็นเวลาของปีไหนและสร้างลิงก์ไปยังที่เก็บถาวรของปีดังกล่าว

หากตัวแปรปีมีค่าเป็นปีปัจจุบันหรือปีก่อนหน้า จะไม่มีการคืนค่าลิงก์ไปยังคลังเอกสาร

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