सामग्री पर जाएँ

मॉड्यूल:translations/multi-nowiki

विक्षनरी से

इस मॉड्यूल हेतु प्रलेख मॉड्यूल:translations/multi-nowiki/doc पर बनाया जा सकता है

local export = {}

local function make_frame(args, parent_args)
	return {
		args = args,
		getParent = function(self)
			return { args = parent_args }
		end,
	}
end

local template_functions = {}

template_functions["t"] = function(args)
	return require "Module:translations".show(make_frame({}, args))
end

template_functions["t+"] = function(args)
	return require "Module:translations".show(make_frame({ interwiki = "tpos" }, args))
end

function make_module_args(template_args)
	-- We do this unconditionally because a valid module invocation always has
	-- a function name (which looks like argument 1 of a template) so we always
	-- need to shift down arguments.
	local new_args = require "Module:table".shallowcopy(template_args)
	for k, v in pairs(template_args) do
		local old_k = k
		k = tonumber(k)
		if k and require "Module:table".isPositiveInteger(k) then
			new_args[old_k] = nil
			if k > 1 then
				new_args[k - 1] = v
			end
		end
	end
	return new_args
end

function export.expand(frame)
	local args
	if mw.title.getCurrentTitle().nsText == "Module" then
		args = frame.args
	else
		args = frame:getParent().args
	end
	local text = mw.text.unstripNoWiki(args[1])
	text = mw.text.trim(text)
	local parse_template
	local function expand(text)
		return text:gsub(
			"%b{}",
			function(maybe_template)
				if maybe_template:sub(2, 2) == "{" and maybe_template:sub(-2, -2) == "}" then
					parse_template = parse_template or require("Module:templateparser").parseTemplate
					local template_name, template_args = parse_template(maybe_template)
					-- Technically substitution syntax is more lax, but this gets
					-- most cases.
					template_name = template_name:gsub("^subst:", "")
					-- Same for module invocation syntax.
					local module_name = template_name:match "^#invoke:(.+)"
					if module_name then
						local function_name = template_args[1]
						if function_name then
							local module_args = make_module_args(template_args)
							return require("Module:" .. module_name)[function_name](make_frame(module_args, {}))
						end
					end
					local new_template_args = {}
					for k, v in pairs(template_args) do
						new_template_args[k] = expand(v)
					end
					if template_functions[template_name] then
						return template_functions[template_name](new_template_args)
					else
						return frame:expandTemplate { title = template_name, args = new_template_args }
					end
				end
			end)
	end
	-- Strip off extra return value.
	return (expand(text))
end

return export