Module:$var

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Lua

CodeDiscussionEditHistoryLinksLink count Subpages:DocumentationTestsResultsSandboxLive code All modules

warning Warning:Do not edit. This page is maintained by an automated tool. All edits should be done at mediawiki.org. (translate this warning)

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

Usage

[edit]

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

Code

-- <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: escaped "|" and "=" as "{{!}}" and "{{=}}"?
	end
	for k, v in pairs(args) do
		if not unnamed[k] then
			content:wikitext("|", k, "=", v) -- XXX: escaped "|" as "{{!}}"?
		end
	end
	content:wikitext("}}")
	return tostring(mw.html.create("code"):wikitext(mw.text.nowiki(tostring(content:allDone()))):allDone())
end

local mt = {}
function mt.__index(t, 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)