Module:IconData
More actions
Resolves a lookup key against a named dataset and delegates rendering to Module:Icon. It is the data layer of the icon system, sitting between dataset-specific wrapper templates and the icon renderer.
Editors placing dataset-driven icons on a page should use a wrapper template such as Template:Ammo or Template:Condition rather than invoking this module directly.
Datasets
Datasets are stored as data modules at Module:IconData/<DatasetName> and cached via mw.loadData. A key is a string passed to the wrapper template that identifies which entry to look up in the dataset. When an entry is found, its fields and any arguments supplied by the calling template are forwarded to Icon.buildIcon; each entry contains:
icon(required): file name of the icon.invert(optional): whether to invert the icon in light mode.
Example dataset structure using Module:IconData/Currency:
return {
coin = { icon = "Icon_Coin.png",
invert = true },
cred = { icon = "Icon_Cred.png" },
token = { icon = "Icon_Raider_Token.png" },
merit = { icon = "Icon_Merit.png" },
seed = { icon = "Icon_Nature.png",
invert = true },
skill = { icon = "Icon_Skill_Point.png",
invert = true },
}
Wrappers
Wrapper templates are the editor-facing interface of the icon system. Each wrapper template invokes Module:IconData with a fixed dataset name and forwards arguments from the caller to the module, keeping the dataset and module invisible at the call site.
Example wrapper template using Template:Currency:
<includeonly>{{#invoke:IconData|main
| dataset = Currency
| key = {{{key|{{{1|}}}}}}
| text = {{formatnum:{{{text|{{{2|}}}}}}}}
| link = {{{link|{{{3|}}}}}}
| size = {{{size|}}}}}<!--
--></includeonly><noinclude><!--
-->{{Documentation}}</noinclude>
Adding a new dataset and wrapper
Replace <DatasetName> with the name of the new dataset throughout.
- Create
Module:IconData/<DatasetName>using the dataset structure below. - Create a wrapper template at
Template:<DatasetName>using the wrapper structure below.
return {
key = { icon = "Icon_Name.png" },
}
<includeonly>{{#invoke:IconData|main
| dataset = <DatasetName>
| key = {{{key|{{{1|}}}}}}
| text = {{{text|{{{2|}}}}}}
| link = {{{link|{{{3|}}}}}}
| size = {{{size|}}}}}<!--
--></includeonly><noinclude><!--
-->{{Documentation}}</noinclude>
The key argument is lowercased before lookup, so callers may pass keys in any case. The text, link, and size arguments are forwarded to and documented at Module:Icon.
Errors
Raises an error when:
- No
datasetis supplied. - No
keyis supplied for the given dataset. - The supplied
keyis not present in the dataset. - The named dataset does not exist;
mw.loadDataraises a "module not found" error.
All errors are caught by main, rendered as an inline {{{1}}}, and tracked via Category:Pages with icon errors.
See also
- Module:Icon: performs the actual rendering.
--- Looks up an icon by key in a named dataset and renders it.
local Icon = require('Module:Icon')
local IconData = {}
local ERROR_CATEGORY = 'Pages with icon errors'
--- @class IconDataEntry
--- @field icon string File name of the icon
--- @field invert boolean|nil Whether to invert the icon in light mode
--- Trim a value and return nil if the result is blank.
local function nilIfBlank(value)
if value == nil then return nil end
value = mw.text.trim(value)
if value == '' then return nil end
return value
end
--- Render an inline error message and add the tracking category.
---
--- @param message string Error message text
--- @return string Wikitext for the styled error and category link
local function renderError(message)
return table.concat({tostring(mw.html.create('span')
:addClass('error')
:wikitext(message)),
'[[Category:', ERROR_CATEGORY, ']]'
})
end
--- Look up an entry by key in the given dataset.
---
--- @param dataset string Name of the dataset subpage (e.g., "Ammo")
--- @param key string Lookup key
--- @return IconDataEntry|nil Entry for the given key, or nil if not found
local function findEntry(dataset, key)
local data = mw.loadData('Module:IconData/' .. dataset)
return data[string.lower(key)]
end
--- Resolve args to an entry and forward to Icon.buildIcon.
--- Raises an error when required args are missing or the key is not in the dataset.
---
--- @param args table Named args from the invocation
--- @return string Wikitext for the rendered icon
local function build(args)
local dataset = nilIfBlank(args.dataset)
if not dataset then
error('Module:IconData: missing dataset', 0)
end
local key = nilIfBlank(args.key)
if not key then
error(string.format('Module:IconData: missing key for dataset "%s"', dataset), 0)
end
local entry = findEntry(dataset, key)
if not entry then
error(string.format('Module:IconData: no entry "%s" in dataset "%s"', key, dataset), 0)
end
return Icon.buildIcon({
icon = entry.icon,
text = nilIfBlank(args.text),
link = nilIfBlank(args.link),
size = args.size,
invert = entry.invert,
})
end
--- Entry point: {{#invoke:IconData|main|...}}
--- Catches errors raised during lookup or rendering and displays them inline
--- with a tracking category for maintenance.
---
--- @param frame table Scribunto frame object; reads named args from frame.args
--- @return string Wikitext for the rendered icon, or an error display
function IconData.main(frame)
local success, result = pcall(build, frame.args)
if success then
return result
end
return renderError(result)
end
return IconData