Module:Icon: Difference between revisions

From escforumwiki
Jump to navigation Jump to search
en>Mr. Stradivarius
suppress links for modules where the "link" field in the data table is false
en>Hike395
implement Template:Icon link, use Module:Arguments for parsing, cleanup sandbox handling
Line 2: Line 2:


require("Module:No globals")
require("Module:No globals")
local yesNo = require("Module:Yesno")
local getArgs = require("Module:Arguments").getArgs
local getPlain = nil


local DATA_MODULE = 'Module:Icon/data'
local p = {}
local p = {}


-- Implements [[Template:Icon]]
-- Returns the icon image corresponding to a string (like 'B')
function p._main(args, data)
function p._main(args, data)
data = data or mw.loadData(DATA_MODULE)
local inSandbox = yesNo(args.sandbox)
local data_module = 'Module:Icon/data'..(inSandbox and '/sandbox' or '')
data = data or mw.loadData(data_module)
local code = args.class or args[1]
local code = args.class or args[1]
local iconData
local iconData
Line 24: Line 30:
args.size or '16x16px'
args.size or '16x16px'
)
)
end
-- Implements [[Template:Icon link]], a superset of [[Template:Icon]]
-- Returns an icon, plus a suitably formatted wikilink
function p._link(args, data)
args.size = args.size or args.iconsize
local icon = p._main(args, data)
-- If no link given in args[2], default back to [[Template:Icon]]
if not args[2] then
return icon
end
-- Strip wiki markup out of link
getPlain = getPlain or require("Module:Text").Text().getPlain
local link = getPlain(args[2])
local display = args[3] or args[2]
-- italicize display string, if requested
if yesNo(args.i) or yesNo(args.italic) or yesNo(args.italics) then
display = '<i>'..display..'</i>'
end
-- if display is link, just use standard wlink
if link == display then
return icon..'&nbsp;[['..link..']]'
end
return icon..'&nbsp;[['..link..'|'..display..']]'
end
end


function p.main(frame)
function p.main(frame)
local args = {}
local args = getArgs(frame,{parentFirst=true})
for k, v in pairs(frame:getParent().args) do
args[k] = v
end
return p._main(args)
return p._main(args)
end
function p.link(frame)
local args = getArgs(frame,{parentFirst=true})
return p._link(args)
end
end


return p
return p

Revision as of 08:18, 8 January 2022

This module displays an icon depending on the code it is given. It implements Template:Icon.

Usage

From wikitext

From wikitext this module should be used via Template:Icon. Please see the template page for documentation.

From Lua

To use this module from another Lua module, first load it:

local mIcon = require('Module:Icon')

Then you can make icons with the _main function.

mIcon._main(args)

The args variable is a table of arguments. This corresponds to the parameters accepted by Template:Icon - please see the template page for parameter documentation.

Data

The icon data is stored at Module:Icon/data. See the instructions there for how to add and remove icons.



-- This module implements [[Template:Icon]].

require("Module:No globals")
local yesNo = require("Module:Yesno")
local getArgs = require("Module:Arguments").getArgs
local getPlain = nil

local p = {}

-- Implements [[Template:Icon]]
-- Returns the icon image corresponding to a string (like 'B')
function p._main(args, data)
	local inSandbox = yesNo(args.sandbox)
	local data_module = 'Module:Icon/data'..(inSandbox and '/sandbox' or '')
	data = data or mw.loadData(data_module)
	local code = args.class or args[1]
	local iconData
	if code then
		code = code:match('^%s*(.-)%s*$'):lower() -- trim whitespace and put in lower case
		iconData = data[code]
	end
	if not iconData then
		iconData = data._DEFAULT
	end
	return string.format(
		'[[File:%s%s%s|%s|class=noviewer]]',
		iconData.image,
		iconData.tooltip and '|' .. iconData.tooltip or '',
		iconData.link == false and '|link=' or '',
		args.size or '16x16px'
	)
end

-- Implements [[Template:Icon link]], a superset of [[Template:Icon]]
-- Returns an icon, plus a suitably formatted wikilink
function p._link(args, data)
	args.size = args.size or args.iconsize
	local icon = p._main(args, data)
	-- If no link given in args[2], default back to [[Template:Icon]]
	if not args[2] then
		return icon
	end
	-- Strip wiki markup out of link
	getPlain = getPlain or require("Module:Text").Text().getPlain
	local link = getPlain(args[2])
	local display = args[3] or args[2]
	-- italicize display string, if requested
	if yesNo(args.i) or yesNo(args.italic) or yesNo(args.italics) then
		display = '<i>'..display..'</i>'
	end
	-- if display is link, just use standard wlink
	if link == display then
		return icon..'&nbsp;[['..link..']]'
	end
	return icon..'&nbsp;[['..link..'|'..display..']]'
end

function p.main(frame)
	local args = getArgs(frame,{parentFirst=true})
	return p._main(args)
end

function p.link(frame)
	local args = getArgs(frame,{parentFirst=true})
	return p._link(args)
end

return p