Lua support for the , , and templates and replacement of various supporting templates. 

]=]

require('strict')

local getArgs = require ('Module:Arguments').getArgs;
local unicode = require ("Module:Unicode data");								-- for is_latin and is_rtl
local yesno = require ('Module:Yesno');

local lang_data = mw.loadData ('Module:Lang/data');							-- language name override and transliteration tool-tip tables
local lang_name_table = lang_data.lang_name_table;								-- language codes, names, regions, scripts, suppressed scripts
local lang_table = lang_data.lang_name_table.lang;
local lang_dep_table = lang_data.lang_name_table.lang_dep;
local script_table = lang_data.lang_name_table.script;
local region_table = lang_data.lang_name_table.region;
local variant_table = lang_data.lang_name_table.variant;
local suppressed_table = lang_data.lang_name_table.suppressed;
local override_table = lang_data.override;

local synonym_table = mw.loadData ('Module:Lang/ISO 639 synonyms');				-- ISO 639-2/639-2T code translation to 639-1 code

local namespace = mw.title.getCurrentTitle.namespace;							-- used for categorization

local this_wiki_lang = mw.language.getContentLanguage.code;					-- get this wiki's language

local initial_style_state;														-- set by lang_xx_normal and lang_xx_italic

local maint_cats = {};															-- maintenance categories go here
local maint_msgs = {};															-- and their messages go here

----------------------------------------------------------------------------------------------

Returns true if argument is set; false otherwise. Argument is 'set' when it exists (not nil) or when it is not an empty string.


local function is_set( var )
	return not (var == nil or var == '');
end

--italic=unset, |italic=invert disables automatic italic markup. Individual leading/trailing
apostrophes are converted to their html numeric entity equivalent so that the new italic markup doesn't become
bold markup inadvertently.

Leading and trailing wiki markup is extracted from args.text into separate table elements. Addition, removal,
replacement of wiki markup is handled by a string.gsub replacement table operating only on these separate elements.
In the string.gsub matching pattern, '.*' matches empty string as well as the three expected wiki markup patterns.

This function expects that markup in args.text is complete and correct; if it is not, oddness may result.


