모듈:London Gazette util

-- this module is created to support 
require('strict')
local getArgs = require('Module:Arguments').getArgs
local span_open = ;
local code_open = ;
local help_link = ' (도움말)';
local supp_error = mw.ustring.format ('%sinvalid %s|supp=%s', span_open, code_open, help_link);
local duplicate_page_error = mw.ustring.format (' %smore than one of %s|page= and %s|pages=%s', span_open, code_open, code_open, help_link);
local p = {}
----------------------------------------------------------------------------------------------
Whether variable is set or not.  A variable is set when it is not nil and not empty.
local function is_set( var )
	return not (var == nil or var == );
end
------------------------------------------------------------------------------
render a numerical text string in ordinal form suitable for English language use.  In this module, num_str is
limited by calling functions to the integer values 1-99.  The argument num_str must be known to be set before
this function is called.
local function ordinal_suffix (num_str)
	local lsd;																	-- least significant digit
	local suffixes = {'1' = 'st', '2' = 'nd', '3' = 'rd'};				-- table of suffixes except 'th'
	if num_str:match ('^11-3$') then											-- check the 11-13 odd balls first to get them out of the way
		return num_str .. 'th';													-- 11th, 12th, 13th
	end
	
	lsd = num_str:match ('^%d?(%d)$');											-- all other numbers: get the least significant digit
	return num_str .. (suffixeslsd or 'th');									-- append the suffix from the suffixes table or default to 'th'
end
--2 metadata.
function p.make_page_param (frame)
	local args = getArgs(frame);
	local page_or_pages;
	page_or_pages = args.page or args.unnamed or args.pages;					-- only one; prefer |page=
	
	if is_set (page_or_pages) then
		if 'pages' == args.param then
			if page_or_pages:match (',%-–') then								-- only for |pages= parameter
				page_or_pages = mw.ustring.gsub (page_or_pages, '%s*%-–%s*', '–');	-- hyphen to en dash; remove spaces
				return page_or_pages;											-- has separator character so make the parameter |pages=
			else
				return ;														-- no separator so value will be assigned to |page=
			end
		elseif 'page' == args.param then
			if page_or_pages:match (',%-–') then								-- only for |pages= parameter
				return ;														-- has separator so value will be assigned to |pages=
			else
				return page_or_pages;											-- no separator character so make the parameter |page=
			end
		else
			return ;															-- |param= something else
		end
	end
	return ;																	-- if here no pagination or not correct |page= or |pages= parameter
end
--2
citation's metadata.
function p.page_error (frame)
	local args = getArgs(frame);
	if is_set (args.page) and is_set (args.pages) then
		return duplicate_page_error;											-- both of |page= and |pages= are set
	else
		return ;
	end
end
--supp= values will set the type value.  These are: 'y' or a number 1-99.
row numbers in comments apply to the table in Template_talk:London_Gazette#Rewrite_as_wrapper_around_template:cite_news
function p.type_param (frame)
	local args = getArgs(frame);
	
	if not is_set (args'display-supp') and not is_set (args.supp) then		-- when both |display-supp= and |supp= are not set
		return ;																-- row 1 not a supplement so no display
	end
	
	args.supp = args.supp and args.supp:lower();								-- make text values lower case
	if not is_set (args'display-supp') and is_set (args.supp) then			-- when only |supp= is set
		if 'y' == args.supp then
			return 'Supplement';												-- row 2 the first or only supplement
		elseif args.supp:match ('^%d%d?$') then									-- one or two digits
				return ordinal_suffix (args.supp) .. ' supplement';				-- row 3 for the 1st-99th supplement
		else
			return supp_error;													-- row 4 any other text not supported show an error message
		end
	end
	if is_set (args'display-supp') and not is_set (args.supp) then			-- when only |display-supp= is set
		if args'display-supp':match ('^%d%d?$') then							-- one or two digits
			return ordinal_suffix (args'display-supp') .. ' supplement';		-- row 7 for the 1st-99th supplement
		elseif 'y' == args'display-supp' then
			return 'Supplement';												-- row 6 unnumbered supplement in /page/ space
		else
			return args'display-supp';										-- row 11 user specified text; supplement is not in supplement space (a Gazette website error)
		end
	end
																				-- here when both |display-supp= and |supp= are set
	if args'display-supp':match ('^%d%d?$') then								-- supplement number
		if 'y' == args.supp or (args'display-supp' == args.supp) then
			return ordinal_suffix (args'display-supp') .. ' supplement';		-- rows 8, 9
		else																	-- |supp= is not a number or number isn't same as number in |display-supp=
			return supp_error;													-- row 10 different values are ambiguous
		end
	else																		-- not a supplement number
		if ('y' == args.supp) and ('none' == args'display-supp') then
			return ;															-- row 5 for the case when a /page/ is in /supplement/ space 
		elseif ('y' == args.supp) or args.supp:match ('^%d%d?$') or not is_set (args.supp) then
			return args'display-supp';										-- rows 12, 13 user specified text
		else
			return supp_error;													-- row 14 any other |supp= value not supported show an error message
		end
	end
end
--city= value; defaults to London
local function url_city (city_param)
	local city_names = {'b' = 'Belfast', 'belfast' = 'Belfast', 'e' = 'Edinburgh', 'edinburgh' = 'Edinburgh'};
	
	city_param = city_param and city_param:lower();								-- lower() to index into the city_names table
	return city_namescity_param or 'London';									-- the city, or default to London
end
	
--supp= values will set the page number path element.  These are: 'y' or a number 1-99.
local function url_page (page, supp)
	if is_set (supp) then
		if ('y' == supp) or supp:match ('^%d%d?$') then
			page = page:match ('^%a?%a?%d+');									-- one or two letters followed by digits or just digits (supplement to issue 61608)
		else
			page = page:match ('^%d+');											-- |supp= set to an unexpected value, so one or more digits only
		end
	else
		page = page:match ('^%d+');												-- |supp= not set, so one or more digits only
	end
	
	return page or ;															-- at minimum return empty string for concatenation
end
--supp= values will set the page/supplement path element to /supplement/.  These are: 'y' or a number 1-99
local function url_page_or_supplement (supp_param)
	supp_param = (supp_param and supp_param:lower()) or ;						--make sure lower case for comparisons
	
	if ('y' == supp_param) or supp_param:match ('^%d%d?$') then
		return 'supplement';
	else
		return 'page';															-- anything else
	end
end
	
--url=
function p.url_param (frame)
	local args = getArgs(frame);
	local pg = args.page or args.pages or ;									-- first set parameter or empty string
	local url_table = {															-- a table of the various url parts
		'https://www.thegazette.co.uk',											-- static domain name
		url_city (args.city),													-- default to London
		'issue',																-- static path text
		args.issue,																-- issue number
		url_page_or_supplement (args.supp),										-- either of /page/ or /supplement/ according to the state of |supp=
		url_page (pg, args.supp)												-- pages: digits only; supplements: optional letter followed by 1 or more digits
		};
	
	return table.concat (url_table, '/');										-- concatenate all of the parts together and done
end
return p;