Module:Lua banner: Difference between revisions

From Workers & Resources: Soviet Republic Official Wiki
(Fix `wish` parameter)
 
m (1 revision imported)
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
-- This module implements the {{lua}} template.
-- <pre>
--------------------------------------------------------------------------------
-- This module implements the {{T|Lua}} template.
--
-- @module LuaBanner
-- @alias p
-- @release stable
-- @author [[User:ExE Boss]]m
-- @require [[Global Lua Modules/Arguments|Module:Arguments]]
-- @require [[Global Lua Modules/Config|Module:Config]]
-- @require [[Global Lua Modules/I18n|Module:I18n]]
-- @require [[Global Lua Modules/List|Module:List]]
-- @require [[Global Lua Modules/Message box|Module:Message box]]
-- @require [[Global Lua Modules/TableTools|Module:TableTools]]
-- @require [[Global Lua Modules/Yesno|Module:Yesno]]
-- @require [[Module:Lua banner/config]]
-- @require [[Module:Lua banner/i18n]]
-- @attribution [[mw:Module:Lua banner|Module:Lua banner]] (MediaWiki)
-- @attribution [[wikipedia:Module:Lua banner|Module:Lua banner]] (Wikipedia)
-- @see [[mw:Module:Lua banner|Original module on MediaWiki]]
-- @see [[wikipedia:Module:Lua banner|Similar module on Wikipedia]]
-- @see [[Template:Lua]]
--------------------------------------------------------------------------------


local yesno = require('Module:Yesno')
local libraryUtil = require("libraryUtil");
local mList = require('Module:List')
local checkType = libraryUtil.checkType;
local mTableTools = require('Module:TableTools')
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg;
local mMessageBox = require('Module:Message box')
local TNT = require('Module:TNT')


local p = {}
local getArgs = require("Dev:Arguments").getArgs;
local list = require("Dev:List");
local messageBox = require("Dev:Message box");
local tableTools = require("Dev:TableTools");
local yesno = require("Dev:Yesno");


local function format(msg)
local cfg = require("Dev:Config").loadConfig("Lua banner");
return TNT.format('I18n/Lua banner', msg)
local i18n = require("Dev:I18n").loadMessages("Lua banner");
 
local function noEmptyString(value)
if (type(value) ~= "string" or string ~= "") then
return value;
end
end
 
local p = {};
 
--[[
-- @function p.main
-- @param {table|Frame} args
-- @param[opt] args.nocat
-- @param[opt] {string} args.category
-- @param[opt] {Title} title
-- @return {string}
--]]
function p.main(frame, ...)
return p._main(getArgs(frame), ...);
end
end


local function getConfig()
function p._main(args, title)
return mw.loadData('Module:Lua banner/config')
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
end


function p.main(frame)
--[[
local origArgs = frame:getParent().args
-- @function p.renderBox
local args = {}
-- @private
for k, v in pairs(origArgs) do
-- @param {table} modules
v = v:match('^%s*(.-)%s*$')
-- @param[opt] {Title} title
if v ~= '' then
-- @return {string}
args[k] = v
--]]
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
end
return p._main(args)
end


function p._main(args, cfg)
local boxArgs = {};
local modules = mTableTools.compressSparseArray(args)
local luaLink = i18n:msg("lua_link");
local box = p.renderBox(modules, cfg, args)
local trackingCategories = p.renderTrackingCategories(args, modules, nil, cfg)
return box .. trackingCategories
end


