模块:Portal bar
此模块被引用于约19,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