模組:Subject bar
外觀
![]() | 此模組已評為beta版,可廣泛使用。因其新近完成,請謹慎使用,以確保輸出結果符合預期。 |
![]() | 此模組被引用於許多頁面。 為了避免造成大規模的影響,所有對此模組的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模組中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
![]() | 此模組使用Lua語言: |
這個模塊實現了{{Subject bar}}模板。 請不要從文章或其他wiki頁面中使用此模塊。 您應該改用{{Subject bar}}模板。 要使用另一個Lua模塊中的模塊,請繼續閱讀。
從其他 Lua 模塊使用
[編輯]像這樣加載模塊:
local subjectBar = require('Module:Subject bar')._main
然後你可以像這樣使用subjectBar
函數:
local myBar = subjectBar{
portal = 'Portal 1',
portal2 = 'Portal 2',
-- ...
commons = true,
commons-search = 'Commons search string',
wikt = true,
wikt-search = 'Wiktionary search string'
-- ...
}
有關可能參數的完整列表,請參閱Template:Subject bar/doc。
require('strict')
local getArgs = require('Module:Arguments').getArgs
local yesNo = require('Module:Yesno')
-- Determine whether we're being called from a sandbox
local isSandbox = mw.getCurrentFrame():getTitle():find('sandbox', 1, true)
local sandbox = isSandbox and '/sandbox' or ''
local p = {}
local sisters = {'commons','species','voy','n','wikt','b','q','s','v','iw','iw1','iw2','d'}
local function findNumericArgs(key, args)
local pattern = "^"..key.."_?(%d+)$" -- pattern to match
local values = {}
for k, v in pairs(args) do --- loop through all arguments
local ord = tonumber(mw.ustring.match(k,pattern)) --- if "foo_?%d+", extract number
if ord then
values[ord] = v
end
end
if args[key] ~= nil then
values[1] = args[key]
end
local compressSparseArray = require('Module:TableTools').compressSparseArray
values = compressSparseArray(values) --- squeeze out gaps/nils in values, keep ordering
return values
end
function p._main(args)
local result = ""
local hasPortal = false
for key, _ in pairs(args) do
if mw.ustring.sub(key,1,6) == 'portal' or tonumber(key) then
hasPortal = true
break
end
end
local hasSister = yesNo(args.auto,true) or yesNo(args.author,true) or yesNo(args.cookbook,true)
for _, sister in ipairs(sisters) do
if hasSister then
break
end
if yesNo(args[sister],true) or yesNo(args[sister..'-search'],true) then
hasSister = true
end
end
if hasPortal then
local portalList = findNumericArgs("portal",args)
for _, positional in ipairs(args) do
table.insert(portalList, positional)
end
local portalBar = require('Module:Portal bar'..sandbox)._main
result = result..portalBar(portalList, {tracking=args.tracking, qid=args.qid})
end
if hasSister then
local sisterArgs = {auto=1, bar=1, trackSingle=not hasPortal}
sisterArgs[1] = args.search
for _, k in ipairs({'author','commonscat','cookbook','display','tracking','qid'}) do
sisterArgs[k] = args[k]
end
for _, t in ipairs(sisters) do
sisterArgs[t] = args[t..'-search'] or args[t]
end
local sisterBar = require('Module:Sister project links'..sandbox)._main
result = result..sisterBar(sisterArgs)
end
return result
end
function p.main(frame)
-- If called via #invoke, use the args passed into the invoking template,
-- or the args passed to #invoke if any exist. Otherwise assume args are
-- being passed directly in from the debug console or from another Lua module.
local args = getArgs(frame)
return p._main(args)
end
return p