Open main menu

Changes

Module:Lang

1,814 bytes added, 18:09, 29 December 2019
synch from sandbox; proto-language handling; external template error messaging tweaks;
if text:match ('^%*') then
table.insert (html, '*'); -- move proto language text prefix outside of italic markup if any; use numeric entity because plane plain splat confuses MediaWiki
text = text:gsub ('^%*', ''); -- remove the splat from the text
end
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 (text, language_name, proto_param) if language_namefalse == proto_param then -- when forced by |proto=no return text:find gsub ('^Proto%-**', '') and not ; -- return textwithout 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 and text does not already have leading splator forced by |proto=yes return table.concat text:gsub ({'^%**', text}'*'); -- 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
return text, tag;
end
 
 
--[[--------------------------< V A L I D A T E _ P R O T O >--------------------------------------------------
 
validates value assigned to |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
end
msg = validate_text ('lang'template, args); -- ensure that |text= is set
if is_set (msg) then -- msg is an already-formatted error message
return msg;
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 ({'invalid &#124;proto=: ', args.proto}), 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 ({{{1}}} in {{lang}})
args.text = invert_italics (args.text)
end
args.text = proto_prefix (args.text, language_name, args.proto); -- prefix proto-language text with a splat
table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size, language_name));
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 ('conflicting: {{{1}}} and &#124;text=', args, 'lang-xx'template);
else
args.text = args[1] or args.text; -- prefer args.text
end
msg = validate_text ('lang-xx'template, args); -- ensure that |text= is set, does not contain italic markup and is protected from improper bolding
if is_set (msg) then
return msg;
if args[2] and args.translit then
return make_error_msg ('conflicting: {{{2}}} and &#124;translit=', args, 'lang-xx'template);
else
args.translit = args[2] or args.translit -- prefer args.translit
if args[3] and (args.translation or args.lit) then
return make_error_msg ('conflicting: {{{3}}} and &#124;lit= or &#124;translation=', args, 'lang-xx'template);
elseif args.translation and args.lit then
return make_error_msg ('conflicting: &#124;lit= and &#124;translation=', args, 'lang-xx'template);
else
args.translation = args[3] or args.translation or args.lit; -- prefer args.translation
if args.links and args.link then
return make_error_msg ('conflicting: &#124;links= and &#124;link=', args, 'lang-xx'template);
else
args.link = args.link or args.links; -- prefer args.link
if msg then -- if an error detected then there is an error message
return make_error_msg (msg, args, 'lang-xx'template);
end
args.italic, msg = validate_italic (args);
if msg then
return make_error_msg (msg, args, 'lang-xx'template);
end
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 ({'invalid &#124;proto=: ', args.proto}), args, template);
end
end
args.text = proto_prefix (args.text, language_name, args.proto); -- prefix proto-language text with a splat
table.insert (out, make_text_html (args.code, args.text, tag, args.rtl, args.italic, args.size))
table.insert (out, translit);
else
return make_error_msg (table.concat ({'invalid translit-std: \'', args['translit-std'] or '[missing]'}), args, 'lang-xx'template);
end
end