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 getImageName = require( 'Module:Portal' ).image
local p = {}
local isUserPage;
local function checkPortalExists(portal)
return not (mw.title.makeTitle(100, portal).id == 0)
end
local function checkIfUserPage()
local thisPage = mw.title.getCurrentTitle()
if (thisPage.namespace == 2) or (thisPage.namespace == 3) then
isUserPage = true
return true
end
isUserPage = false
return false
end
function p._main(portals, args)
local root = ""
mw.logObject(args)
-- ignore extra portals listed
-- If no portals have been specified, display an error and add the page to a tracking category.
checkIfUserPage()
if not portals[1] then
root = '<span style="font-size:100%;" class="error">error: missing portal name.</span></strong>"'
if not (isUserPage) then
root = root .. '[[Category:Portal templates without a parameter]][[Category:Portal-inline template without a parameter]]'
end
return tostring(root)
end
if portals[2] or portals[3] then
root = '<span style="font-size:100%;" class="error">error: Template:portal-inline accepts only one portal as a parameter</span></strong> "'
if not (isUserPage) then
root = root .. '[[Category:Portal-inline template with more than one portal parameter]]'
end
end
if not pcall(checkPortalExists, portals[1]) or not checkPortalExists(portals[1]) then
-- Getting here means a redlinked portal has been found
if not ((args.redlinks == 'yes') or (args.redlinks == 'y') or (args.redlinks == 'true') or (args.redlinks == 'include')) then
-- just return if redlinks is not "yes" or similar
if not (isUserPage) then
if portals[2] or portals[3] then
root = root .. '[[Category:Portal templates with redlinked portals]][[Category:Portal-inline template with redlinked portals]]'
else
root = root .. '[[Category:Portal templates with all redlinked portals]][[Category:Portal-inline template with redlinked portals]]'
end
end
return tostring(root)
end
if not (isUserPage) then
root = '[[Category:Portal templates with all redlinked portals]][[Category:Portal-inline template with all redlinked portals]]'
end
end
if args['size'] == "tiny" then
args['size'] = "16x16px"
else
args['size'] = "32x28px"
end
local displayName = ""
if not (args['text'] == "" or args['text'] == nil) then
displayName = args['text']
elseif not (args.short == "" or args.short == nil) then
displayName = portals[1]
else
displayName = portals[1] .. " portal"
end
-- display portal-inline content
root = root .. string.format('[[File:%s|class=noviewer|%s]] [[Portal:%s|%s]]', getImageName{ portals[1] }, args['size'], portals[1], displayName)
return tostring(root)
end
-- copied from [[Module:Portal]]
local function processPortalArgs(args)
-- This function processes a table of arguments and returns two tables: an array of portal names for processing by ipairs, and a table of
-- the named arguments that specify style options, etc. 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-inline|Politics}}. The behaviour of ipairs is undefined if nil values are present, so we need to make sure they are all removed.
args = type(args) == 'table' and args or {}
local portals = {}
local namedArgs = {}
for k, v in pairs(args) do
if type(k) == 'number' and type(v) == 'string' then -- Make sure we have no non-string portal names.
table.insert(portals, k)
elseif type(k) ~= 'number' then
namedArgs[k] = v
end
end
table.sort(portals)
for i, v in ipairs(portals) do
portals[i] = args[v]
end
return portals, namedArgs
end
local function makeWrapper(funcName)
-- Processes external arguments and sends them to the other functions.
return function (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
-- Trim whitespace and remove blank arguments.
local args = {}
for k, v in pairs(origArgs) do
if type(v) == 'string' then
v = mw.text.trim(v)
end
if v ~= '' then
args[k] = v
end
end
return p[funcName](processPortalArgs(args)) -- passes two tables to func: an array of portal names, and a table of named arguments.
end
end
for _, funcName in ipairs{'main'} do
p[funcName] = makeWrapper('_' .. funcName)
end
return p