Module:Message box: Difference between revisions
rewrite with a "box" object to make the code a little less spaghetti-like
m>Mr. Stradivarius (pass nocat, page, and demospace parameters to Module:Category handler, and add a choice for which arguments to allow as blank) |
m>Mr. Stradivarius (rewrite with a "box" object to make the code a little less spaghetti-like) |
||
| Line 14: | Line 14: | ||
local tconcat = table.concat | local tconcat = table.concat | ||
local | local box = {} | ||
local function getTitleObject(page) | local function getTitleObject(page) | ||
| Line 34: | Line 34: | ||
return false | return false | ||
end | end | ||
end | end | ||
| Line 87: | Line 65: | ||
end | end | ||
function box.getNamespaceId(ns) | |||
if not ns then return end | if not ns then return end | ||
if type(ns) == 'string' then | if type(ns) == 'string' then | ||
| Line 101: | Line 79: | ||
end | end | ||
function box.getMboxType(nsid) | |||
-- Gets the mbox type from a namespace number. | -- Gets the mbox type from a namespace number. | ||
if nsid == 0 then | if nsid == 0 then | ||
| Line 119: | Line 97: | ||
end | end | ||
function | function box:addCat(ns, cat, sort) | ||
if type( | if type(cat) ~= 'string' then return end | ||
local nsVals = {'main', 'template', 'all'} | |||
local tname | |||
for i, val in ipairs(nsVals) do | |||
if ns == val then | |||
tname = ns .. 'Cats' | |||
end | |||
end | end | ||
if not tname then | |||
for i, val in ipairs(nsVals) do | |||
nsVals[i] = format('"%s"', val) | |||
end | |||
error('invalid ns parameter passed to box:addCat; valid values are ' .. mw.text.listToText(nsVals, nil, ' or ')) | |||
end | |||
self[tname] = self[tname] or {} | |||
if type(sort) == 'string' then | |||
tinsert(self[tname], format('[[Category:%s|%s]]', cat, sort)) | |||
else | |||
tinsert(self[tname], format('[[Category:%s]]', cat)) | |||
end | |||
end | |||
function box:addClass(class) | |||
if type(class) ~= 'string' then return end | |||
self.classes = self.classes or {} | |||
tinsert(self.classes, class) | |||
end | |||
function box:setTitle(args) | |||
-- Get the title object and the namespace. | -- Get the title object and the namespace. | ||
local pageTitle = getTitleObject(args.page ~= '' and args.page) | local pageTitle = getTitleObject(args.page ~= '' and args.page) | ||
self.title = pageTitle or mw.title.getCurrentTitle() | |||
local demospace = getNamespaceId(args.demospace ~= '' and args.demospace) | local demospace = box.getNamespaceId(args.demospace ~= '' and args.demospace) | ||
self.nsid = demospace or self.title.namespace | |||
end | |||