Module:Native name/sandbox: Difference between revisions

Content deleted Content added
Create sandbox version of Module:Native name
 
some simplifications
 
(13 intermediate revisions by 3 users not shown)
Line 3:
local getArgs = require ('Module:Arguments').getArgs;
local lang_module = require ('Module:Lang');
local yes_no = require('Module:Yesno')
 
local defined_values = {
Line 67 ⟶ 68:
args_t.fontsize - deprecated alias of |parensize=
args_t.nolink - any value inhibits wikilinking of language name
args_t.suppress_empty_list_error - when set to 'yes', suppresses an 'empty' error message; mostly for use within another template
 
this function calls these functions in Module:lang:
Line 84 ⟶ 87:
local template = (args_t.template and args_t.template) or 'native name'; -- for error messaging; use 'native name list' when called from native_name_list(), etc
 
if not (args_t[1] or args_t[2]) and yes_no (args_t.suppress_empty_list_error) then
return ''; -- if empty list error is suppressed, return empty string
ifelseif not args_t[21] then
return error_msg ((args_t.index and messages_t.tag_required_idx) or messages_t.tag_required, template, args_t.index)
elseif not args_t[2] then
end
if not args_t[2] then
return error_msg ((args_t.index and messages_t.name_required_idx) or messages_t.name_required, template, args_t.index)
end
Line 114 ⟶ 118:
if args_t.nolink then
table.insert (out_t, table.concat ({'(', lang_module._name_from_tag ({args_t[1], ['template']=template}), ')'}));
ifelseif lang_module._is_ietf_tag (args_t[1]) then
table.insert (out_t, table.concat ({'(', lang_module._name_from_tag ({args_t[1], ['link'] ='yes', ['template']=template}), ')'}));
else
table.insert (out_t, '(language?)'); -- TODO: any reason to keep this?
if lang_module._is_ietf_tag (args_t[1]) then
table.insert (out_t, table.concat ({'(', lang_module._name_from_tag ({args_t[1], ['link'] ='yes', ['template']=template}), ')'}));
else
table.insert (out_t, '(language?)'); -- TODO: any reason to keep this?
end
end
 
Line 159 ⟶ 161:
also supports:
args_t.postfixn - wikitext to be appended to list item n (references other appropriate text)
args_t.suppress_empty_list_error - when set to 'yes', suppresses an 'empty list' error message; mostly for use within another template
 
]]
 
Line 167 ⟶ 170:
end
 
local max_enumunsorted_enumerators_t = 0; {} -- accumulateunsorted the valuek/v totable of tagn and namen enumerators where k is the enumerator withand thev is greatestalways valuetrue
 
for param, _ in pairs (args_t) do -- loop through all parameters
local nenumerator = tonumbermw.ustring.match (param:match, "^tag('%d+$')$"); -- extractis thethis requireda |tagn= parameter? extract enumerator if present
local n if =enumerator 1; then -- initializeif there is an enumerator
max_enum = ((n and (n > max_enum)) and n) or max_enum; -- bump <max_enum> if <n> is greater
unsorted_enumerators_t[tonumber(enumerator)] = true -- add enumerator to the table
else
local name_match = mw.ustring.match (param, "^name(%d+)$") -- is this a |tagn= parameter? extract enumerator if present
n if =name_match n + 1; then -- bumpif there is thean enumerator
unsorted_enumerators_t[tonumber (name_match)] = true -- add enumerator to the table
end
end
end
 
local enumerators_t = {} -- will hold a sorted sequence of enumerators
for n, _ in pairs (unsorted_enumerators_t) do -- loop through the k/v table of enumerators
table.insert (enumerators_t, n) -- add the enumerator to the sequence
end
table.sort (enumerators_t) -- and ascending sort
 
local list_t = {}; -- list of formatted native names goes here
local n = 1; -- initialize an enumerator
 
for _, n in ipairs (enumerators_t) do -- loop through the sorted enumerators
while n <= max_enum do
table.insert (list_t, table.concat ({
'<li>', -- open the list item
_native_name ({ -- go render the native name
args_t['tag'..n],
Line 192 ⟶ 206:
['template'] = 'native name list', -- for error messaging
['index'] = n, -- for error messaging
}),
args_t['postfix'..n] or '',
'</li>' -- close the list item
}));
n = n + 1; -- bump the enumerator
end
 
if 0 <== #list_t then
return (yes_no (args_t.suppress_empty_list_error) and '') or -- return empty string when error suppressed
if 1 < #list_t then
return error_msg (messages_t.empty_list, 'native name list'); -- otherwise error
table.insert (list_t, 1, '<div class="plainlist"><ul>'); -- open the div; open the unordered list
if elseif 1 <== #list_t then
table.insert (list_t, '</ul></div>'); -- close the unordered list; close the div
else return list_t[1]; -- herereturn whenthe onlyvery oneshort list item; no need for list markup; TODO: add error message for this case?
local out = table.concat (list_t):gsub ('<li>', ''):gsub ('</li>', '');
return out; -- to avoid replacement count contaminating the output
end
else
return require ('Module:List').unbulleted (list_t); -- use unbulleted list from module
return error_msg (messages_t.empty_list, 'native name list');
end
return table.concat (list_t); -- make a big string and done
end
 
Line 261 ⟶ 268:
return table.concat ({value, error_msg (messages_t.malformed_param, 'native name checker')}, ' '); -- no {{lang}} or {{native_name}} template
end
if 1 < count then
ifand not (value:find ('<div class="plainlist *" *>') or not value:find ('</div>$') then -- must be wrapped in 'plainlist' div
and value:find ('</div>$'))
then
return table.concat ({value, error_msg (messages_t.list_markup, 'native name checker')}, ' ');
end
end