https://wiki.pbbg.com/index.php?title=Module:Template_invocation&feed=atom&action=historyModule:Template invocation - Revision history2024-03-29T15:26:21ZRevision history for this page on the wikiMediaWiki 1.35.1https://wiki.pbbg.com/index.php?title=Module:Template_invocation&diff=378&oldid=prevFoohonPie: 1 revision imported2019-10-10T01:08:29Z<p>1 revision imported</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<tr class="diff-title" lang="en">
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="1" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 01:08, 10 October 2019</td>
</tr><tr><td colspan="2" class="diff-notice" lang="en"><div class="mw-diff-empty">(No difference)</div>
</td></tr></table>FoohonPiehttps://wiki.pbbg.com/index.php?title=Module:Template_invocation&diff=377&oldid=preven>KnightLago: Protected "Module:Template invocation": Highly visible template ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))2018-05-10T01:59:14Z<p>Protected "<a href="/wiki/Module:Template_invocation" title="Module:Template invocation">Module:Template invocation</a>": <a href="/index.php?title=WP:High-risk_templates&action=edit&redlink=1" class="new" title="WP:High-risk templates (page does not exist)">Highly visible template</a> ([Edit=Require template editor access] (indefinite) [Move=Require template editor access] (indefinite))</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, we need to do it as a named arg<br />
break<br />
end<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 />
local invArgs_list = {} -- sort a parameter list; preferable to randomly sorted output<br />
for k, v in pairs(invArgs) do<br />
invArgs_list[#invArgs_list + 1] = k<br />
end<br />
table.sort(invArgs_list)<br />
for i, v in ipairs(invArgs_list) do -- Add named args based on sorted parameter list<br />
ret[#ret + 1] = seps.pipe<br />
ret[#ret + 1] = 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>KnightLago