Jump to content

Module:Portal-inline/sandbox

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Hike395 (talk | contribs) at 13:24, 11 February 2022 (fix). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
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

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 1
	args.maxPortals = args.maxPortals 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

	args.size = args.size == "tiny" and "16x16px" or "32x28px"
	
	local displayName = ""
	if not (args['text'] == "" or args['text'] == nil) then
		displayName = args['text']
	elseif args.short then
		displayName = portals[1]
	else
		displayName = portals[1] .. " portal"
	end
	-- display portal-inline content
	return string.format('[[File:%s|class=noviewer|%s]] [[Portal:%s|%s]]%s', 
		getImageName{ portals[1] }, args['size'], portals[1], displayName, trackingCat or '')
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)
	local container = mw.html.create('ul')
	container:css('float','right')
	container:css('font-size','85%')
	container:css('font-weight','bold')
	container:css('font-style','italic')
	container:css('padding','0.1em')
	local first = true
	for _, portal in ipairs(portals) do
		local li = container:tag('li')
		li:css('margin-top','0.3em')
		li:wikitext(p._main({portal},args))
	end
	return tostring(container)
end

return p