Module:Portal bar/sandbox: Difference between revisions

Content deleted Content added
maybe better without hlist?
better implementation of nbsp
 
(107 intermediate revisions by 11 users not shown)
Line 1:
-- This module implements {{portal bar}}.
 
require('strict')
local p = {}
 
-- determine whether we're being called from a sandbox
local htmlBuilder = require( 'Module:HtmlBuilder' )
local isSandbox = mw.getCurrentFrame():getTitle():find('sandbox', 1, true)
local buildNavbox = require( 'Module:Navbox' )._navbox
local getImageNamesandbox = require(isSandbox and 'Module:Portal/sandbox' ).imageor ''
 
local portalModule = require('Module:Portal'..sandbox)
local getImageName = portalModule._image
local checkPortals = portalModule._checkPortals
local processPortalArgs = portalModule._processPortalArgs
local yesno = require( 'Module:Yesno' )
local getArgs = require('Module:Arguments').getArgs
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.
-- check for sensible args
local noBorder = yesno( args.border ) == false -- Returns true if args.border is "no", "n", "false", 0 or false.
args = type(args) == "table" and args or {}
local list = htmlBuilder.create( 'ul' )
for i, portal in ipairs( portals ) do
-- Normalize arguments
list
for key, default in pairs({border=true,redlinks=false,tracking=true}) do
.tag( 'li' )
if args[key] == nil then args[key] = default end
.css( 'display', 'inline' )
args[key] = yesno(args[key], default)
.css( 'white-space', 'nowrap' )
end
.tag( 'span' )
 
.css( 'margin', 'auto 0.5em' )
local nav = mw.html.create( 'div' )
.wikitext( mw.ustring.format( '[[File:%s|24x21px]]', getImageName{ portal } ) )
:addClass(sandboxVersion('portal-bar'))
.done()
:addClass( 'noprint metadata noviewer' )
.tag( 'span' )
:attr( 'role', 'navigation' )
.css( 'font-weight', 'bold' )
:attr( 'aria-label' , 'Portals' )
.wikitext( mw.ustring.format( '[[Portal:%s|%s portal]]', portal, portal ) )
:addClass(sandboxVersion(args.border and 'portal-bar-bordered' or 'portal-bar-unbordered'))
end
if noBorder then
local trackingCat = ''
local root = htmlBuilder.create( 'div' )
-- Allow any number of portals
root
args.minPortals = 0
.css( 'width', '100%' )
args.maxPortals = -1
.css( 'text-align', 'center' )
-- Check to see whether there are redlinks, filter out unless args.redlink is true
.css( 'padding', '1px' )
portals, trackingCat = checkPortals(portals, args)
.css( 'font-size', '88%' )
. nav:wikitext( tostring( list ) trackingCat)
if #portals == 0 then
return tostring( root )
return trackingCat
else
end
return buildNavbox{
 
name = 'Portal bar',
local related = yesno(args.related)
bodyclass = 'noprint',
if related then
list1 = tostring( list )
nav:addClass(sandboxVersion('portal-bar-related'))
}
else
end
local header = nav:tag('span')
header:addClass(sandboxVersion('portal-bar-header'))
header:wikitext('[[Wikipedia:Contents/Portals|Portal]]')
if #portals > 1 then
header:wikitext('s')
end
header:wikitext(':')
end
local container = nav:tag('ul')
container:addClass(sandboxVersion('portal-bar-content'))
if related then
container:addClass(sandboxVersion('portal-bar-content-related'))
end
local size = related and '27x25px' or '21x19px'
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,true), 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 )
local origArgs = getArgs(frame)
-- If called via #invoke, use the args passed into the invoking
local portals, args = processPortalArgs(origArgs)
-- template, or the args passed to #invoke if any exist. Otherwise
return p._main( portals, args )
-- assume args are being passed directly in from the debug console
-- or from another Lua module.
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
-- 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