Modul:Icons
-- Modul:Icons
-- Gibt ein Icon als HTML aus, je nach Typ (FontAwesome oder SVG)
-- Wrapper-Vorlagen: fa, svg, icon
local p = {}
-- Zentrale Tabelle aller Icons aus dem Data-Modul laden
local icons = mw.loadData("Modul:Icons/Data")
-- Standardwerte
local DEFAULT_TYPE = "fa"
local DEFAULT_SIZE = "1em"
local ERROR_ICON = '<span class="icon-error" title="Fehlendes Icon" aria-label="Fehlendes Icon">?</span>[[Kategorie:Fehlende Icons]]'
-- Hilfsfunktion für die Umrechnung von em in px bei SVG.
local function em2px(size)
if not size or size == "" then
return "20px"
end
-- Erlaubt z. B. "1.2em", "2em", "24px", "40"
local em = mw.ustring.match(size, "([%d%.]+)em")
if em then
local px = math.floor(tonumber(em) * 16 + 0.5)
return tostring(px) .. "px"
end
local px = mw.ustring.match(size, "(%d+)px")
if px then
return px .. "px"
end
-- Fallback: nur Zahl, ohne px/em
if tonumber(size) then
return tostring(size) .. "px"
end
return "20px" -- Standard
end
-- Hauptfunktion: Gibt das Icon aus (argumente: type, name, size)
function p._icon(args)
local type = args.type or DEFAULT_TYPE
local name = args.name if not name or name == "" then
return ERROR_ICON
end
local size = args.size or DEFAULT_SIZE
-- Lookup in der Icon-Tabelle
local icon = icons[name]
if not icon then
return ERROR_ICON
end
if icon.type ~= type then
return ERROR_ICON
end
-- ARIA/Tooltip-Label (optional)
local label = icon.label or name
-- Icon-Ausgabe nach Typ
if icon.type == "fa" then
-- FontAwesome: <i class="fa fa-user" ...>
return string.format(
'<i class="fa %s" style="font-size:%s;" title="%s" aria-label="%s"></i>',
icon.class, size, label, label
)
elseif icon.type == "svg" then
-- SVG: Als Detei über den Parser laden, Größe in EM wird umgerechnet!
local wikisize = em2px(size)
return string.format('[[Datei:%s|%s|class=icon-svg|alt=%s|%s]]', icon.class, wikisize, label, label)
else
-- Unbekannter Typ
return ERROR_ICON
end
end
-- Wrapper: für {{fa|name|size}}
function p.fa(frame)
local name = frame.args[1]
local size = frame.args[2]
return p._icon{type="fa", name=name, size=size}
end
-- Wrapper: für {{svg|name|size}}
function p.svg(frame)
local name = frame.args[1]
local size = frame.args[2]
return p._icon{type="svg", name=name, size=size}
end
-- Wrapper: für {{icon|type|name|size}}
function p.icon(frame)
local type = frame.args[1] or DEFAULT_TYPE
local name = frame.args[2]
local size = frame.args[3]
return p._icon{type=type, name=name, size=size}
end
return p