Module:Icon

From TechInfoDepot
Jump to navigationJump to search
Documentation icon Module documentation[view] [edit] [history] [purge]

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.

 1 -- This module implements [[Template:Icon]].
 2 
 3 require("Module:No globals")
 4 local yesNo = require("Module:Yesno")
 5 local getArgs = require("Module:Arguments").getArgs
 6 local getPlain = nil
 7 
 8 local p = {}
 9 
10 -- Determine whether we're being called from a sandbox
11 local sandbox = mw.getCurrentFrame():getTitle():find('sandbox', 1, true) and '/sandbox' or ''
12 
13 -- Implements [[Template:Icon]]
14 -- Returns the icon image corresponding to a string (like 'B')
15 function p._main(args, data)
16 	local data_module = 'Module:Icon/data'..sandbox
17 	data = data or mw.loadData(data_module)
18 	local code = args.class or args[1]
19 	local iconData
20 	if code then
21 		code = code:match('^%s*(.-)%s*$'):lower() -- trim whitespace and put in lower case
22 		iconData = data[code]
23 	end
24 	if not iconData then
25 		iconData = data._DEFAULT
26 	end
27 	return string.format(
28 		'[[File:%s%s%s|%s|class=noviewer|alt=%s]]',
29 		iconData.image,
30 		iconData.tooltip and '|' .. iconData.tooltip or '',
31 		iconData.link == false and '|link=' or '',
32 		args.size or '16x16px',
33 		iconData.alt or ''
34 	)
35 end
36 
37 -- Implements [[Template:Icon link]], a superset of [[Template:Icon]]
38 -- Returns an icon, plus a suitably formatted wikilink
39 function p._link(args, data)
40 	args.size = args.size or args.iconsize
41 	local icon = p._main(args, data)
42 	-- If no link given in args[2], default back to [[Template:Icon]]
43 	if not args[2] then
44 		return icon
45 	end
46 	-- Strip wiki markup out of link
47 	getPlain = getPlain or require("Module:Text").Text().getPlain
48 	local link = getPlain(args[2])
49 	local display = args[3] or args[2]
50 	-- italicize display string, if requested
51 	if yesNo(args.i) or yesNo(args.italic) or yesNo(args.italics) then
52 		display = '<i>'..display..'</i>'
53 	end
54 	-- if display is link, just use standard wlink
55 	if link == display then
56 		return icon..'&nbsp;[['..link..']]'
57 	end
58 	return icon..'&nbsp;[['..link..'|'..display..']]'
59 end
60 
61 function p.main(frame)
62 	local args = getArgs(frame,{parentFirst=true})
63 	return p._main(args)
64 end
65 
66 function p.link(frame)
67 	local args = getArgs(frame,{parentFirst=true})
68 	return p._link(args)
69 end
70 
71 return p