Module:$var

From Bonkipedia
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

{{#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 [[Module:TableTools]]
--------------------------------------------------------------------------------

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

local p, mt = {}, {};

local function echoSource(name, args)
	local numArgs, sArgs = {}, {};
	local numArgsCount = 0;

	for k, v in pairs(args) do
		if (tableTools.isPositiveInteger(k)) then
			if (k > numArgsCount) then numArgsCount = k end
			numArgs[k] = v;
		else
			sArgs[k] = v;
		end
	end

	local iArgs = {};
	for i, v in ipairs(numArgs) do
		iArgs[i] = v;
	end

	if #iArgs ~= numArgsCount then
		local namedArgs2 = {};
		for i = #iArgs + 1, numArgsCount do
			local v = numArgs[i];
			if v ~= nil then
				namedArgs2[i] = v;
			end
		end
		for k, v in pairs(sArgs) do
			namedArgs2[k] = v;
		end
		sArgs = namedArgs2;
	end

	local content = mw.html.create()
		:wikitext("{{", name);

	for _, v in ipairs(numArgs) do
		content:wikitext("|", v);
	end
	for k, v in pairs(sArgs) do
		content:wikitext("|", k, "=", v);
	end
	content:wikitext("}}");

	return tostring(
		mw.html.create("code")
			:wikitext(mw.text.nowiki(tostring(content)))
			:allDone()
	);
end

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(p, mt);