local function invert_italics (source)
	local invert_pattern_table = {												-- leading/trailing markup add/remove/replace patterns
		[""]="\'\'",															-- empty string becomes italic markup
		["\'\'"]="",															-- italic markup becomes empty string
		["\'\'\'"]="\'\'\'\'\'",												-- bold becomes bold italic
		["\'\'\'\'\'"]="\'\'\'",												-- bold italic become bold
	local seg = {};

	source = source:gsub ;							-- protect single quote marks from being interpreted as bold markup

	seg[1] = source:match ('^(\'\'+%f[^\']).+') or '';							-- get leading markup, if any; ignore single quote
	seg[3] = source:match ('.+(%f[\']\'\'+)$') or '';							-- get trailing markup, if any; ignore single quote

	if '' ~= seg[1] and '' ~= seg[3] then										-- extract the 'text' 
		seg[2] = source:match ('^\'\'+%f[^\'](.+)%f[\']\'\'+$')					-- from between leading and trailing markup
	elseif '' ~= seg[1] then
		seg[2] = source:match ('^\'\'+%f[^\'](.+)')								-- following leading markup
	elseif '' ~= seg[3] then
		seg[2] = source:match ('(.+)%f[\']\'\'+$')								-- preceding trailing markup
	else
		seg[2] = source															-- when there is no markup
	end

	seg[1] = invert_pattern_table[seg[1]] or seg[1];							-- replace leading markup according to pattern table
	seg[3] = invert_pattern_table[seg[3]] or seg[3];							-- replace leading markup according to pattern table

	return table.concat (seg);													-- put it all back together and done
end

--italic= or |italics= assigned values.

When |italic= is set and has an acceptible assigned value, return the matching css font-style property value or,
for the special case 'default', return nil.

When |italic= is not set, or has an unacceptible assigned value, return nil and a nil error message.

When both |italic= and |italics= are set, returns nil and a 'conflicting' error message.

The return value nil causes the calling lang, lang_xx, or transl function to set args.italic according to the template's
defined default  or to the value appropriate to |script=, if set (
and only).

Accepted values and the values that this function returns are are:
	nil		-	when |italic= absent or not set; returns nil
	default	-	for completeness, should rarely if ever be used; returns nil
	yes		-	force args.text to be rendered in italic font; returns 'italic'
	no		-	force args.text to be rendered in normal font; returns 'normal'
	unset	-	disables font control so that font-style applied to text is dictated by markup inside or outside the template; returns 'inherit'
	invert	-	disables font control so that font-style applied to text is dictated by markup outside or inverted inside the template; returns 'invert'


local function validate_italic (args)
	local properties = {['yes'] = 'italic', ['no'] = 'normal', ['unset'] = 'inherit', ['invert'] = 'invert', ['default'] = nil};
	
	local count = 0
	for _, arg in pairs {'italic', 'italics', 'i'} do
		if args[arg] then
			count = count + 1
		end
	end

	if count > 1 then															-- return nil and an error message if more than one is set 
		return nil, 'only one of |italic=, |italics=, or |i= can be specified';
	end
	
	return properties[args.italic or args.italics or args.i], nil;				-- return an appropriate value and a nil error message
end

--[=[------------------------------------------------------------------------------------

Default behavior of the and templates is to add categorization when the templates are used in mainspace.
This default functionality may be suppressed by setting |nocat=yes or |cat=no. This function selects one of these two parameters
to control categorization.

Because having two parameters with 'opposite' names and 'opposite' values is confusing, this function accepts only affirmative
values for |nocat= and only negative values for |cat=; in both cases the 'other' sense (and non-sense) is not accepted and the
parameter is treated as if it were not set in the template.

Sets args.nocat to true if categorization is to be turned off; to nil if the default behavior should apply.

Accepted values for |nocat= are the text strings:
	'yes', 'y', 'true', 't', on, '1'		-- Module:Yesno returns logical true for all of these; false or nil else
for |cat=
	'no', 'n', 'false', 'f', 'off', '0'		-- Module:Yesno returns logical false for all of these; true or nil else

]=]

local function validate_cat_args (args)
	if not (args.nocat or args.cat) then										-- both are nil, so categorize
		return;
	end
	
	if false == yesno (args.cat) or true == yesno (args.nocat) then
		args.nocat = true;														-- set to true when args.nocat is affirmative; nil else (as if the parameter were not set in the template)
	else																		-- args.nocat is the parameter actually used.
		args.nocat = nil;
	end
end

------------------------------------------------------------------------------------------

Whether needle is in haystack


local function in_array 
	if needle == nil then
		return false;
	end
	for n,v in ipairs( haystack ) do
		if v == needle then
			return n;
		end
	end
	return false;
end

----------------------------------------------------------------------------

prettify ietf tags to use recommended subtag formats:
	code: lower case
	script: sentence case
	region: upper case
	variant: lower case
	private: lower case prefixed with -x-
	

local function format_ietf_tag 
	local out = {};

	if is_set (private) then
		return table.concat ;		-- if private, all other tags ignored
	end
	
	table.insert ;
	if is_set (script) then
		script = script:lower:gsub ;
		table.insert ;
	end

	if is_set (region) then
		table.insert ;
	end
	
	if is_set (variant) then
		table.insert ;
	end
	
	return table.concat ;
end

--script=, |region=, and |variant= are supported (not supported in 
because those parameters are superfluous to the IETF subtags in |code=)

returns six values; all lower case. Valid parts are returned as themselves; omitted parts are returned as empty strings, invalid
parts are returned as nil; the sixth returned item is an error message (if an error detected) or nil.

see http://www.rfc-editor.org/rfc/bcp/bcp47.txt section 2.1


local function get_ietf_parts 
	local code, script, region, variant, private;								-- ietf tag parts

	if not is_set (source) then
		return nil, nil, nil, nil, nil, 'missing language tag';
	end

	local pattern = {															-- table of tables holding acceptibe ietf tag patterns and short names of the ietf part captured by the pattern
		
		
		
		
		
		
		
		

	local t = {};																-- table of captures; serves as a translator between captured ietf tag parts and named variables

	for i, v in ipairs (pattern) do												-- spin through the pattern table looking for a match
		local c1, c2, c3, c4;													-- captures in the 'pattern' from the pattern table go here
	
		c1, c2, c3, c4 = source:match (pattern[i][1]);							-- one or more captures set if source matches pattern[i])
			if c1 then															-- c1 always set on match
				code = c1;														-- first capture is always code
				t = {
					[pattern[i][2] or 'x'] = c2,								-- fill the table of captures with the rest of the captures
					[pattern[i][3] or 'x'] = c3,								-- take index names from pattern table and assign sequential captures
					[pattern[i][4] or 'x'] = c4,								-- index name may be nil in pattern[i] table so "or 'x'" spoofs a name for this index in this table
				script = t.s or '';												-- translate table contents to named variables;
				region = t.r or '';												-- absent table entries are nil so set named ietf parts to empty string for concatenation
				variant= t.v or '';
				private = t.p or '';
				break;															-- and done
			end
	end

	if not code then
		return nil, nil, nil, nil, nil, table.concat ;		-- don't know what we got but it is malformed
	end

	code = code:lower;														-- ensure that we use and return lower case version of this
	
	if not (override_table[code] or lang_table[code] or synonym_table[code] or lang_dep_table[code]) then
		return nil, nil, nil, nil, nil, table.concat ;		-- invalid language code, don't know about the others (don't care?)
	end
	
	if synonym_table[code] then													-- if 639-2/639-2T code has a 639-1 synonym
		table.insert (maint_cats, table.concat );
		table.insert (maint_msgs, table.concat );
		code = synonym_table[code];												-- use the synonym
	end

	if is_set (script) then
		if is_set (args_script) then
			return code, nil, nil, nil, nil, 'redundant script tag';			-- both code with script and |script= not allowed
		end
	else
		script = args_script or '';												-- use args.script if provided
	end 

	if is_set (script) then
		script = script:lower;												-- ensure that we use and return lower case version of this
		if not script_table[script] then
			return code, nil, nil, nil, nil, table.concat ;	-- language code ok, invalid script, don't know about the others (don't care?)
		end
	end
	if suppressed_table[script] then											-- ensure that code-script does not use a suppressed script
		if in_array  then
			return code, nil, nil, nil, nil, table.concat ;	-- language code ok, script is suppressed for this code
		end
	end

	if is_set (region) then
		if is_set (args_region) then
			return code, nil, nil, nil, nil, 'redundant region tag';			-- both code with region and |region= not allowed
		end
	else
		region = args_region or '';												-- use args.region if provided
	end 

	if is_set (region) then
		region = region:lower;												-- ensure that we use and return lower case version of this
		if not region_table[region] then
			return code, script, nil, nil, nil, table.concat ;
		end
	end
	
	if is_set (variant) then
		if is_set (args_variant) then
			return code, nil, nil, nil, nil, 'redundant variant tag';			-- both code with variant and |variant= not allowed
		end
	else
		variant = args_variant or '';											-- use args.variant if provided
	end 

	if is_set (variant) then
		variant = variant:lower;												-- ensure that we use and return lower case version of this
		if not variant_table[variant] then										-- make sure variant is valid
			return code, script, region, nil, nil, table.concat ;
		end																		-- does this duplicate/replace tests in lang and lang_xx?
		if is_set (script) then													-- if script set it must be part of the 'prefix'
			if not in_array (table.concat , variant_table[variant]['prefixes']) then
				return code, script, region, nil, nil, table.concat ;
			end
		elseif is_set (region) then												-- if region set, there are some prefixes that require lang code and region (en-CA-newfound)
			if not in_array  then		-- first see if lang code is all that's required (en-oxendict though en-GB-oxendict is preferred)
				if not in_array (table.concat , variant_table[variant]['prefixes']) then		-- now try for lang code and region (en-CA-newfound)
					return code, script, region, nil, nil, table.concat ;
				end
			end
		else																	-- cheap way to determine if there are prefixes; fonipa and others don't have prefixes; # operator always returns 0
			if variant_table[variant]['prefixes'][1] and not in_array  then
				return code, script, region, nil, nil, table.concat ;
			end
		end
	end

	if is_set (private) then
		private = private:lower;												-- ensure that we use and return lower case version of this
		if not override_table[table.concat ] then		-- make sure private tag is valid; note that index 
			return code, script, region, nil, nil, table.concat ;
		end
	end
	return code, script, region, variant, private, nil;							-- return the good bits; make sure that msg is nil
