Module:BadgeLua: Difference between revisions
More actions
starcitizen>Alistar Bot Icon via Module:Icon (+ optional mask); add whole-badge link prop (via update-page on MediaWiki MCP Server) |
Bonsaibauer (talk | contribs) m 1 revision imported: Import of Star-Citizen-Modules |
||
(No difference)
| |||
Latest revision as of 21:58, 4 November 2026
|type = website |description = Module:BadgeLua is a template page used on the Star Citizen Wiki. Templates are pages that are embedded (transcluded) into other pages to allow for the repetition of information. |site_name = Star Citizen Wiki |locale = en-gb }}
Lua interface for an inline pill-shaped badge, with an optional icon, custom text color, and custom background. Suited to status tags, version markers, or any short metadata that should read as a distinct chip rather than plain text.
Editors use this through {{Badge}}; see Template:Badge. Required directly by several rendering modules, including Module:Rarity, Module:DietaryEffect, and Module:Entity/ProductionStatus.
For module editors
API
p.render(props): builds and returns the badge markup plus its bundled<templatestyles>tags.props:text(required),variant(error/success/warning),icon(file name, rendered via Module:Icon at 16px),mask(render the icon as acurrentColormask instead of a thumbnail),link(wrap the whole pill in one anchor),color/backgroundColor(inline CSS overrides),class.p.main(frame): wikitext entry point behind{{Badge}}. Reads args via Module:Arguments; the first positional argument becomestext, andbgis a shorthand forbackgroundColor(an explicitbackgroundColor=wins).maskis normalised through Module:Yesno.
Gotchas
- An unrecognised
variantis silently ignored, so a typo never emits a broken half-styled class. color/backgroundColorrender as inline styles, so they win over thevariantclass for text and background color, but a variant's border color has no inline counterpart and stays whatever the class set.- Icon's own
<templatestyles>tag is appended only wheniconis set, so a plain text badge doesn't load it unconditionally. linkwraps the already-rendered pill string in[[Target|…]]; the<templatestyles>tags stay outside the link label.
Styles
.t-badge, .t-badge__icon, and .t-badge__text are a contract other code targets directly rather than through props: Module:Rarity and Module:Entity/ProductionStatus override the base look with higher-specificity selectors (.t-badge.rarity-badge--*), and the aggridRenderers gadget (MediaWiki:Gadget-aggridRenderers.js/.css) reconstructs the same class names in JS so an AG Grid badge cell matches a wikitext one. Renaming a class here breaks all of them silently.
require('strict')
--- Lua interface for building a badge.
local Icon = require('Module:Icon')
--- @class BadgeProps
--- @field text string
--- @field variant 'error'|'success'|'warning'
--- @field color string
--- @field backgroundColor string
--- @field icon string
--- @field mask boolean Render the icon as a currentColor mask (matches the badge colour)
--- @field link string Wrap the whole badge in a link to this page
--- @field class string
local VARIANTS = {
error = true,
success = true,
warning = true,
}
local p = {}
--- @param props BadgeProps
--- @return string
function p.render(props)
local root = mw.html.create('span'):addClass('t-badge')
if type(props.variant) == 'string' and VARIANTS[props.variant] then
root:addClass('t-badge--' .. props.variant)
end
if type(props.class) == 'string' then
root:addClass(props.class)
end
if type(props.color) == 'string' then
root:css('color', props.color)
end
if type(props.backgroundColor) == 'string' then
root:css('background-color', props.backgroundColor)
end
if type(props.icon) == 'string' then
root:wikitext(Icon.render({
icon = props.icon,
size = '16px',
mask = props.mask,
class = 't-badge__icon',
}))
end
root:tag('span'):addClass('t-badge__text'):wikitext(props.text)
local frame = mw.getCurrentFrame()
local styles = frame:extensionTag({
name = 'templatestyles',
args = { src = 'Module:BadgeLua/styles.css' },
})
-- Icon ships its own stylesheet; only needed when the badge has an icon.
if type(props.icon) == 'string' then
styles = styles
.. frame:extensionTag({
name = 'templatestyles',
args = { src = 'Module:Icon/styles.css' },
})
end
local badge = tostring(root)
-- Wrap the whole pill in one anchor so the entire badge is the link, not just
-- its text. The templatestyles tags stay outside the link label.
if type(props.link) == 'string' and props.link ~= '' then
badge = string.format('[[%s|%s]]', props.link, badge)
end
return styles .. badge
end
--- Wikitext entry point for the module
---
--- Accepts shorthand argument names for the most common props:
--- - first positional argument is the badge text
--- - `bg` is an alias for `backgroundColor`
---
--- @param frame mw.frame
--- @return string
function p.main(frame)
local getArgs = require('Module:Arguments').getArgs
local args = getArgs(frame)
if args.text == nil then
args.text = args[1]
end
if args.backgroundColor == nil then
args.backgroundColor = args.bg
end
if args.mask ~= nil then
args.mask = require('Module:Yesno')(args.mask)
end
return p.render(args)
end
return p