模組:Portal bar
外觀
![]() | 此模組被引用於約20,000個頁面。 為了避免造成大規模的影響,所有對此模組的編輯應先於沙盒或測試樣例上測試。 測試後無誤的版本可以一次性地加入此模組中,但是修改前請務必於討論頁發起討論。 模板引用數量會自動更新。 |
![]() | 此模組已評為beta版,可廣泛使用。因其新近完成,請謹慎使用,以確保輸出結果符合預期。 |
![]() | 本模組使用以下模板樣式: |
本模塊實現{{portal bar}}模板,展示一個水平的主題(portal)欄。
用法
{{#invoke:Portal bar|main|''portal 1''|''portal 2 ''|...|border=''no''}}
- 位置參數——待展示主題的名稱
border
——如果|border=
等於no
、n
、false
或0
,該主題欄無邊框。
舉例
{{#invoke:portal bar|main|艺术|科学|文学}}
生成:
{{#invoke:portal bar|main|艺术|科学|文学|border=no}}
生成:
更多例子,參見Template:Portal bar/testcases.
圖片
本模塊透過Module:Portal獲取主題圖片。關於增加、修改、移除圖片,請參見Module:Portal#Image中相關指引。
-- This module implements {{portal bar}}.
require('Module:No globals')
-- 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 portalModule = require('Module:Portal'..sandbox)
local getImageName = portalModule._image
local yesno = require( 'Module:Yesno' )
local p = {}
local function sandboxVersion(s)
return isSandbox and s.."-sand" or s
end
-- Builds the portal bar used by {{portal bar}}.
function p._main( portals, args )
if #portals < 1 then return '' end -- Don't display a blank navbox if no portals were specified.
if args.border == nil then args.border = true end
args.border = yesno(args.border, true)
local nav = mw.html.create( 'div' )
:addClass(sandboxVersion('portal-bar'))
:addClass( 'noprint metadata noviewer' )
:attr( 'role', 'navigation' )
:attr( 'aria-label' , 'Portals' )
:addClass(sandboxVersion(args.border and 'portal-bar-bordered' or 'portal-bar-unbordered'))
local container = nav:tag('ul')
container:addClass(sandboxVersion('portal-bar-content'))
local size = '24x21px'
for _, portal in ipairs( portals ) do
container
:tag( 'li' )
:addClass(sandboxVersion('portal-bar-item'))
:wikitext( string.format('<span class="nowrap">[[File:%s|%s]] </span>[[Portal:%s|%s主題]]',
getImageName{ portal }, size, portal, portal))
end
local styleFile = 'Module:Portal bar'..sandbox..'/styles.css'
return mw.getCurrentFrame():extensionTag{
name = 'templatestyles', args = { src = styleFile }
} .. tostring( nav )
end
-- Processes external arguments and sends them to the other functions.
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 origArgs
if type( frame.getParent ) == 'function' then
origArgs = frame:getParent().args
for k, v in pairs( frame.args ) do
origArgs = frame.args
break
end
else
origArgs = frame
end
-- Process the args to make an array of portal names that can be used with ipairs. We need to use ipairs because we want to list
-- all the portals in the order they were passed to the template, but we also want to be able to deal with positional arguments
-- passed explicitly, for example {{portal|2=Politics}}. The behaviour of ipairs is undefined if nil values are present, so we
-- need to make sure they are all removed.
local portals, args = {}, {}
for k, v in pairs( origArgs ) do
if type( k ) == 'number' and type( v ) == 'string' then -- Make sure we have no non-string portal names.
if mw.ustring.find( v, '%S' ) then -- Remove blank values.
table.insert( portals, k )
end
elseif type( k ) ~= 'number' then -- Separate named arguments from portals.
if type( v ) == 'string' then
v = mw.text.trim( v )
end
args[ k ] = v
end
end
table.sort( portals )
for i, v in ipairs( portals ) do
portals[ i ] = mw.text.trim( origArgs[ v ] ) -- Swap keys with values, trimming whitespace.
end
return p._main( portals, args )
end
return p