Module:Labeled icon: Difference between revisions
From Space Station 14 Wiki
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
local getArgs = require('Module:Arguments').getArgs | local getArgs = require('Module:Arguments').getArgs | ||
-- =========================== | |||
-- # Internal vars # | |||
-- Whether template styles were added. | |||
local were_template_styles_added = false | |||
-- =========================== | |||
local function assert_not_nil(value, error_message) | local function assert_not_nil(value, error_message) | ||
Line 30: | Line 38: | ||
end | end | ||
return newt | return newt | ||
end | |||
-- =================== | |||
local function generate_template_styles(frame) | |||
return frame:extensionTag("templatestyles", "", { src = 'Template:Labeled icon/styles.css' }) | |||
end | end | ||
Line 62: | Line 76: | ||
local container_el = mw.html.create('div') | local container_el = mw.html.create('div') | ||
:addClass("li-c") | :addClass("li-c") | ||
if not were_template_styles_added then | |||
container_el.node(generate_template_styles(frame)) | |||
were_template_styles_added = true | |||
end | |||
container_el | |||
:wikitext("[[File:" .. icon .. "|" .. icon_size .. "|link=" .. (link or "") .. "|class=lc-i]]") | :wikitext("[[File:" .. icon .. "|" .. icon_size .. "|link=" .. (link or "") .. "|class=lc-i]]") | ||
:node(label_el) | :node(label_el) |
Revision as of 19:23, 30 March 2025
Module documentation
|
---|
View or edit this documentation • (about module documentation) |
Implements {{Labeled icon}}.
local p = {}
local getArgs = require('Module:Arguments').getArgs
-- ===========================
-- # Internal vars #
-- Whether template styles were added.
local were_template_styles_added = false
-- ===========================
local function assert_not_nil(value, error_message)
if value == nil then
if error_message == nil then
error("value is nil")
else
error(error_message)
end
end
end
local function numeric_table_length(t)
local count = 0
for _ in ipairs(t) do count = count + 1 end
return count
end
local function table_length(t)
local count = 0
for _ in pairs(t) do count = count + 1 end
return count
end
local function map_table(t, f)
local newt = {}
for k, v in pairs(t) do
newt[k] = f(k, v, t)
end
return newt
end
-- ===================
local function generate_template_styles(frame)
return frame:extensionTag("templatestyles", "", { src = 'Template:Labeled icon/styles.css' })
end
-- ===================
function p.main(frame)
local args = getArgs(frame)
-- [REQUIRED]
local icon = args[1]
assert_not_nil(icon, "failed to generate labeled icon: icon not provided");
local icon_size = args[2]
assert_not_nil(icon, "failed to generate labeled icon: icon size not provided");
local label = args[3]
assert_not_nil(icon, "failed to generate labeled icon: label not provided");
-- [OPTIONAL]
local link = args[4]
local zoom = args.zoom
-- ============
local label_el = mw.html.create('span')
:addClass("lc-l")
:wikitext(link and "[[" .. link .. "|" .. label .. "]]" or label)
local container_el = mw.html.create('div')
:addClass("li-c")
if not were_template_styles_added then
container_el.node(generate_template_styles(frame))
were_template_styles_added = true
end
container_el
:wikitext("[[File:" .. icon .. "|" .. icon_size .. "|link=" .. (link or "") .. "|class=lc-i]]")
:node(label_el)
return container_el
end
return p