Module:$var

From USApedia
Revision as of 22:09, 7 September 2024 by MrT (talk | contribs) (Created page with "-- <nowiki> -------------------------------------------------------------------------------- -- Lua module for rendering the source of template transclusions using -- $<var>var</var>. -- -- @module $var -- @alias p -- @author User:ExE Boss -- @require Module:Arguments -------------------------------------------------------------------------------- require("strict") loca...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Template:Shared Template Warning

Lua module for rendering the source of template transclusions using Template:Mono.

Usage

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

[[Category:Modules{{#translation:}}]]


-- <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)