Module:Transclude: Difference between revisions
From Neodyland Wiki
More actions
Create Module:Transclude for use on `/i18n` subpages that delegate to a `/layout` subpage |
m 7 revisions imported |
||
| (6 intermediate revisions by 3 users not shown) | |||
| Line 6: | Line 6: | ||
-- @module transclude | -- @module transclude | ||
-- @author [[User:ExE Boss]] | -- @author [[User:ExE Boss]] | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
require(' | require('strict') | ||
local checkType = require('libraryUtil').checkType; | local checkType = require('libraryUtil').checkType; | ||
local p = {} | |||
local INVALID_TITLES = { | local INVALID_TITLES = { | ||
["."] = true, | ["."] = true, | ||
[".."] = true, | [".."] = true, | ||
}; | }; | ||
local function _ne(value) | |||
return value and value ~= '' | |||
end | |||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Line 24: | Line 28: | ||
-- @param {table} outArgs | -- @param {table} outArgs | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function mergeArgs(inArgs, outArgs) | local function mergeArgs(inArgs, outArgs, opts) | ||
checkType("mergeArgs", 1, inArgs, "table"); | checkType("mergeArgs", 1, inArgs, "table"); | ||
checkType("mergeArgs", 2, outArgs, "table"); | checkType("mergeArgs", 2, outArgs, "table"); | ||
checkType("mergeArgs", 3, opts, "table", true); | |||
opts = opts or {} | |||
local ignoredParams = opts.ignoredParams; | |||
local ignoredPrefix = opts.ignoredPrefix; | |||
for name, value in pairs(inArgs) do | for name, value in pairs(inArgs) do | ||
outArgs[name] = value; | if (type(name) == "string") then | ||
if not ( | |||
(ignoredParams and ignoredParams[name] == true) | |||
or (ignoredPrefix and | |||
ignoredPrefix == name:sub(1, ignoredPrefix:len())) | |||
) then | |||
outArgs[name] = value; | |||
end | |||
else | |||
outArgs[name] = value; | |||
end | |||
end | end | ||
return outArgs; | return outArgs; | ||
end | end | ||
-- Exported for testing: | |||
p['#mergeArgs'] = mergeArgs; | |||
local mt = {}; | local mt = {}; | ||
| Line 53: | Line 75: | ||
return function (frame) | return function (frame) | ||
checkType('transclude["' .. name .. '"]', 1, frame, "table"); | checkType('transclude["' .. name .. '"]', 1, frame, "table"); | ||
local parentFrame = frame:getParent(); | |||
local ignoredParams, ignoredPrefix; | |||
if _ne(frame.args["#ignoredParams"]) then | |||
ignoredParams = {}; | |||
for m in mw.text.gsplit(frame.args["#ignoredParams"], '|', true) do | |||
ignoredParams[mw.text.trim(m)] = true; | |||
end | |||
end | |||
if _ne(frame.args['#ignoredPrefix']) then | |||
ignoredPrefix = frame.args['#ignoredPrefix']; | |||
end | |||
local args = {}; | local args = {}; | ||
mergeArgs( | if (parentFrame) then | ||
mergeArgs(frame.args, args); | mergeArgs(parentFrame.args, args, { | ||
ignoredParams = ignoredParams, | |||
ignoredPrefix = ignoredPrefix, | |||
}); | |||
end | |||
mergeArgs(frame.args, args, { | |||
ignoredPrefix = "#", | |||
}); | |||
if (not title.exists) then | if (not title.exists) then | ||
| Line 69: | Line 111: | ||
end | end | ||
return setmetatable( | return setmetatable(p, mt); | ||
Latest revision as of 06:05, 9 August 2026
This documentation is transcluded from Module:Transclude/doc.
-- <nowiki>
--------------------------------------------------------------------------------
-- Module that allows transcluding a template with all parent arguments.
-- Useful in `/i18n` template subpages that delegate to a `/layout` subpage.
--
-- @module transclude
-- @author [[User:ExE Boss]]
--------------------------------------------------------------------------------
require('strict')
local checkType = require('libraryUtil').checkType;
local p = {}
local INVALID_TITLES = {
["."] = true,
[".."] = true,
};
local function _ne(value)
return value and value ~= ''
end
--------------------------------------------------------------------------------
-- Helper function that merges argument tables.
--
-- @function mergeArgs
-- @param {table} inArgs
-- @param {table} outArgs
--------------------------------------------------------------------------------
local function mergeArgs(inArgs, outArgs, opts)
checkType("mergeArgs", 1, inArgs, "table");
checkType("mergeArgs", 2, outArgs, "table");
checkType("mergeArgs", 3, opts, "table", true);
opts = opts or {}
local ignoredParams = opts.ignoredParams;
local ignoredPrefix = opts.ignoredPrefix;
for name, value in pairs(inArgs) do
if (type(name) == "string") then
if not (
(ignoredParams and ignoredParams[name] == true)
or (ignoredPrefix and
ignoredPrefix == name:sub(1, ignoredPrefix:len()))
) then
outArgs[name] = value;
end
else
outArgs[name] = value;
end
end
return outArgs;
end
-- Exported for testing:
p['#mergeArgs'] = mergeArgs;
local mt = {};
function mt.__index(_, name)
if (
type(name) ~= "string"
or INVALID_TITLES[name]
or mw.ustring.find(name, "#", nil, true)
) then
return nil;
end
local title = mw.title.new(name, "Template")
if (not title) then
return nil;
end
return function (frame)
checkType('transclude["' .. name .. '"]', 1, frame, "table");
local parentFrame = frame:getParent();
local ignoredParams, ignoredPrefix;
if _ne(frame.args["#ignoredParams"]) then
ignoredParams = {};
for m in mw.text.gsplit(frame.args["#ignoredParams"], '|', true) do
ignoredParams[mw.text.trim(m)] = true;
end
end
if _ne(frame.args['#ignoredPrefix']) then
ignoredPrefix = frame.args['#ignoredPrefix'];
end
local args = {};
if (parentFrame) then
mergeArgs(parentFrame.args, args, {
ignoredParams = ignoredParams,
ignoredPrefix = ignoredPrefix,
});
end
mergeArgs(frame.args, args, {
ignoredPrefix = "#",
});
if (not title.exists) then
return "[[:" .. title.fullText .. "]]";
end
return frame:expandTemplate{
title = title,
args = args,
};
end
end
return setmetatable(p, mt);