Module:Toolbar: Difference between revisions
From the Croc Wiki, the Croc encyclopedia
Jump to navigationJump to search
Content added Content deleted
m (Protected Module:Toolbar: High-risk Lua module ([Edit=Block all non-admin users] (indefinite) [Move=Block all non-admin users] (indefinite))) |
(simplifying - the original was probably good practice for me, but this version is better code in the circumstances) |
||
Line 1: | Line 1: | ||
local p = {} |
local p = {} |
||
local args = {} |
local args = {} |
||
-- Create concatenator object. |
|||
local Concatenator = {result = ''} |
|||
function Concatenator:new() |
|||
local o = {} |
|||
setmetatable(o, self) |
|||
self.__index = self |
|||
return o |
|||
end |
|||
function Concatenator:add(s) |
|||
if type(s) ~= 'string' then |
|||
error('Attempt to concatenate non-string value', 2) |
|||
end |
|||
self.result = self.result .. s |
|||
end |
|||
-- Get the keys of the numerical arguments that are present. |
-- Get the keys of the numerical arguments that are present. |
||
Line 38: | Line 21: | ||
-- Generate the toolbar items. |
-- Generate the toolbar items. |
||
local |
local ret = '' |
||
for i, v in ipairs(nums) do |
for i, v in ipairs(nums) do |
||
ret = ret .. args[v] |
|||
if nums[i + 1] then |
if nums[i + 1] then |
||
ret = ret .. sep |
|||
end |
end |
||
end |
end |
||
return |
return ret |
||
end |
end |
||
Line 51: | Line 34: | ||
local class = args.class or '' |
local class = args.class or '' |
||
local style = (args.style and ('style="' .. args.style .. '"')) or '' |
local style = (args.style and ('style="' .. args.style .. '"')) or '' |
||
local t = Concatenator:new() |
|||
-- Generate opening span tag. |
|||
t:add('<span class="plainlinks ') |
|||
t:add(class) |
|||
t:add('" ') |
|||
t:add(style) |
|||
t:add('>') |
|||
-- Generate toolbar items. |
|||
t:add('(') |
|||
⚫ | |||
t:add(')') |
|||
local ret = '<span class="plainlinks ' .. class .. '" ' .. style .. '>' |
|||
-- Generate closing span tag. |
|||
⚫ | |||
.. '</span>' |
|||
return |
return ret |
||
end |
end |
||
Revision as of 15:47, June 11, 2013
This Lua module is used in system messages. Changes to it can cause immediate changes to the Wikipedia user interface. To avoid major disruption, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Please discuss changes on the talk page before implementing them. |
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This module implements {{toolbar}}. Please see the template page for documentation.
See also
- {{toolbar}}, {{toolbar/sandbox}}, /testcases
local p = {}
local args = {}
-- Get the keys of the numerical arguments that are present.
local function getArgNums()
local nums = {}
for k, v in pairs(args) do
local num = tostring(k):match('^([1-9]%d*)$')
if num then table.insert(nums, tonumber(num)) end
end
table.sort(nums)
return nums
end
local function makeToolbarItems()
-- Get numerical argument keys.
local nums = getArgNums()
-- Get the separator text.
local sep = (args.separator or 'pipe') .. '-separator'
sep = mw.message.new(sep):plain()
-- Generate the toolbar items.
local ret = ''
for i, v in ipairs(nums) do
ret = ret .. args[v]
if nums[i + 1] then
ret = ret .. sep
end
end
return ret
end
local function makeToolbar()
local class = args.class or ''
local style = (args.style and ('style="' .. args.style .. '"')) or ''
local ret = '<span class="plainlinks ' .. class .. '" ' .. style .. '>'
.. '(' .. makeToolbarItems() .. ')'
.. '</span>'
return ret
end
function p.main(frame)
-- If called via #invoke, use the args passed into the invoking template.
-- Otherwise, for testing purposes, assume args are being passed directly in.
local origArgs
if frame == mw.getCurrentFrame() then
origArgs = frame:getParent().args
else
origArgs = frame
end
-- Strip whitespace and remove nil values
for k, v in pairs(origArgs) do
v = mw.text.trim(v)
if v ~= '' then
args[k] = v
end
end
return makeToolbar()
end
return p