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:15, 23 December 2021 (add "Error: " to message). 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/sandbox')
local getImageName = portalModule.image
local checkTracking = portalModule._checkTracking
local checkExists = portalModule._checkExists
local processPortalArgs = portalModule._processPortalArgs

local p = {}

local function formatError(errMsg, trackingCat)
	local result = trackingCat
	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
	-- Don't track bad namespaces or page names
	args.tracking = args.tracking and checkTracking()
	args.nominimum = false
	-- Check for existing categories, drop if not. 
	local trackingCat = ''
	local errMsg = nil
	-- If more than one portal, we can't use inline
	if #portals > 1 then
		errMsg = "Template:portal-inline accepts only one portal argument"
		trackingCat = trackingCat..'[[Category:Portal-inline template with more than one portal parameter]]'
		return formatError(errMsg, trackingCat)
	end
	portals, trackingCat, errMsg = checkExists(portals,args)
	-- use both generic tracking category + specific one for inline portal
	local specificCat = mw.ustring.gsub(trackingCat,"Portal templates","Portal-inline template")
	trackingCat = trackingCat..(specificCat or '')
	if #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
	local root = formatError(errMsg, trackingCat)
	root = string.format('[[File:%s|class=noviewer|%s]] [[Portal:%s|%s]]', 
		getImageName{ portals[1] }, args['size'], portals[1], displayName)..root
	return tostring(root)
end

function p.main(frame)
	origArgs = getArgs(frame)
	portals, args = processPortalArgs(origArgs)
	return p._main(portals, args)
end

return p