「モジュール:Toolbar」の版間の差分

提供: ひつじ小屋別館2代目
移動先: 案内検索
(1版)
(1版)
 
(他の1人の利用者による、間の1版が非表示)
1行目: 1行目:
 
local p = {}
 
local p = {}
local args = {}
 
  
-- Get the keys of the numerical arguments that are present.
+
local function getArgNums(args)
local function getArgNums()
+
-- Get the keys of the numerical arguments that are present.
    local nums = {}
+
local nums = {}
    for k, v in pairs(args) do
+
local tinsert = table.insert
        if type(k) == 'number' then
+
for k, v in pairs(args) do
            table.insert(nums, k)
+
if type(k) == 'number' then
        end
+
tinsert(nums, k)
    end
+
end
    table.sort(nums)
+
end
    return nums
+
table.sort(nums)
 +
return nums
 
end
 
end
  
local function makeToolbarItems()
+
local function makeToolbarItems(args)
    -- Get numerical argument keys.
+
local nums = getArgNums(args)
    local nums = getArgNums()
+
local sep = (args.separator or 'pipe') .. '-separator'
    -- Get the separator text.
+
sep = mw.message.new(sep):plain()
    local sep = (args.separator or 'pipe') .. '-separator'
+
local ret = {}
    sep = mw.message.new(sep):plain()
+
local tinsert = table.insert
   
+
for i, v in ipairs(nums) do
    -- Generate the toolbar items.
+
tinsert(ret, args[v])
    local ret = ''
+
end
    for i, v in ipairs(nums) do
+
if #ret > 0 then
        ret = ret .. args[v]
+
return table.concat(ret, sep)
        if nums[i + 1] then
+
end
            ret = ret .. sep
 
        end
 
    end
 
    return ret
 
 
end
 
end
  
local function makeToolbar()
+
function p._main(args)
    local class = (args.class and (' ' .. args.class))  or ''
+
local toolbarItems = makeToolbarItems(args)
    local style = (args.style and (' style="' .. args.style .. '"')) or ''
+
if not toolbarItems then return '' end -- Return the blank string if no arguments were specified, rather than returning empty brackets
   
+
return mw.ustring.format(
    local ret = '<span class="plainlinks' .. class .. '"' .. style .. '>'
+
'<span class="plainlinks%s"%s>(%s)</span>',
        .. '(' .. makeToolbarItems() .. ')'
+
type(args.class) == 'string' and ' ' .. args.class or '',
        .. '</span>'
+
type(args.style) == 'string' and mw.ustring.format(' style="%s"', args.style) or '',
   
+
toolbarItems
    return ret
+
)
 
end
 
end
  
 
function p.main(frame)
 
function p.main(frame)
    -- If called via #invoke, use the args passed into the invoking template.
+
-- If called via #invoke, use the args passed into the invoking template.
    -- Otherwise, for testing purposes, assume args are being passed directly in.
+
-- Otherwise, for testing purposes, assume args are being passed directly in.
    local origArgs
+
local origArgs
    if frame == mw.getCurrentFrame() then
+
if frame == mw.getCurrentFrame() then
        origArgs = frame:getParent().args
+
origArgs = frame:getParent().args
    else
+
for k, v in pairs(frame.args) do
        origArgs = frame
+
origArgs = frame.args
    end
+
break
   
+
end
    -- Strip whitespace and remove nil values
+
else
    for k, v in pairs(origArgs) do
+
origArgs = frame
        v = mw.text.trim(v)
+
end
        if v ~= '' then
+
-- Strip whitespace and remove nil values
            args[k] = v
+
local args = {}
        end
+
for k, v in pairs(origArgs) do
    end
+
if type(v) == 'string' then
   
+
v = mw.text.trim(v)
    return makeToolbar()
+
end
 +
if v ~= '' then
 +
args[k] = v
 +
end
 +
end
 +
return p._main(args)
 
end
 
end
+
 
 
return p
 
return p

2014年1月25日 (土) 22:59時点における最新版

This module implements {{toolbar}}. Please see the template page for documentation.


local p = {}

local function getArgNums(args)
	-- Get the keys of the numerical arguments that are present.
	local nums = {}
	local tinsert = table.insert
	for k, v in pairs(args) do
		if type(k) == 'number' then
			tinsert(nums, k)
		end
	end
	table.sort(nums)
	return nums
end

local function makeToolbarItems(args)
	local nums = getArgNums(args)
	local sep = (args.separator or 'pipe') .. '-separator'
	sep = mw.message.new(sep):plain()
	local ret = {}
	local tinsert = table.insert
	for i, v in ipairs(nums) do
		tinsert(ret, args[v])
	end
	if #ret > 0 then
		return table.concat(ret, sep)
	end
end

function p._main(args)
	local toolbarItems = makeToolbarItems(args)
	if not toolbarItems then return '' end -- Return the blank string if no arguments were specified, rather than returning empty brackets
	return mw.ustring.format(
		'<span class="plainlinks%s"%s>(%s)</span>',
		type(args.class) == 'string' and ' ' .. args.class or '',
		type(args.style) == 'string' and mw.ustring.format(' style="%s"', args.style) or '',
		toolbarItems
	)
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
		for k, v in pairs(frame.args) do
			origArgs = frame.args
			break
		end
	else
		origArgs = frame
	end
	-- Strip whitespace and remove nil values
	local args = {}
	for k, v in pairs(origArgs) do
		if type(v) == 'string' then
			v = mw.text.trim(v)
		end
		if v ~= '' then
			args[k] = v
		end
	end
	return p._main(args)
end

return p