မဝ်ဂျူ:unsubst
This module protects templates and modules from being wrongly substituted.
Usage
[ပလေဝ်ဒါန်]First, put the following at the beginning of the entry_point function in the module:
if mw.isSubsting() then
return require('Module:unsubst').unsubst_template("entry_point")
end
Where "entry_point" should be replaced with the function name. Next, edit the template:
{{safesubst:<noinclude/>#invoke:...|entry_point|...}}
The Lua error in မဝ်ဂျူ:template_parser/templates at line 80: Parameter 2 is not used by this template.. is optional. The template will substitute into its transcluded form.
To protect the module itself instead of the template, add this at the start of the function instead:
if mw.isSubsting() then
return require('Module:unsubst').unsubst_module("entry_point")
end
Directly in templates
[ပလေဝ်ဒါန်]You can wrap the template code in
{{safesubst:#invoke:unsubst|me|=
...
}}
It will work just like unsubst_template above, apart from also generating a transclusion to Module:unsubst. Additionally, it protects templates from being copied and pasted to non-template pages.
-- Cannot unsubst if not currently being substed.
if not mw.isSubsting() then
local frame_title = mw.getCurrentFrame():getTitle()
-- If another module is calling this one, throw an error.
if frame_title ~= "Module:unsubst" then
error(("[[%s]] should not require [[Module:unsubst]] unless mw.isSubsting() returns true"):format(frame_title))
end
-- Otherwise, enable {{#invoke:unsubst|me|...}}.
local export = {}
local process_params = require("Module:parameters").process
function export.me(frame)
return process_params(frame.args, {
[""] = true
})[""]
end
return export
end
local m_pages = require("Module:pages")
local m_template_parser = require("Module:template parser")
local build_template = m_template_parser.buildTemplate
local concat = table.concat
local is_internal_title = m_pages.is_internal_title
local new_title = mw.title.new
local function get_title(frame)
local title = new_title(frame:getTitle())
return is_internal_title(title) and title or
-- This shouldn't happen.
error("frame returned an invalid title")
end
local function serialize(title, args)
return "{{" .. concat(build_template(title, args), "|") .. "}}"
end
local mt = {}
if m_pages.is_transcluded() then
local get_template_invocation_name = m_template_parser.getTemplateInvocationName
function mt:__index(entry_point)
return function(frame)
frame = frame:getParent()
return serialize(get_template_invocation_name(get_title(frame)), frame.args)
end
end
else
function mt:__index(entry_point)
return function(frame)
local title = get_title(frame)
return title.namespace == 828 and
(serialize(("safesubst:<noinclude/>#invoke:%s|%s"):format(title.text, entry_point), frame.args)) or
-- This shouldn't happen.
error(("[[Module:unsubst]] cannot work if the current frame is not in the %s namespace, because the #invoke magic word requires it"):format(mw.site.namespaces[828].canonicalName))
end
end
end
return setmetatable({}, mt)