<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-GB">
	<id>https://the-democratika.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Module%3ATemplate_invocation</id>
	<title>Module:Template invocation - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://the-democratika.com/wiki/index.php?action=history&amp;feed=atom&amp;title=Module%3ATemplate_invocation"/>
	<link rel="alternate" type="text/html" href="https://the-democratika.com/wiki/index.php?title=Module:Template_invocation&amp;action=history"/>
	<updated>2026-04-04T21:04:09Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.0</generator>
	<entry>
		<id>https://the-democratika.com/wiki/index.php?title=Module:Template_invocation&amp;diff=10086&amp;oldid=prev</id>
		<title>&gt;Pppery: Update name call to fix a few edge cases per request</title>
		<link rel="alternate" type="text/html" href="https://the-democratika.com/wiki/index.php?title=Module:Template_invocation&amp;diff=10086&amp;oldid=prev"/>
		<updated>2024-09-05T20:21:20Z</updated>

		<summary type="html">&lt;p&gt;Update name call to fix a few edge cases per request&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;-- This module provides functions for making MediaWiki template invocations.&lt;br /&gt;
&lt;br /&gt;
local checkType = require(&amp;#039;libraryUtil&amp;#039;).checkType&lt;br /&gt;
&lt;br /&gt;
local p = {}&lt;br /&gt;
&lt;br /&gt;
------------------------------------------------------------------------&lt;br /&gt;
--         Name:  p.name&lt;br /&gt;
--      Purpose:  Find a template invocation name from a page name or a&lt;br /&gt;
--                mw.title object.&lt;br /&gt;
--  Description:  This function detects whether a string or a mw.title&lt;br /&gt;
--                object has been passed in, and uses that to find a&lt;br /&gt;
--                template name as it is used in template invocations.&lt;br /&gt;
--   Parameters:  title - full page name or mw.title object for the&lt;br /&gt;
--                template (string or mw.title object)&lt;br /&gt;
--      Returns:  String&lt;br /&gt;
------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
function p.name(title)&lt;br /&gt;
	if type(title) == &amp;#039;string&amp;#039; then&lt;br /&gt;
		title = mw.title.new(title)&lt;br /&gt;
		if not title or #title.prefixedText == 0 or #title.interwiki &amp;gt; 0 then&lt;br /&gt;
			error(&amp;quot;invalid title in parameter #1 of function &amp;#039;name&amp;#039;&amp;quot;, 2)&lt;br /&gt;
		end&lt;br /&gt;
	elseif type(title) ~= &amp;#039;table&amp;#039; or type(title.getContent) ~= &amp;#039;function&amp;#039; then&lt;br /&gt;
		error(&amp;quot;parameter #1 of function &amp;#039;name&amp;#039; must be a string or a mw.title object&amp;quot;, 2)&lt;br /&gt;
	end&lt;br /&gt;
	if title.namespace == 10 then&lt;br /&gt;
		local text = title.text&lt;br /&gt;
		local check = mw.title.new(text, 10)&lt;br /&gt;
		-- Exclude the prefix, unless we have something like &amp;quot;Template:Category:Foo&amp;quot;, which can&amp;#039;t be abbreviated to &amp;quot;Category:Foo&amp;quot;.&lt;br /&gt;
		return check and mw.title.equals(title, check) and text or title.prefixedText&lt;br /&gt;
	elseif title.namespace == 0 then&lt;br /&gt;
		return &amp;#039;:&amp;#039; .. title.prefixedText&lt;br /&gt;
	else&lt;br /&gt;
		return title.prefixedText&lt;br /&gt;
	end&lt;br /&gt;
end&lt;br /&gt;
------------------------------------------------------------------------&lt;br /&gt;
--         Name:  p.invocation&lt;br /&gt;
--      Purpose:  Construct a MediaWiki template invocation.&lt;br /&gt;
--  Description:  This function makes a template invocation from the&lt;br /&gt;
--                name and the arguments given. Note that it isn&amp;#039;t&lt;br /&gt;
--                perfect: we have no way of knowing what whitespace was&lt;br /&gt;
--                in the original invocation, the named parameters will be&lt;br /&gt;
--                alphabetically sorted, and any parameters with duplicate keys&lt;br /&gt;
--                will be removed.&lt;br /&gt;
--   Parameters:  name - the template name, formatted as it will appear&lt;br /&gt;
--                    in the invocation. (string)&lt;br /&gt;
--                args - a table of template arguments. (table)&lt;br /&gt;
--                format - formatting options. (string, optional)&lt;br /&gt;
--                    Set to &amp;quot;nowiki&amp;quot; to escape, curly braces, pipes and&lt;br /&gt;
--                    equals signs with their HTML entities. The default&lt;br /&gt;
--                    is unescaped.&lt;br /&gt;
--      Returns:  String&lt;br /&gt;
------------------------------------------------------------------------&lt;br /&gt;
&lt;br /&gt;
function p.invocation(name, args, format)&lt;br /&gt;
	checkType(&amp;#039;invocation&amp;#039;, 1, name, &amp;#039;string&amp;#039;)&lt;br /&gt;
	checkType(&amp;#039;invocation&amp;#039;, 2, args, &amp;#039;table&amp;#039;)&lt;br /&gt;
	checkType(&amp;#039;invocation&amp;#039;, 3, format, &amp;#039;string&amp;#039;, true)&lt;br /&gt;
&lt;br /&gt;
	-- Validate the args table and make a copy to work from. We need to&lt;br /&gt;
	-- make a copy of the table rather than just using the original, as&lt;br /&gt;
	-- some of the values may be erased when building the invocation.&lt;br /&gt;
	local invArgs = {}&lt;br /&gt;
	for k, v in pairs(args) do&lt;br /&gt;
		local typek = type(k)&lt;br /&gt;
		local typev = type(v)&lt;br /&gt;
		if typek ~= &amp;#039;string&amp;#039; and typek ~= &amp;#039;number&amp;#039;&lt;br /&gt;
			or typev ~= &amp;#039;string&amp;#039; and typev ~= &amp;#039;number&amp;#039;&lt;br /&gt;
		then&lt;br /&gt;
			error(&amp;quot;invalid arguments table in parameter #2 of &amp;quot; ..&lt;br /&gt;
			&amp;quot;&amp;#039;invocation&amp;#039; (keys and values must be strings or numbers)&amp;quot;, 2)&lt;br /&gt;
		end&lt;br /&gt;
		invArgs[k] = v&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- Get the separators to use.&lt;br /&gt;
	local seps = {&lt;br /&gt;
		openb = &amp;#039;{{&amp;#039;,&lt;br /&gt;
		closeb = &amp;#039;}}&amp;#039;,&lt;br /&gt;
		pipe = &amp;#039;|&amp;#039;,&lt;br /&gt;
		equals = &amp;#039;=&amp;#039;&lt;br /&gt;
	}&lt;br /&gt;
	if format == &amp;#039;nowiki&amp;#039; then&lt;br /&gt;
		for k, v in pairs(seps) do&lt;br /&gt;
			seps[k] = mw.text.nowiki(v)&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
&lt;br /&gt;
	-- Build the invocation body with numbered args first, then named.&lt;br /&gt;
	local ret = {}&lt;br /&gt;
	ret[#ret + 1] = seps.openb&lt;br /&gt;
	ret[#ret + 1] = name&lt;br /&gt;
	for k, v in ipairs(invArgs) do&lt;br /&gt;
		if type(v) == &amp;#039;string&amp;#039; and v:find(&amp;#039;=&amp;#039;, 1, true) then&lt;br /&gt;
			-- Likely something like 1=foo=bar which needs to be displayed as a named arg.&lt;br /&gt;
		else&lt;br /&gt;
			ret[#ret + 1] = seps.pipe&lt;br /&gt;
			ret[#ret + 1] = v&lt;br /&gt;
			invArgs[k] = nil -- Erase the key so that we don&amp;#039;t add the value twice&lt;br /&gt;
		end&lt;br /&gt;
	end&lt;br /&gt;
	local keys = {} -- sort parameter list; better than arbitrary order&lt;br /&gt;
	for k, _ in pairs(invArgs) do&lt;br /&gt;
		keys[#keys + 1] = k&lt;br /&gt;
	end&lt;br /&gt;
	table.sort(keys,&lt;br /&gt;
		function (a, b)&lt;br /&gt;
			-- Sort with keys of type number first, then string.&lt;br /&gt;
			if type(a) == type(b) then&lt;br /&gt;
				return a &amp;lt; b&lt;br /&gt;
			elseif type(a) == &amp;#039;number&amp;#039; then&lt;br /&gt;
				return true&lt;br /&gt;
			end&lt;br /&gt;
		end&lt;br /&gt;
	)&lt;br /&gt;
	&lt;br /&gt;
	local maybeSpace = &amp;#039;&amp;#039; -- First named parameter should not be separated by a space&lt;br /&gt;
	for _, v in ipairs(keys) do -- Add named args based on sorted parameter list&lt;br /&gt;
		ret[#ret + 1] = maybeSpace .. seps.pipe&lt;br /&gt;
		ret[#ret + 1] = tostring(v)&lt;br /&gt;
		ret[#ret + 1] = seps.equals&lt;br /&gt;
		ret[#ret + 1] = invArgs[v]&lt;br /&gt;
		maybeSpace = &amp;#039; &amp;#039;&lt;br /&gt;
	end&lt;br /&gt;
	ret[#ret + 1] = seps.closeb&lt;br /&gt;
&lt;br /&gt;
	return table.concat(ret)&lt;br /&gt;
end&lt;br /&gt;
&lt;br /&gt;
return p&lt;/div&gt;</summary>
		<author><name>&gt;Pppery</name></author>
	</entry>
</feed>