https://en.seminaverbi.bibleget.io/w/index.php?title=Module:Template_invocation&feed=atom&action=historyModule:Template invocation - Revision history2024-03-28T23:15:42ZRevision history for this page on the wikiMediaWiki 1.41.0https://en.seminaverbi.bibleget.io/w/index.php?title=Module:Template_invocation&diff=22157&oldid=prevJohnrdorazio: 1 revision imported2020-09-02T20:33:17Z<p>1 revision imported</p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 20:33, September 2, 2020</td>
</tr><tr><td colspan="4" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr>
<!-- diff cache key seminaverbi_en:diff:1.41:old-22156:rev-22157 -->
</table>Johnrdoraziohttps://en.seminaverbi.bibleget.io/w/index.php?title=Module:Template_invocation&diff=22156&oldid=preven>Johnuniq: fixes requested at Module talk:Template invocation: keys of parameters may be numbers or strings so need a custom sort; that break was wrong as more could be following2020-05-20T04:03:53Z<p>fixes requested at <a href="/w/index.php?title=Module_talk:Template_invocation&action=edit&redlink=1" class="new" title="Module talk:Template invocation (page does not exist)">Module talk:Template invocation</a>: keys of parameters may be numbers or strings so need a custom sort; that break was wrong as more could be following</p>
<p><b>New page</b></p><div>-- This module provides functions for making MediaWiki template invocations.<br />
<br />
local checkType = require('libraryUtil').checkType<br />
<br />
local p = {}<br />
<br />
------------------------------------------------------------------------<br />
-- Name: p.name<br />
-- Purpose: Find a template invocation name from a page name or a<br />
-- mw.title object.<br />
-- Description: This function detects whether a string or a mw.title<br />
-- object has been passed in, and uses that to find a<br />
-- template name as it is used in template invocations.<br />
-- Parameters: title - full page name or mw.title object for the<br />
-- template (string or mw.title object)<br />
-- Returns: String<br />
------------------------------------------------------------------------<br />
<br />
function p.name(title)<br />
if type(title) == 'string' then<br />
title = mw.title.new(title)<br />
if not title then<br />
error("invalid title in parameter #1 of function 'name'", 2)<br />
end<br />
elseif type(title) ~= 'table' or type(title.getContent) ~= 'function' then<br />
error("parameter #1 of function 'name' must be a string or a mw.title object", 2)<br />
end<br />
if title.namespace == 10 then<br />
return title.text<br />
elseif title.namespace == 0 then<br />
return ':' .. title.prefixedText<br />
else<br />
return title.prefixedText<br />
end<br />
end<br />
<br />
------------------------------------------------------------------------<br />
-- Name: p.invocation<br />
-- Purpose: Construct a MediaWiki template invocation.<br />
-- Description: This function makes a template invocation from the<br />
-- name and the arguments given. Note that it isn't<br />
-- perfect: we have no way of knowing what whitespace was<br />
-- in the original invocation, the named parameters will be<br />
-- alphabetically sorted, and any parameters with duplicate keys<br />
-- will be removed.<br />
-- Parameters: name - the template name, formatted as it will appear<br />
-- in the invocation. (string)<br />
-- args - a table of template arguments. (table)<br />
-- format - formatting options. (string, optional)<br />
-- Set to "nowiki" to escape, curly braces, pipes and<br />
-- equals signs with their HTML entities. The default<br />
-- is unescaped.<br />
-- Returns: String<br />
------------------------------------------------------------------------<br />
<br />
function p.invocation(name, args, format)<br />
checkType('invocation', 1, name, 'string')<br />
checkType('invocation', 2, args, 'table')<br />
checkType('invocation', 3, format, 'string', true)<br />
<br />
-- Validate the args table and make a copy to work from. We need to<br />
-- make a copy of the table rather than just using the original, as<br />
-- some of the values may be erased when building the invocation.<br />
local invArgs = {}<br />
for k, v in pairs(args) do<br />
local typek = type(k)<br />
local typev = type(v)<br />
if typek ~= 'string' and typek ~= 'number'<br />
or typev ~= 'string' and typev ~= 'number'<br />
then<br />
error("invalid arguments table in parameter #2 of " ..<br />
"'invocation' (keys and values must be strings or numbers)", 2)<br />
end<br />
invArgs[k] = v<br />
end<br />
<br />
-- Get the separators to use.<br />
local seps = {<br />
openb = '{{',<br />
closeb = '}}',<br />
pipe = '|',<br />
equals = '='<br />
}<br />
if format == 'nowiki' then<br />
for k, v in pairs(seps) do<br />
seps[k] = mw.text.nowiki(v)<br />
end<br />
end<br />
<br />
-- Build the invocation body with numbered args first, then named.<br />
local ret = {}<br />
ret[#ret + 1] = seps.openb<br />
ret[#ret + 1] = name<br />
for k, v in ipairs(invArgs) do<br />
if type(v) == 'string' and v:find('=', 1, true) then<br />
-- Likely something like 1=foo=bar which needs to be displayed as a named arg.<br />
else<br />
ret[#ret + 1] = seps.pipe<br />
ret[#ret + 1] = v<br />
invArgs[k] = nil -- Erase the key so that we don't add the value twice<br />
end<br />
end<br />
local keys = {} -- sort parameter list; better than arbitrary order<br />
for k, _ in pairs(invArgs) do<br />
keys[#keys + 1] = k<br />
end<br />
table.sort(keys, function (a, b)<br />
-- Sort with keys of type number first, then string.<br />
if type(a) == type(b) then<br />
return a < b<br />
elseif type(a) == 'number' then<br />
return true<br />
end<br />
end)<br />
for _, v in ipairs(keys) do -- Add named args based on sorted parameter list<br />
ret[#ret + 1] = seps.pipe<br />
ret[#ret + 1] = tostring(v)<br />
ret[#ret + 1] = seps.equals<br />
ret[#ret + 1] = invArgs[v]<br />
end<br />
ret[#ret + 1] = seps.closeb<br />
<br />
return table.concat(ret)<br />
end<br />
<br />
return p</div>en>Johnuniq