end

------------------------------------------------------------------------------

assembles an error message from template name, message text, help link, and error category.


local function make_error_msg 
	local out = {};
	local category;
	
	if 'Transl' == template then
		category = 'Transl';
	else
		category = 'Lang and lang-xx'
	end
	
	table.insert (out, table.concat );	-- for error messages output args.text if available
	table.insert (out, table.concat ({'');
	
	if (0 == namespace or 10 == namespace) and not args.nocat then						-- categorize in article space (and template space to take care of broken usages)
		table.insert (out, table.concat );
	end

	return table.concat (out);
end
	

--[=[-----------------------------------------------------------------------------

Makes a wikilink; when both link and display text is provided, returns a wikilink in the form D; if only
link is provided, returns a wikilink in the form L; if neither are provided or link is omitted, returns an
empty string.

]=]

local function make_wikilink 
	if is_set (link) then
		if is_set (display) then
			return table.concat ;
		else
			return table.concat ;
		end
	else
		return '';
	end
end

------------------------------------------------------------------------------

adds  text. mixed not supported


local function div_markup_add 
local implied_p = {};

	if text:find ('^\n[%*:;#]') then											-- look for list markup; list markup must begin at start of text
		if 'italic' == style then
			return mw.ustring.gsub (text, '(\n[%*:;#]+)([^\n]+)', '%1');	-- insert italic markup at each list item
		else
			return text;
		end
	end

	if text:find ('\n+') then													-- look for any number of \n characters in text
		text = text:gsub ('([^\n])\n([^\n])', '%1 %2');							-- replace single newline characters with a space character which mimics mediawiki

		if 'italic' == style then
			text = text:gsub;						-- insert p and italic markup tags at each impled p (two or more consecutive '\n\n' sequences)
		else
			text = text:gsub ;							-- insert p markup at each impled p
			text = text:gsub ;										-- strip newline characters
		end
	end

	return text;
end

------------------------------------------------------------------------------

Add the html markup to text according to the type of content that it is:  tags for inline content or
 tags for block content


local function make_text_html 
	local html = {};
	local style_added = '';

	if text:match ('^%*') then
		table.insert ;											-- move proto language text prefix outside of italic markup if any; use numeric entity because plain splat confuses MediaWiki
		text = text:gsub ;											-- remove the splat from the text
	end

	if 'span' == tag then														-- default html tag for inline content
		if 'italic' == style then												-- but if italic
			tag = 'i';															-- change to  tags
		end
	else																		-- must be div so go
		text = div_markup_add ;									-- handle implied 
	end

	table.insert (html, table.concat ({' html tag
	code = code:gsub ;												-- strip private use subtag from code tag because meaningless outside of wikipedia
	table.insert (html, table.concat );				-- add language attribute

	if rtl or unicode.is_rtl(text) then
		table.insert ;										-- add direction attribute for right to left languages
	end

	if 'normal' == style then													-- when |italic=no
		table.insert ;						-- override external markup, if any
		style_added = '\"';														-- remember that style attribute added and is not yet closed
	end

	if is_set (size) then														-- when |size=
		if is_set (style_added) then
			table.insert (html, table.concat );		-- add when style attribute already inserted
		else
			table.insert (html, table.concat );	-- create style attribute
			style_added = '\"';													-- remember that style attribute added and is not yet closed
		end
	end

	if is_set (language) then
		table.insert (html, table.concat );	--start the title text
		if language:find ('languages') then
			table.insert ;							-- for collective languages
		else
			table.insert ;								-- for individual languages
		end
		table.insert ;												-- close the opening html tag
	else
		table.insert (html, table.concat );					-- close the style attribute and close opening html tag
	end
	table.insert ;													-- insert the text

	table.insert (html, table.concat ({' html tag

	if rtl then																	-- legacy; shouldn't be necessary because all of the rtl text is wrapped inside an html tag with dir="rtl" attribute 
		table.insert ;											-- make sure the browser knows that we're at the end of the rtl
	end
	return table.concat (html);													-- put it all together and done
end

--[=[-----------------------------------------------------------------------------

For individual language, , returns:
	Category:Articles containing -language text

for English:
	Category:Articles containing explicitly cited English-language text
	
for ISO 639-2 collective languages (and for 639-1 bh):
	Category:Articles with text in  languages

]=]

local function make_category 
	local cat = {};
	local retval;

	if ((0 ~= namespace) or nocat) and not name_get then						-- only categorize in article space
		return '';																-- return empty string for concatenation
	end

	--if language_name:find ('languages') then
	--	return table.concat ;
	--end
		
	table.insert ;

	--if 'en' == code then
	--	table.insert ;				-- falls back to English if regional name not available
	--else
		table.insert ;
	--end
	
	table.insert ;

	return table.concat (cat);
end

--script= is not used in for this purpose; instead it uses |code. Because language scripts
are listed in the switches they are included in the data tables. The script parameter is introduced
at . If |script= is set, this function uses it in preference to code.

To avoid confusion, in this module and the templates that use it, the transliteration script parameter is renamed
to be |translit-script= 

This function is used by both lang_xx and transl
	lang_xx always provides code, language_name, and translit; may provide tscript; never provides style
	transl always provides language_name, translit, and one of code or tscript, never both; always provides style

For , style only applies when a language code is provided

local function make_translit 
	local title;
	local tout = {};
	local title_table = lang_data.translit_title_table;							-- table of transliteration standards and the language codes and scripts that apply to those standards
	
	if is_set (code) then														-- when a language code is provided 
		if not style then														-- nil for the default italic style
			table.insert (tout, " tag
		else
			table.insert (tout, table.concat );	-- non-standard style, construct a span tag for it
		end
		table.insert ;
		table.insert ;								-- transliterations are always Latin script
	else
		table.insert ;									-- when no language code: no lang= attribute, not italic ( only)
	end
	
	std = std and std:lower;													-- lower case for table indexing
	
	if not is_set (std) and not is_set (tscript) then							-- when neither standard nor script specified
		table.insert ;										-- write a generic tool tip
		if not language_name:find ('languages') then							-- collective language names (plural 'languages' is part of the name)
			table.insert 									-- skip this text (individual and macro languages only)
		end
		table.insert ;									-- finish the tool tip; use romanization when neither script nor standard supplied
	elseif is_set (std) and is_set (tscript) then								-- when both are specified
		if title_table[std] then												-- and if standard is legitimate
			if title_table[std][tscript] then									-- and if script for that standard is legitimate
				table.insert (tout, table.concat ({title_table[std][tscript:lower], '  transliteration'}));	-- add the appropriate text to the tool tip
			else
				table.insert ;				-- use the default if script not in std table; TODO: maint cat? error message because script not found for this standard?
			end
		else
			return '';															-- invalid standard, setup for error message
		end

	elseif is_set (std) then													-- translit-script not set, use language code
		if not title_table[std] then return ''; end								-- invalid standard, setup for error message
		
		if title_table[std][code] then											-- if language code is in the table (transl may not provide a language code)
--			table.insert (tout, table.concat ({title_table[std][code:lower], '  transliteration'}));	-- add the appropriate text to the tool tip
			table.insert (tout, table.concat ({title_table[std][code:lower], '  transliteration'}));	-- add the appropriate text to the tool tip
		else																	-- code doesn't match
			table.insert ;					-- so use the standard's default
		end
	else																		-- here if translit-script set but translit-std not set
		if title_table['no_std'][tscript] then
			table.insert ;				-- use translit-script if set
		elseif title_table['no_std'][code] then
			table.insert ;					-- use language code
		else
			if is_set (tscript) then
				table.insert (tout, table.concat );	-- write a script tool tip
			elseif is_set (code) then
				if not language_name:find ('languages') then					-- collective language names (plural 'languages' is part of the name)
					table.insert 							-- skip this text (individual and macro languages only)
				end
				table.insert ;						-- finish the tool tip
			else
				table.insert ;						-- generic tool tip (can we ever get here?)
			end
		end
	end

	table.insert ;
	table.insert ;
	if is_set (code) and not style then											-- when a language code is provided 
		table.insert ;											-- no language code so close the span tag
	end
	return table.concat (tout);
end

--italic= template parameter so italic markup should never appear in args.text
either as ''itself'' or as **''bold italic**'' unless |italic=unset or |italic=invert.


local function validate_text 
	if not is_set (args.text) then
		return make_error_msg ;
	end

	if args.text:find ("%f[\']\'\'\'\'%f[^\']") or args.text:find ("\'\'\'\'\'[\']+") then	-- because we're looking, look for 4 appostrophes or 6+ appostrophes
		return make_error_msg ;
	end

	local style = args.italic;

	if ('unset' ~= style) and ('invert' ~=style) then
		if args.text:find ("%f[\']\'\'%f[^\']") or args.text:find ("%f[\']\'\'\'\'\'%f[^\']") then	-- italic but not bold, or bold italic
			return make_error_msg ;
		end
	end
end

----------------------------------------------------------------------------------

render mainenance messages and categories


local function render_maint(nocat)
	local maint = {};
	
	if 0 < #maint_msgs then														-- when there are maintenance messages
		table.insert (maint, table.concat ({'');										-- close the span
	end
	
	if (0 < #maint_cats) and (0 == namespace) and not nocat then				-- when there are maintenance categories; article namespace only
		for _, cat in ipairs (maint_cats) do
			table.insert (maint, table.concat );	-- format and add the categories
		end
	end
	
	return table.concat (maint);
end

----------------------------------------------------------------------------------

for proto languages, text is prefixed with a splat. We do that here as a flag for make_text_html so that a splat
will be rendered outside of italic markup (if used). If the first character in text here is already a splat, we
do nothing

proto_param is boolean or nil; true adds splat prefix regardless of language name; false removes and / or inhibits
regardless of language name; nil does nothing; presumes that the value in text is correct but removes extra splac


local function proto_prefix 
	if false == proto_param then												-- when forced by |proto=no
		return text:gsub ;											-- return text without splat prefix regardless of language name or existing splat prefix in text
	elseif (language_name:find ('^Proto%-') or (true == proto_param)) then		-- language is a proto or forced by |proto=yes
		return text:gsub ;											-- prefix proto-language text with a splat; also removes duplicate prefixing splats
	end
	
	return text:gsub ;												-- return text unmolested except multiple splats reduced to one splat
end

----------------------------------------------------------------------------------

looks for a poem strip marker in text; returns true when found; false else

auto-italic detection disabled when text has poem stripmarker because it is not possible for this code to know
the content that will replace the stripmarker.


local function has_poem_tag (text)
	return text:find ('\127[^\127]*UNIQ%-%-poem%-[%a%d]+%-QINU[^\127]*\127') and true or false;
end

----------------------------------------------------------------------------

Inspects content of and selectively trims text. Returns text and the name of an appropriate html tag for text.

If text contains:
	\n\n	text has implied  tags - trim leading and trailing whitespace and return 
If text begins with list markup:
	\n*		unordered
	\n;		definition
	\n:		definition
	\n#		ordered
trim all leading whitespace except \n and trim all trailing whitespace

If text contains  tags because 
the stripmarker is replaced with text wrapped in  tags.


local function html_tag_select (text)
	local tag;
	
	if has_poem_tag (text) then													-- contains poem stripmarker (we can't know the content of that)
		tag = 'div';															-- poem replacement is in div tags so lang must use div tags
	elseif mw.text.trim (text):find ('\n\n+') then								-- contains implied p tags
		text = mw.text.trim (text);												-- trim leading and trailing whitespace characters
		tag = 'div';															-- must be div because span may not contain p tags (added later by MediaWiki); poem replacement is in div tags
	elseif text:find ('\n[%*:;%#]') then										-- if text has list markup
		text = text:gsub :gsub ;					-- trim all whitespace except leading newline character '\n'
		tag = 'div';															-- must be div because span may not contain ul, dd, dl, ol tags (added later by MediaWiki)
	else
		text = mw.text.trim (text);												-- plain text
		tag = 'span';															-- so span is fine
	end
	
	return text, tag;
end

--proto=; permitted values are yes and no; yes returns as true, no returns as false,
empty string (or parameter omitted) returns as nil; any other value returns as nil with a second return value of
true indicating that some other value has been assigned to |proto=


local function validate_proto (proto_param)
	if 'yes' == proto_param then
		return true;
	elseif 'no' == proto_param then
		return false;
	elseif is_set (proto_param) then
		return nil, true;														-- |proto= something other than 'yes' or 'no'
	else
		return nil;																-- missing or empty
	end
end

------------------------------------------------------------------------

common function to return language name from the data set according to IETF tag

returns language name if found in data tables; nil else


local function language_name_get 
	ietf = ietf:lower;														-- ietf:lower because format_ietf_tag returns mixed case
	
	local name;																	-- remains nil if not found
	
	if override_table[ietf] then												-- look for whole IETF tag in override table
		name = override_table[ietf][1];	
	elseif override_table[code] then											-- not there so try basic language code
		name = override_table[code][1];
	elseif lang_table[code] then												-- shift to iana code/name table
		name = lang_table[code][1];												-- table entries sometimes have multiple names, always take the first one
	elseif lang_dep_table[code] then
--		if cat then
--			table.insert (maint_cats, table.concat );
--			table.insert (maint_msgs, table.concat );
--		end
		name = lang_dep_table[code][1];											-- table entries sometimes have multiple names, always take the first one
	end

	if lang_dep_table[code] and cat then										-- because deprecated code may have been overridden to en.wiki preferred name
		table.insert (maint_cats, table.concat );
		table.insert (maint_msgs, table.concat );
	end

	return name;																-- name from data tables or nil
end

------------------------------------------------------------------------------------------------

entry point for 

there should be no reason to set parameters in the 
	

parameters are received from the template's frame (parent frame)


local function _lang (args)
	local out = {};
	local language_name;														-- used to make category names
	local category_name;														-- same as language_name except that it retains any parenthetical disambiguators (if any) from the data set
	local subtags = {};															-- IETF subtags script, region, variant, and private
	local code;																	-- the language code
	local msg;																	-- for error messages
	local tag = 'span';															-- initial value for make_text_html
	local template = args.template or 'Lang';

	validate_cat_args (args);													-- determine if categorization should be suppressed

	if args[1] and args.code then
		return make_error_msg ;
	else
		args.code = args[1] or args.code;										-- prefer args.code
	end

	if args[2] and args.text then
		return make_error_msg ;
	else
		args.text = args[2] or args.text;										-- prefer args.text
	end
	
	msg = validate_text ;										-- ensure that |text= is set
	if is_set (msg) then														-- msg is an already-formatted error message
		return msg;
	end
	
	args.text, tag = html_tag_select (args.text);								-- inspects text; returns appropriate html tag with text trimmed accordingly

	args.rtl = args.rtl == 'yes';												-- convert to boolean: 'yes' -> true, other values -> false

	args.proto, msg = validate_proto (args.proto);								-- return boolean, or nil, or nil and error message flag
	if msg then
		return make_error_msg (table.concat , args, template);
	end

	code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (args.code);	-- |script=, |region=, |variant= not supported because they should be part of args.code (} in )

	if msg then
		return make_error_msg ;
	end

	args.italic, msg = validate_italic (args);
	if msg then
		return make_error_msg ;
	end

	if nil == args.italic then													-- nil when |italic= absent or not set or |italic=default; args.italic controls
		if ('latn' == subtags.script) or								 		-- script is latn
			(this_wiki_lang ~= code and not is_set (subtags.script) and not has_poem_tag (args.text) and unicode.is_Latin (args.text)) then -- text not this wiki's language, no script specified and not in poem markup but is wholly latn script (auto-italics)
				args.italic = 'italic';											-- DEFAULT for templates is upright; but if latn script set for font-style:italic
		else
			args.italic = 'inherit';											-- italic not set; script not latn; inherit current style
		end
	end
	
	if is_set (subtags.script) then												-- if script set, override rtl setting
		if in_array  then
			args.rtl = true;													-- script is an rtl script
		else
			args.rtl = false;													-- script is not an rtl script
		end
	end

	args.code = format_ietf_tag ;	-- format to recommended subtag styles
	language_name = language_name_get ;					-- get language name; try ietf tag first, then code w/o variant then code w/ variant

	if 'invert' == args.italic and 'span' == tag then							-- invert only supported for in-line content
		args.text = invert_italics (args.text)
	end

	args.text = proto_prefix ;			-- prefix proto-language text with a splat

	table.insert (out, make_text_html );
	table.insert (out, make_category );
	table.insert (out, render_maint(args.nocat));								-- maintenance messages and categories

	return table.concat (out);													-- put it all together and done
end

--------------------------------------------------------------------------------------------------

entry point for 

there should be no reason to set parameters in the 
	

parameters are received from the template's frame (parent frame)


local function lang (frame)
	local args = getArgs (frame, {												-- this code so that we can detect and handle wiki list markup in text
		valueFunc = function 
			if 2 == key or 'text' == key then									-- the 'text' parameter; do not trim wite space
				return value;													-- return untrimmed 'text'
			elseif value then													-- all other values: if the value is not nil
				value = mw.text.trim (value);									-- trim whitespace
				if '' ~= value then												-- empty string when value was only whitespace
					return value;
				end
			end
			return nil;															-- value was empty or contained only whitespace
		end																		-- end of valueFunc

	return _lang (args);
end

--script= as well.

For each template choose the appropriate entry-point function so that this function knows the default
styling that should be applied to text.

For normal, upright style:
	
For italic style:
	

All other parameters should be received from the template's frame (parent frame)

Supported parameters are:
				any other text replaces language-name label - automatic wikilinking disabled
	
	for those templates that support transliteration (those templates where |text= is not entirely latn script):

For , the positional parameters are:
no other positional parameters are allowed


local function _lang_xx (args)
	local out = {};
	local language_name;														-- used to make display text, article links
	local category_name;														-- same as language_name except that it retains any parenthetical disambiguators (if any) from the data set
	local subtags = {};															-- IETF subtags script, region, and variant
	local code;																	-- the language code

	local translit_script_name;													-- name associated with IANA (ISO 15924) script code
	local translit;
	local translit_title;
	local msg;																	-- for error messages
	local tag = 'span';															-- initial value for make_text_html
	local template = args.template or 'Lang-xx';

	if args[1] and args.text then
		return make_error_msg ;
	else
		args.text = args[1] or args.text;										-- prefer args.text
	end
	
	msg = validate_text ;										-- ensure that |text= is set, does not contain italic markup and is protected from improper bolding
	if is_set (msg) then
		return msg;
	end

	args.text, tag = html_tag_select (args.text);								-- inspects text; returns appropriate html tag with text trimmed accordingly

	if args[2] and args.translit then
		return make_error_msg ;
	else
		args.translit = args[2] or args.translit								-- prefer args.translit
	end
	
	if args[3] and (args.translation or args.lit) then
		return make_error_msg ;
	elseif args.translation and args.lit then
		return make_error_msg ;
	else
		args.translation = args[3] or args.translation or args.lit;				-- prefer args.translation
	end

	if args.links and args.link then
		return make_error_msg ;
	else
		args.link = args.link or args.links;									-- prefer args.link
	end

	validate_cat_args (args);													-- determine if categorization should be suppressed

	args.rtl = args.rtl == 'yes';												-- convert to boolean: 'yes' -> true, other values -> false

	code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts ;	-- private omitted because private

	if msg then																	-- if an error detected then there is an error message
		return make_error_msg ;
	end
	
	args.italic, msg = validate_italic (args);
	if msg then
		return make_error_msg ;
	end

	if nil == args.italic then													-- args.italic controls
		if is_set (subtags.script) then
			if 'latn' == subtags.script then
				args.italic = 'italic';											-- |script=Latn; set for font-style:italic
			else
				args.italic = initial_style_state;								-- italic not set; script is not latn; set for font-style:
			end
		else
			args.italic = initial_style_state;									-- here when |italic= and |script= not set; set for font-style:
		end
	end
	
	if is_set (subtags.script) then												-- if script set override rtl setting
		if in_array  then
			args.rtl = true;													-- script is an rtl script
		else
			args.rtl = false;													-- script is not an rtl script
		end
	end

	args.proto, msg = validate_proto (args.proto);								-- return boolean, or nil, or nil and error message flag
	if msg then
		return make_error_msg (table.concat , args, template);
	end

	args.code = format_ietf_tag ;	-- format to recommended subtag styles
	language_name = language_name_get ;					-- get language name; try ietf tag first, then code w/o variant then code w/ variant

	category_name = language_name;												-- category names retain IANA parenthetical diambiguators (if any)
	language_name = language_name:gsub ;							-- remove IANA parenthetical disambiguators or qualifiers from names that have them

	if args.label then
		if 'none' ~= args.label then
			table.insert (out, table.concat );				-- custom label
		end
	else
		if 'no' == args.link then
			table.insert ;									-- language name without wikilink
		else
			if language_name:find ('languages') then
				table.insert (out, make_wikilink (language_name));				-- collective language name uses simple wikilink
			elseif lang_data.article_name[code] then
				table.insert (out, make_wikilink );	-- language name with wikilink from override data
			else
				table.insert (out, make_wikilink );	-- language name with wikilink
			end
		end
		table.insert ;												-- separator
	end

	if 'invert' == args.italic then
		args.text = invert_italics (args.text)
	end
	
	args.text = proto_prefix ;			-- prefix proto-language text with a splat

	table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size, ('none' == args.label) and language_name or nil))

	if is_set (args.translit) and not unicode.is_Latin (args.text) then			-- transliteration (not supported in ); not supported when args.text is wholly latn text (this is an imperfect test)
		table.insert ;												-- comma to separate text from translit
		if 'none' ~= args.label then
			table.insert ;								-- close the small tag
		end
		
		translit = make_translit 
		if is_set (translit) then
			table.insert ;
		else
			return make_error_msg (table.concat , args, template);
		end
	end
	
	if is_set (args.translation) then											-- translation (not supported in )
		table.insert ;
		if 'none' ~= args.label then
			table.insert ;
			if 'no' == args.link then
				table.insert ;
		end
		table.insert (out, table.concat );	-- use html entities to avoid wiki markup confusion
	end
	
	table.insert (out, make_category );
	table.insert (out, render_maint(args.nocat));								-- maintenance messages and categories

	return table.concat (out);													-- put it all together and done
end

--------------------------------------------------------------------------

common function to get args table from templates

returns table of args


local function lang_xx_args_get (frame)
	local args = getArgs(frame,
		parentFirst= true,														-- parameters in the template override parameters set in the 
		valueFunc = function 
			if 1 == key then													-- the 'text' parameter; do not trim wite space
				return value;													-- return untrimmed 'text'
			elseif value then													-- all other values: if the value is not nil
				value = mw.text.trim (value);									-- trim whitespace
				if '' ~= value then												-- empty string when value was only whitespace
					return value;
				end
			end
			return nil;															-- value was empty or contained only whitespace
		end																		-- end of valueFunc

	return args;
end

------------------------------------------------------------------------------

Entry point for those templates that call lang_xx_italic. Sets the initial style state to italic.


local function lang_xx_italic (frame)
	local args = lang_xx_args_get (frame);
	
	initial_style_state = 'italic';
	return _lang_xx (args);
end

----------------------------------------------------------------------------

Entry point ffrom another module. Sets the initial style state to italic.


local function _lang_xx_italic (args)
	initial_style_state = 'italic';
	return _lang_xx (args);
end

----------------------------------------------------------------------------

Entry point for those templates that call lang_xx_inherit. Sets the initial style state to inherit.


local function lang_xx_inherit (frame)
	local args = lang_xx_args_get (frame);

	initial_style_state = 'inherit';
	return _lang_xx (args);
end

--------------------------------------------------------------------------

Entry point from another module. Sets the initial style state to inherit.


local function _lang_xx_inherit (args)
	initial_style_state = 'inherit';
	return _lang_xx (args);
end

----------------------------------------------------------------------------------

Returns true when a language name associated with IETF language tag exists; nil else. IETF language tag must be valid.

All code combinations supported by and the templates are supported by this function.

Module entry point from another module


local function _is_ietf_tag (tag)												-- entry point when this module is required into another
	local c, s, r, v, p, err;													-- code, script, region, variant, private, error message
	c, s, r, v, p, err = get_ietf_parts (tag);									-- disassemble tag into constituent part and validate

	return ((c and not err) and true) or nil;									-- return true when code portion has a value without error message; nil else
end

------------------------------------------------------------------------------------

Module entry point from an 


local function is_ietf_tag (frame)
	return _is_ietf_tag (getArgs (frame)[1]);									-- args[1] is the ietf language tag to be tested; also get parent frame
end

------------------------------------------------------------------------

Module entry point from an ; same as is_ietf_tag except does not get parameters from the parent
(template) frame. This function not useful when called by because 
is in the parent frame.


local function is_ietf_tag_frame (frame)
	return _is_ietf_tag (getArgs [1]);				-- args[1] is the ietf language tag to be tested; do not get parent frame
end

--link= parameter to yes to get wikilinked version of the language name.

Module entry point from another module


local function _name_from_tag (args)
	local subtags = {};															-- IETF subtags script, region, variant, and private
	local raw_code = args[1];													-- save a copy of the input IETF subtag
	local link = 'yes' == args['link'];											-- make a boolean
	local label = args.label;
	local code;																	-- the language code
	local msg;																	-- gets an error message if IETF language tag is malformed or invalid
	local language_name = '';
	
	code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (raw_code);
	if msg then
		local template = (args['template'] and table.concat ({': '})) or '';	-- make template name (if provided by the template)
		return table.concat ({''});
	end

	raw_code = format_ietf_tag ;	-- format to recommended subtag styles; private omitted because private
	language_name = language_name_get ;							-- get language name; try ietf tag first, then code w/o variant then code w/ variant

	language_name = language_name:gsub ;							-- remove IANA parenthetical disambiguators or qualifiers from names that have them

	if link then																-- when |link=yes, wikilink the language name
		if language_name:find ('languages') then
			language_name = make_wikilink ;				-- collective language name uses simple wikilink
		elseif lang_data.article_name[code] then
			language_name = make_wikilink ;	-- language name with wikilink from override data
		else
			language_name = make_wikilink ;	-- language name with wikilink
		end
	end

	return language_name;
end

--------------------------------------------------------------------------------

Module entry point from an 


local function name_from_tag (frame)											-- entry point from an 
	return _name_from_tag (getArgs(frame))										-- pass-on the args table, nothing else; getArgs so we also get parent frame
end

------------------------------------------------------------------------------

Returns the ietf language tag associated with the language name. Spelling of language name must be correct
according to the spelling in the source tables. When a standard language name has a parenthetical disambiguator,
that disambiguator must be omitted (they are not present in the data name-to-tag tables).

Module entry point from another module


local function _tag_from_name (args)											-- entry point from another module
	local msg;

	if args[1] and '' ~= args[1] then
		local data = mw.loadData ('Module:Lang/tag from name');					-- get the reversed data tables TODO: change when going live
		local lang = args[1]:lower;											-- allow any-case for the language name (speeling must till be correct)
		local tag = data.rev_override_table[lang] or data.rev_lang_table[lang] or data.rev_lang_dep_table[lang];	-- get the code; look first in the override then in the standard

		if tag then
			return tag, true;													-- language name found so return tag and done; second return used by is_lang_name
		else
			msg = 'language: ' .. args[1] .. ' not found'						-- language name not found, error message
		end
	else
		msg = 'missing language name'											-- language name not provided, error message
	end

	local template = '';
	if args.template and '' ~= args.template then
		template = table.concat ({': '});				-- make template name (if provided by the template)
	end
	return table.concat ({''});
end

--------------------------------------------------------------------------------

Module entry point from an 


local function tag_from_name (frame)											-- entry point from an 
	local result, _ = _tag_from_name (getArgs(frame))							-- pass-on the args table, nothing else; getArgs so we also get parent frame; supress second return used by is_lang_name
	return result;		
end

----------------------------------------------------------------------------------

Module entry point from an 


local function is_lang_name (frame)
	local _, result = _tag_from_name (getArgs(frame))							-- pass-on the args table, nothing else; getArgs so we also get parent frame; supress second return used by tag_from_name
	return result and true or nil;		
end

--------------------------------------------------------------------------------------------

Module entry point from another module


local function _transl (args)
	local title_table = lang_data.translit_title_table;							-- table of transliteration standards and the language codes and scripts that apply to those standards
	local language_name;														-- language name that matches language code; used for tool tip
	local translit;																-- transliterated text to display
	local script;																-- IANA script
	local msg;																	-- for when called functions return an error message

	if is_set (args[3]) then													-- [3] set when 
		args.text = args[3];													-- get the transliterated text
		args.translit_std = args[2] and args[2]:lower;						-- get the standard; lower case for table indexing

		if not title_table[args.translit_std] then
			return make_error_msg (table.concat , args, 'Transl');
		end
	else
		if is_set (args[2]) then												-- [2] set when 
			args.text = args[2];												-- get the transliterated text
		else
			if args[1] and args[1]:match ('^%a%a%a?%a?$') then					-- args[2] missing; is args[1] a code or its it the transliterated text?
				return make_error_msg ;				-- args[1] is a code so we're missing text
			else
				args.text = args[1];											-- args[1] is not a code so we're missing that; assign args.text for error message
				return make_error_msg ;
			end
		end
	end

	if is_set (args[1]) then													-- IANA language code used for html lang= attribute; or ISO 15924 script code
		if args[1]:match ('^%a%a%a?%a?$') then									-- args[1] has correct form?
			args.code = args[1]:lower;										-- use the language/script code; only ; lower case because table indexes are lower case
		else
			return make_error_msg (table.concat , args, 'Transl');	-- invalid language / script code
		end
	else
		return make_error_msg ;	-- missing language / script code so quit
	end

	args.italic, msg = validate_italic (args);
	if msg then
		return make_error_msg ;
	end
	
	if 'italic' == args.italic then												-- 'italic' when |italic=yes; because that is same as absent or not set and |italic=default
		args.italic = nil;														-- set to nil; 
	end

	if override_table[args.code] then											-- is code a language code defined in the override table?
		language_name = override_table[args.code][1];
	elseif lang_table[args.code] then											-- is code a language code defined in the standard language code tables?
		language_name = lang_table[args.code][1];
	elseif lang_dep_table[args.code] then										-- is code a language code defined in the deprecated language code tables?
		language_name = lang_dep_table[args.code][1];
	elseif script_table[args.code] then											-- if here, code is not a language code; is it a script code?
		language_name = script_table[args.code][1];
		script = args.code;														-- code was an ISO 15924 script so use that instead
		args.code = '';															-- unset because not a language code
	else
		return make_error_msg (table.concat , args, 'Transl');	-- invalid language / script code
	end
																				-- here only when all parameters passed to make_translit are valid
	return make_translit ;
end

----------------------------------------------------------------------------------------------

Module entry point from an 


local function transl (frame)
	return _transl (getArgs(frame));
end

------------------------------------------------------------------------

Returns category name associated with IETF language tag if valid; error message else

All code combinations supported by and the templates are supported by this function.

Module entry point from another module


local function _category_from_tag (args)
	local subtags = {};															-- IETF subtags script, region, variant, and private
	local raw_code = args[1];													-- save a copy of the input IETF subtag
	local link = 'yes' == args['link'];											-- make a boolean
	local label = args.label;
	local code;																	-- the language code
	local msg;																	-- gets an error message if IETF language tag is malformed or invalid
	local category_name = '';
	
	code, subtags.script, subtags.region, subtags.variant, subtags.private, msg = get_ietf_parts (raw_code);
	if msg then
		local template = (args['template'] and table.concat ({': '})) or '';	-- make template name (if provided by the template)
		return table.concat ({''});
	end

	raw_code = format_ietf_tag ;	-- format to recommended subtag styles; private omitted because private
	category_name = language_name_get ;							-- get language name; try ietf tag first, then code w/o variant then code w/ variant
	category_name = make_category :gsub ;

	return category_name;
end

------------------------------------------------------------------------

Module entry point from an 


local function category_from_tag (frame)										-- entry point from an 
	return _category_from_tag (getArgs (frame));								-- pass-on the args table, nothing else; getArgs so we also get parent frame
end

----------------------------------------------------------------------

return {
	category_from_tag = category_from_tag,
	lang = lang,																-- entry point for 
	lang_xx_inherit = lang_xx_inherit,											-- entry points for 
	lang_xx_italic = lang_xx_italic,
	is_ietf_tag = is_ietf_tag,
	is_ietf_tag_frame = is_ietf_tag_frame,
	is_lang_name = is_lang_name,
	tag_from_name = tag_from_name,												-- returns ietf tag associated with language name
	name_from_tag = name_from_tag,												-- used for template documentation; possible use in ISO 639 name from code templates
	transl = transl,															-- entry point for 
	
	_category_from_tag = _category_from_tag,									-- entry points when this module is required into other modules
	_lang = _lang,
	_lang_xx_inherit = _lang_xx_inherit,
	_lang_xx_italic = _lang_xx_italic,
	_is_ietf_tag = _is_ietf_tag,
	get_ietf_parts = get_ietf_parts,
	_tag_from_name = _tag_from_name, 
	_name_from_tag = _name_from_tag,
	_transl = _transl,