Module:unsubst

Hello, you have come here looking for the meaning of the word Module:unsubst. In DICTIOUS you will not only get to know all the dictionary meanings for the word Module:unsubst, but we will also tell you about its etymology, its characteristics and you will know how to say Module:unsubst in singular and plural. Everything you need to know about the word Module:unsubst you have here. The definition of the word Module:unsubst will help you to be more precise and correct when speaking or writing your texts. Knowing the definition ofModule:unsubst, as well as those of other words, enriches your vocabulary and provides you with more and better linguistic resources.

This module protects templates and modules from being wrongly substituted.

Usage

There are two ways to use this module:

Protecting all templates in a module

First, put the following at the top of a module which contains functions designed to be called by {{#invoke:}}:

-- Prevent substitution.
if mw.isSubsting() then
	return require("Module:unsubst")
end

Second, edit each template which calls that module from:

{{#invoke:...|function|...}}

to:

{{safesubst:<noinclude/>#invoke:...|function|...}}

Note: this should only be used with modules that only return functions which are designed to be called by templates (not by other modules). If your module also returns functions (or other data) designed to be called by other modules, then the safest solution is to separate out the template functions into a separate module, usually in the format Module:.../templates (e.g. Module:links/templates). This is because unsubst prevents any functions or data in a module from being accessed during substitution, which will disrupt other modules which need to access your module while they are being substituted.

In templates
Template code can be wrapped as follows:
{{safesubst:<noinclude/>#invoke:unsubst|me|=
...
}}

local m_template_parser = require("Module:template parser")

local build_template = m_template_parser.buildTemplate
local concat = table.concat
local get_template_invocation_name = m_template_parser.getTemplateInvocationName
local insert = table.insert
local is_valid_title = require("Module:pages").is_valid_title
local new_title = mw.title.new
local shallowcopy = require("Module:table").shallowcopy

local is_substing = mw.isSubsting()

local function unsubst_me(frame)
	return frame.args
end

local function get_title(frame)
	local title = new_title(frame:getTitle())
	if not is_valid_title(title) then
		-- Only possible if the frame has been tampered with.
		error("frame returned an invalid title.")
	end
	return title
end

local function serialize(title, args)
	return "{{" .. concat(build_template(title, args), "|") .. "}}"
end

local mt = {}

function mt:__index(entry_point)
	-- Cannot unsubst if not currently being substed.
	if not is_substing then
		local frame_title = mw.getCurrentFrame():getTitle()
		if frame_title ~= "Module:unsubst" then
			error(("] should not call ] unless mw.isSubsting() returns true."):format(frame_title))
		-- Allow {{#invoke:unsubst|me|=...}}.
		elseif entry_point == "me" then
			return unsubst_me
		end
		return
	end
	return function(frame)
		local parent = frame:getParent()
		if parent:getTitle() ~= mw.title.getCurrentTitle().fullText then
			return serialize(get_template_invocation_name(get_title(parent)), parent.args)
		end
		local args = shallowcopy(frame.args)
		insert(args, 1, entry_point)
		local title = get_title(frame)
		-- Only possible if with mw.getCurrentFrame has been tampered with.
		if title.namespace ~= 828 then
			error("] cannot work if the current frame is not in the " .. mw.site.namespaces.canonicalName .. " namespace, because the #invoke magic word requires it.")
		end
		return serialize("safesubst:<noinclude/>#invoke:" .. title.text, args)
	end
end

return setmetatable({}, mt)