function p.renderBox(modules, cfg, args)
if (#modules < 1) then
local boxArgs = {}
if (
if #modules < 1 then
cfg:getValue("allow_wishes") == true
cfg = cfg or getConfig()
and title.contentModel ~= "Scribunto"
if cfg['allow_wishes'] or yesno(args and args.wish) then
) then
boxArgs.text = format('wishtext')
boxArgs.text = i18n:msg("wishtext");
else
else
boxArgs.text = string.format('<strong class="error">%s</strong>', format('error_emptylist'))
boxArgs.text = tostring(
mw.html.create("strong")
:addClass("error")
:wikitext(i18n:msg("error"))
);
end
end
else
else
local moduleLinks = {}
local moduleLinks = {};
for i, module in ipairs(modules) do
for i, module in ipairs(modules) do
moduleLinks[i] = string.format('[[:%s]]', module)
moduleLinks[i] = string.format("[[:Module:%s]]", module);
end
end
local moduleList = mList.makeList('bulleted', moduleLinks)
 
boxArgs.text = format('header') .. '\n' .. moduleList
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 = i18n:msg(header, luaLink) .. "\n" .. moduleList;
end
end
boxArgs.type = 'notice'
 
boxArgs.small = true
boxArgs.type = "notice";
boxArgs.small = true;
boxArgs.image = string.format(
boxArgs.image = string.format(
'[[File:Lua-logo-nolabel.svg|30px|alt=%s|link=%s]]',
"[[File:Lua_logo.svg|30px|alt=%s|link=%s]]",
format('logo_alt'),
i18n:msg("logo_alt"),
format('logo_link')
luaLink
)
);
return mMessageBox.main('mbox', boxArgs)
boxArgs.class = 'msgbox';
 
return messageBox.main("mbox", boxArgs);
end
end


function p.renderTrackingCategories(args, modules, titleObj, cfg)
--[[
-- @function p.renderTrackingCategories
-- @private
-- @param {table} args
-- @param {table} modules
-- @param[opt] {Title} title
-- @return {string}
--]]
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
if yesno(args.nocat) then
return ''
return '';
end
end
 
cfg = cfg or getConfig()
local allowWishes = cfg:getValue("allow_wishes");
local cats = {};
local cats = {}
 
-- Error category
-- Error category
if #modules < 1 and not (cfg['allow_wishes'] or yesno(args.wish)) and cfg['error_category'] then
local errorCategory = noEmptyString(cfg:getValue("error_category"));
cats[#cats + 1] = 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
end
 
-- Lua templates category
-- Lua templates category
titleObj = titleObj or mw.title.getCurrentTitle()
local subpageBlacklist = cfg:getValue("subpage_blacklist");
if titleObj.namespace == 10  
if (title.namespace == 10 and not subpageBlacklist[title.subpageText]) then
and not cfg['subpage_blacklist'][titleObj.subpageText]
if (not category) then
then
local moduleCategories = cfg:getValue("module_categories");
local category = args.category
local pagename = modules[1] and mw.title.new(modules[1]);
if not category then
category = pagename and moduleCategories[pagename.text];
local pagename = modules[1] and mw.title.new(modules[1])
 
category = pagename and cfg['module_categories'][pagename.text]
if (not category) then
if not category then
category = noEmptyString(
if (cfg['allow_wishes'] or yesno(args.wish)) and #modules < 1 then
cfg:getValue(
category = cfg['wish_category']
(allowWishes and #modules < 1)
else
and "wish_category"
category = cfg['default_category']
or "default_category"
end
)
);
end
end
end
end
if category then
 
cats[#cats + 1] = category
if (category) then
cats[#cats + 1] = category;
end
end
end
end
 
for i, cat in ipairs(cats) do
for i, cat in ipairs(cats) do
cats[i] = string.format('[[Category:%s]]', cat)
cats[i] = string.format('[[Category:%s]]', cat);
end
end
return table.concat(cats)
 
return table.concat(cats);
end
end


return p
return p;
-- </pre>

Latest revision as of 18:31, 14 June 2024

Documentation for this module may be created at Module:Lua banner/doc

-- <pre>
--------------------------------------------------------------------------------
-- This module implements the {{T|Lua}} template.
--
-- @module LuaBanner
-- @alias p
-- @release stable
-- @author [[User:ExE Boss]]m
-- @require [[Global Lua Modules/Arguments|Module:Arguments]]
-- @require [[Global Lua Modules/Config|Module:Config]]
-- @require [[Global Lua Modules/I18n|Module:I18n]]
-- @require [[Global Lua Modules/List|Module:List]]
-- @require [[Global Lua Modules/Message box|Module:Message box]]
-- @require [[Global Lua Modules/TableTools|Module:TableTools]]
-- @require [[Global Lua Modules/Yesno|Module:Yesno]]
-- @require [[Module:Lua banner/config]]
-- @require [[Module:Lua banner/i18n]]
-- @attribution [[mw:Module:Lua banner|Module:Lua banner]] (MediaWiki)
-- @attribution [[wikipedia:Module:Lua banner|Module:Lua banner]] (Wikipedia)
-- @see [[mw:Module:Lua banner|Original module on MediaWiki]]
-- @see [[wikipedia:Module:Lua banner|Similar module on Wikipedia]]
-- @see [[Template:Lua]]
--------------------------------------------------------------------------------

local libraryUtil = require("libraryUtil");
local checkType = libraryUtil.checkType;
local checkTypeForNamedArg = libraryUtil.checkTypeForNamedArg;

local getArgs = require("Dev:Arguments").getArgs;
local list = require("Dev:List");
local messageBox = require("Dev:Message box");
local tableTools = require("Dev:TableTools");
local yesno = require("Dev:Yesno");

local cfg = require("Dev:Config").loadConfig("Lua banner");
local i18n = require("Dev:I18n").loadMessages("Lua banner");

local function noEmptyString(value)
	if (type(value) ~= "string" or string ~= "") then
		return value;
	end
end

local p = {};

--[[
-- @function p.main
-- @param {table|Frame} args
-- @param[opt] args.nocat
-- @param[opt] {string} args.category
-- @param[opt] {Title} title
-- @return {string}
--]]
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
-- @private
-- @param {table} modules
-- @param[opt] {Title} title
-- @return {string}
--]]
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 = {};
	local luaLink = i18n:msg("lua_link");

	if (#modules < 1) then
		if (
			cfg:getValue("allow_wishes") == true
			and title.contentModel ~= "Scribunto"
		) then
			boxArgs.text = i18n:msg("wishtext");
		else
			boxArgs.text = tostring(
				mw.html.create("strong")
					:addClass("error")
					:wikitext(i18n: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 = i18n:msg(header, luaLink) .. "\n" .. moduleList;
	end

	boxArgs.type = "notice";
	boxArgs.small = true;
	boxArgs.image = string.format(
		"[[File:Lua_logo.svg|30px|alt=%s|link=%s]]",
		i18n:msg("logo_alt"),
		luaLink
	);
	boxArgs.class = 'msgbox';

	return messageBox.main("mbox", boxArgs);
end

--[[
-- @function p.renderTrackingCategories
-- @private
-- @param {table} args
-- @param {table} modules
-- @param[opt] {Title} title
-- @return {string}
--]]
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:getValue("allow_wishes");
	local cats = {};

	-- Error category
	local errorCategory = noEmptyString(cfg:getValue("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:getValue("subpage_blacklist");
	if (title.namespace == 10 and not subpageBlacklist[title.subpageText]) then
		if (not category) then
			local moduleCategories = cfg:getValue("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:getValue(
						(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>