Module documentation
warning Warning:This page is shared between multiple wikis.
All changes to this page will be automatically copied to all wikis listed in the left side bar.
To avoid unnecessary page regeneration and server load, changes should be tested on the page's sandbox.

Lua module for rendering the source of template transclusions using $var .

Usage

{{#invoke:$var|$var_name}}

-- <nowiki>
--------------------------------------------------------------------------------
-- Lua module for rendering the source of template transclusions using
-- [[mw:Special:MyLanguage/Help:Extension:Translate/Page translation administration#Variables|$<var>var</var>]].
--
-- @module $var
-- @alias  p
-- @author [[User:ExE Boss]]
-- @require [[Module:Arguments]]
--------------------------------------------------------------------------------

require("strict")
local getArgs = require("Module:Arguments").getArgs

local function echoSource(name, args)
	local content = mw.html.create()
	local unnamed = {}
	content:wikitext("{{", name)
	for i, v in ipairs(args) do
		unnamed[i] = true -- unnamed arguments can have leading and trailing whitespace
		content:wikitext("|", v) -- XXX: escape "|" as "{{!}}"?
	end
	for k, v in pairs(args) do
		if not unnamed[k] then
			content:wikitext("|", k, "=", v) -- XXX: escape "|" and "=" as "{{!}}" and "{{=}}"?
		end
	end
	content:wikitext("}}")
	return tostring(mw.html.create("code"):wikitext(mw.text.nowiki(tostring(content:allDone()))):allDone())
end

local mt = {}
function mt:__index(name)
	if type(name) == "string" and mw.ustring.find(name, "^%$.") then
		return function(frame)
			local args = getArgs(frame, {
				trim = false,
				removeBlanks = false,
				wrappers = {
					"Template:$1",
					"Template:$2",
					"Template:$3",
					"Template:$4",
					"Template:$5",
					"Template:$6",
					"Template:$7",
					"Template:$8",
					"Template:$9",
				},
			})
			return echoSource(name, args)
		end
	end
end

return setmetatable({}, mt)