Module:Portal-inline
Appearance
![]() | This module is rated as ready for general use. It has reached a mature form and is thought to be relatively bug-free and ready for use wherever appropriate. It is ready to mention on help pages and other Wikipedia resources as an option for new users to learn. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing. |
![]() | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
![]() | This Lua module is used on approximately 242,000 pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
![]() | This module depends on the following other modules: |
This module implements Template:Portal-inline. This module accepts one unnamed parameter which is the portal to link to and several named parameters:
size
— optional; Specify|size=small
to show a 23x20 image or|size=tiny
for a 17×15 image instead of the usual size.text
— optional; Specify|text=(name)
for a different associated name to appear.short
— optional; Specify|short=anything
to remove portal from the output.redlinks
— optional; Specify|redlinks=yes
to show the portal if it is redlinked.nowrap
— optional; Specify|nowrap=yes
to prevent the entire output from wrapping.
local getArgs = require('Module:Arguments').getArgs
local yesno = require('Module:Yesno')
local portalModule = require('Module:Portal')
local getImageName = portalModule.image
local checkPortals = portalModule._checkPortals
local processPortalArgs = portalModule._processPortalArgs
local p = {}
-- Function to format error message and tracking category
-- Arguments:
-- errMsg: string, or nil/false if no error
-- trackingCat: string for tracking category (or empty string)
local function formatError(errMsg, trackingCat)
local result = trackingCat or ''
if errMsg then
local errTag = mw.html.create('span')
errTag:addClass("error")
errTag:css("font-size",'100%')
errTag:wikitext("Error: "..errMsg)
result = tostring(errTag)..result
end
return result
end
local function image(portal, args)
local size = args.size == "tiny" and "16x16px" or "32x28px"
return string.format('[[File:%s|class=noviewer|%s]]',getImageName{portal}, size)
end
local function link(portal, args)
local displayName = ""
if not (args.text == "" or args.text == nil) then
displayName = args.text
elseif args.short then
displayName = portal
else
displayName = portal .. " portal"
end
return string.format('[[Portal:%s|%s]]',portal,displayName)
end
function p._main(portals, args)
mw.logObject(args)
-- Normalize all arguments
if args.redlinks == 'include' then args.redlinks = true end
for key, default in pairs({tracking=true,redlinks=false,short=false}) do
if args[key] == nil then args[key] = default end
args[key] = yesno(args[key], default)
end
local trackingCat = ''
local errMsg = nil
-- Check for existing categories, drop if not.
-- Possible generate tracking category & error message if needed
args.minPortals = args.minPortals or args.mobile and 0 or 1
args.maxPortals = args.maxPortals or args.mobile and -1 or 1
portals, trackingCat, errMsg = checkPortals(portals,args)
-- use more specific tracking cat for inline portal
trackingCat = mw.ustring.gsub(trackingCat,"Portal templates","Portal-inline template")
-- either too many/few portals, or no portals left after filtering, then return
if errMsg or #portals == 0 then
return formatError(errMsg, trackingCat)
end
if not args.mobile then
return image(portals[1],args)..' '..link(portals[1],args)..(trackingCat or '')
end
-- if mobile, generate table
local div = mw.html.create('div')
div:css('float','right')
div:css('padding','0.1em')
div:css('max-width','175px')
local container = div:tag('table')
container:attr('role','navigation')
container:attr('aria-label','portals')
container:css('font-size','85%')
container:css('font-weight','bold')
container:css('font-style','italic')
container:css('line-height','110%')
local first = true
for _, portal in ipairs(portals) do
local row = container:tag('tr')
local img = row:tag('td')
img:css('padding','0.2em')
img:wikitext(image(portal, args))
local lnk = row:tag('td')
lnk:css('padding','0.2em')
lnk:wikitext(link(portal, args))
end
return tostring(div)
end
function p.main(frame)
local origArgs = getArgs(frame)
local portals, args = processPortalArgs(origArgs)
return p._main(portals, args)
end
function p.mobile(frame)
local origArgs = getArgs(frame)
local portals, args = processPortalArgs(origArgs)
args.mobile = true
return p._main(portals, args)
end
return p