Module:Lua banner: Difference between revisions
From Workers & Resources: Soviet Republic Official Wiki
workers-resources-fandom>Letter N Created page with "-- <pre> -------------------------------------------------------------------------------- -- This module implements the {{T|Lua}} template. -- -- @module LuaBanner -- @alias p -- @release stable -- @author User:ExE Bossm -- @require Module:Arguments -- @require Module:Config -- @require Module:I18n -- @require Module:List -- @require [[Global Lua Mod..." |
Use local modules instead of the Fandom Dev: modules, which do not exist here and threw a script error on every page using the Lua banner |
||
| (2 intermediate revisions by one other user not shown) | |||
| Line 1: | Line 1: | ||
-- <pre> | -- <pre> | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- This module implements the {{ | -- This module implements the {{Lua}} template. | ||
-- | |||
-- Adapted from the Fandom original, which required the shared Dev: modules | |||
-- (Arguments, Config, I18n, List, Message box, TableTools, Yesno). Those exist | |||
-- only on Fandom, so this version uses the equivalent local modules, reads | |||
-- Module:Lua banner/config directly, and carries its English strings inline. | |||
-- | -- | ||
-- @attribution [[mw:Module:Lua banner|Module:Lua banner]] (MediaWiki) | -- @attribution [[mw:Module:Lua banner|Module:Lua banner]] (MediaWiki) | ||
-- @attribution [[wikipedia:Module:Lua banner|Module:Lua banner]] (Wikipedia) | -- @attribution [[wikipedia:Module:Lua banner|Module:Lua banner]] (Wikipedia) | ||
-- @see [[Template:Lua]] | -- @see [[Template:Lua]] | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
| Line 27: | Line 17: | ||
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg; | local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg; | ||
local getArgs = require(" | local getArgs = require("Module:Arguments").getArgs; | ||
local list = require(" | local list = require("Module:List"); | ||
local messageBox = require(" | local messageBox = require("Module:Message box"); | ||
local tableTools = require(" | local tableTools = require("Module:TableTools"); | ||
local yesno = require(" | local yesno = require("Module:Yesno"); | ||
local cfg = | local cfg = mw.loadData("Module:Lua banner/config"); | ||
local i18n = | |||
-- Target for the "Lua" link in the banner text. | |||
local luaLink = "mw:Lua"; | |||
local i18n = { | |||
["error"] = "Error: no modules specified", | |||
["wishtext"] = "This template should use [[$1|Lua]].", | |||
["header"] = "This template uses [[$1|Lua]]:", | |||
["header-module"] = "This module uses [[$1|Lua]]:", | |||
["header-user"] = "This user page uses [[$1|Lua]]:", | |||
}; | |||
local function msg(key, ...) | |||
return mw.message.newRawMessage(i18n[key], ...):plain(); | |||
end | |||
local function noEmptyString(value) | local function noEmptyString(value) | ||
if (type(value) | if (type(value) == "string" and value == "") then | ||
return | return nil; | ||
end | end | ||
return value; | |||
end | end | ||
local p = {}; | local p = {}; | ||
function p.main(frame, ...) | function p.main(frame, ...) | ||
return p._main(getArgs(frame), ...); | return p._main(getArgs(frame), ...); | ||
| Line 69: | Line 66: | ||
end | end | ||
function p.renderBox(modules, title) | function p.renderBox(modules, title) | ||
checkType('renderBox', 1, modules, 'table'); | checkType('renderBox', 1, modules, 'table'); | ||
| Line 86: | Line 76: | ||
local boxArgs = {}; | local boxArgs = {}; | ||
if (#modules < 1) then | if (#modules < 1) then | ||
if ( | if (cfg["allow_wishes"] == true and title.contentModel ~= "Scribunto") then | ||
boxArgs.text = msg("wishtext", luaLink); | |||
boxArgs.text = | |||
else | else | ||
boxArgs.text = tostring( | boxArgs.text = tostring( | ||
mw.html.create("strong") | mw.html.create("strong") | ||
:addClass("error") | :addClass("error") | ||
:wikitext( | :wikitext(msg("error")) | ||
); | ); | ||
end | end | ||
| Line 110: | Line 96: | ||
local header; | local header; | ||
if title.contentModel == "Scribunto" then | if (title.contentModel == "Scribunto") then | ||
header = "header-module"; | header = "header-module"; | ||
elseif (title.namespace == 2) then | elseif (title.namespace == 2) then | ||
| Line 118: | Line 104: | ||
end | end | ||
boxArgs.text = | boxArgs.text = msg(header, luaLink) .. "\n" .. moduleList; | ||
end | end | ||
boxArgs.type = "notice"; | boxArgs.type = "notice"; | ||
boxArgs.small = true; | boxArgs.small = true; | ||
-- No icon: File:Lua logo.svg has never been uploaded to this wiki. | |||
boxArgs.image = "none"; | |||
boxArgs.class = 'msgbox'; | boxArgs.class = 'msgbox'; | ||
| Line 133: | Line 116: | ||
end | end | ||
function p.renderTrackingCategories(args, modules, title) | function p.renderTrackingCategories(args, modules, title) | ||
checkType('renderTrackingCategories', 1, args, 'table'); | checkType('renderTrackingCategories', 1, args, 'table'); | ||
| Line 155: | Line 130: | ||
end | end | ||
local allowWishes = cfg | local allowWishes = cfg["allow_wishes"]; | ||
local cats = {}; | local cats = {}; | ||
-- Error category | -- Error category | ||
local errorCategory = noEmptyString(cfg | local errorCategory = noEmptyString(cfg["error_category"]); | ||
if ( | if ( | ||
#modules < 1 | #modules < 1 | ||
| Line 172: | Line 147: | ||
-- Lua templates category | -- Lua templates category | ||
local subpageBlacklist = cfg | local subpageBlacklist = cfg["subpage_blacklist"]; | ||
if (title.namespace == 10 and not subpageBlacklist[title.subpageText]) then | if (title.namespace == 10 and not subpageBlacklist[title.subpageText]) then | ||
if (not category) then | if (not category) then | ||
local moduleCategories = cfg | local moduleCategories = cfg["module_categories"]; | ||
local pagename = modules[1] and mw.title.new(modules[1]); | local pagename = modules[1] and mw.title.new(modules[1]); | ||
category = pagename and moduleCategories[pagename.text]; | category = pagename and moduleCategories[pagename.text]; | ||
| Line 181: | Line 156: | ||
if (not category) then | if (not category) then | ||
category = noEmptyString( | category = noEmptyString( | ||
cfg | cfg[ | ||
(allowWishes and #modules < 1) | (allowWishes and #modules < 1) | ||
and "wish_category" | and "wish_category" | ||
or "default_category" | or "default_category" | ||
] | |||
); | ); | ||
end | end | ||
Latest revision as of 02:12, 8 August 2026
Documentation for this module may be created at Module:Lua banner/doc
-- <pre>
--------------------------------------------------------------------------------
-- This module implements the {{Lua}} template.
--
-- Adapted from the Fandom original, which required the shared Dev: modules
-- (Arguments, Config, I18n, List, Message box, TableTools, Yesno). Those exist
-- only on Fandom, so this version uses the equivalent local modules, reads
-- Module:Lua banner/config directly, and carries its English strings inline.
--
-- @attribution [[mw:Module:Lua banner|Module:Lua banner]] (MediaWiki)
-- @attribution [[wikipedia:Module:Lua banner|Module:Lua banner]] (Wikipedia)
-- @see [[Template:Lua]]
--------------------------------------------------------------------------------
local libraryUtil = require("libraryUtil");
local checkType = libraryUtil.checkType;
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg;
local getArgs = require("Module:Arguments").getArgs;
local list = require("Module:List");
local messageBox = require("Module:Message box");
local tableTools = require("Module:TableTools");
local yesno = require("Module:Yesno");
local cfg = mw.loadData("Module:Lua banner/config");
-- Target for the "Lua" link in the banner text.
local luaLink = "mw:Lua";
local i18n = {
["error"] = "Error: no modules specified",
["wishtext"] = "This template should use [[$1|Lua]].",
["header"] = "This template uses [[$1|Lua]]:",
["header-module"] = "This module uses [[$1|Lua]]:",
["header-user"] = "This user page uses [[$1|Lua]]:",
};
local function msg(key, ...)
return mw.message.newRawMessage(i18n[key], ...):plain();
end
local function noEmptyString(value)
if (type(value) == "string" and value == "") then
return nil;
end
return value;
end
local p = {};
function p.main(frame, ...)
return p._main(getArgs(frame), ...);
end
function p._main(args, title)
checkType('main', 1, args, 'table');
checkType('main', 2, title, 'table', true);
title = title or mw.title.getCurrentTitle();
local modules = tableTools.compressSparseArray(args);
local box = p.renderBox(modules, title);
local trackingCategories = p.renderTrackingCategories(args, modules, title);
return box .. trackingCategories;
end
function p.renderBox(modules, title)
checkType('renderBox', 1, modules, 'table');
checkType('renderBox', 2, title, 'table', true);
title = title or mw.title.getCurrentTitle();
if (title.subpageText == "doc") then
title = title.basePageTitle;
end
local boxArgs = {};
if (#modules < 1) then
if (cfg["allow_wishes"] == true and title.contentModel ~= "Scribunto") then
boxArgs.text = msg("wishtext", luaLink);
else
boxArgs.text = tostring(
mw.html.create("strong")
:addClass("error")
:wikitext(msg("error"))
);
end
else
local moduleLinks = {};
for i, module in ipairs(modules) do
moduleLinks[i] = string.format("[[:Module:%s]]", module);
end
local moduleList = list.makeList("bulleted", moduleLinks);
local header;
if (title.contentModel == "Scribunto") then
header = "header-module";
elseif (title.namespace == 2) then
header = "header-user";
else
header = "header";
end
boxArgs.text = msg(header, luaLink) .. "\n" .. moduleList;
end
boxArgs.type = "notice";
boxArgs.small = true;
-- No icon: File:Lua logo.svg has never been uploaded to this wiki.
boxArgs.image = "none";
boxArgs.class = 'msgbox';
return messageBox.main("mbox", boxArgs);
end
function p.renderTrackingCategories(args, modules, title)
checkType('renderTrackingCategories', 1, args, 'table');
checkType('renderTrackingCategories', 2, modules, 'table');
checkType('renderTrackingCategories', 3, title, 'table', true);
title = title or mw.title.getCurrentTitle();
local category = args.category;
checkTypeForNamedArg('renderTrackingCategories', 'category', category, 'string', true);
if yesno(args.nocat) then
return '';
end
local allowWishes = cfg["allow_wishes"];
local cats = {};
-- Error category
local errorCategory = noEmptyString(cfg["error_category"]);
if (
#modules < 1
and errorCategory
and (
not allowWishes
or (title.subpageText == "doc" and title.basePageTitle or title).contentModel == "Scribunto"
)
) then
cats[#cats + 1] = errorCategory;
end
-- Lua templates category
local subpageBlacklist = cfg["subpage_blacklist"];
if (title.namespace == 10 and not subpageBlacklist[title.subpageText]) then
if (not category) then
local moduleCategories = cfg["module_categories"];
local pagename = modules[1] and mw.title.new(modules[1]);
category = pagename and moduleCategories[pagename.text];
if (not category) then
category = noEmptyString(
cfg[
(allowWishes and #modules < 1)
and "wish_category"
or "default_category"
]
);
end
end
if (category) then
cats[#cats + 1] = category;
end
end
for i, cat in ipairs(cats) do
cats[i] = string.format('[[Category:%s]]', cat);
end
return table.concat(cats);
end
return p;
-- </pre>


