Modul:BündelGalerie
Die Dokumentation für dieses Modul kann unter Modul:BündelGalerie/Doku erstellt werden
local p = {}
-- kleine Helfer
local function trim(s)
if not s then return "" end
return mw.text.trim(tostring(s))
end
local function escapeLuaPattern(s)
return (mw.ustring.gsub(tostring(s), "([%%%^%$%(%)%.%[%]%*%+%-%?])", "%%%1"))
end
-- liest:
-- "Item; Item2; Item3"
-- ODER
-- "Item|Bild.png; Item2; Item3|Bild2.png"
-- NEU: optionaler Badge pro Item:
-- "Item|premium; Item2|sternenpfad; Item3"
-- "Item|Bild.png|premium; Item2|Bild2.png|sternenpfad"
--
-- liefert: { name="...", image="...png", badge="premium|sternenpfad|''" }
local function splitItems(s)
local out = {}
if not s or s == "" then
return out
end
for part in mw.text.gsplit(s, "%s*;%s*") do
part = trim(part)
if part ~= "" then
local a, b, c = part:match("^(.-)%|(.-)%|(.-)$")
if a then
-- name|image|badge
local name = trim(a)
local image = trim(b)
local badge = trim(c)
if image ~= "" and not image:lower():match("%.png$") then
image = image .. ".png"
end
table.insert(out, { name = name, image = image, badge = badge })
else
local x, y = part:match("^(.-)%|(.-)$")
if x then
-- name|something -> entweder image ODER badge
local name = trim(x)
local second = trim(y)
local badge = ""
local image = ""
-- Wenn second "premium"/"sternenpfad" ist -> Badge, sonst Bild
local secondLc = mw.ustring.lower(second)
if secondLc == "premium" or secondLc == "sternenpfad" then
badge = secondLc
image = name .. ".png"
else
image = second
if image ~= "" and not image:lower():match("%.png$") then
image = image .. ".png"
end
end
table.insert(out, { name = name, image = image, badge = badge })
else
-- nur name
table.insert(out, { name = part, image = part .. ".png", badge = "" })
end
end
end
end
return out
end
local function getBundleData(bundleKey)
local tpl = mw.title.new("Vorlage:BündelDaten")
if not tpl or not tpl.exists then
return nil, 'Vorlage "BündelDaten" existiert nicht.'
end
local content = tpl:getContent() or ""
bundleKey = trim(bundleKey)
local keyEsc = escapeLuaPattern(bundleKey)
local pattern = "\n%s*" .. keyEsc .. "%s*=%s*(.-)%s*\n"
local value = mw.ustring.match("\n" .. content .. "\n", pattern)
if not value then
return nil, 'Bündel "' .. bundleKey .. '" nicht gefunden.'
end
return trim(value), nil
end
function p.render(frame)
local args = frame.args or {}
local parent = frame:getParent()
if parent and parent.args then
for k, v in pairs(parent.args) do
if args[k] == nil or args[k] == "" then
args[k] = v
end
end
end
local bundle = trim(args.bundle or args[1])
if bundle == "" then
return '<span class="error">Lua-Fehler: Kein Bündel angegeben. Nutze bundle=...</span>'
end
local title = trim(args.title or "")
local raw, err = getBundleData(bundle)
if err then
return '<span class="error">Lua-Fehler: ' .. mw.text.nowiki(err) .. "</span>"
end
local items = splitItems(raw)
if #items == 0 then
return '<span class="error">Lua-Fehler: Bündel "' .. mw.text.nowiki(bundle) .. '" ist leer.</span>'
end
local out = {}
if title ~= "" then
table.insert(out, "==" .. title .. "==")
end
table.insert(out, '<div class="bundle-gallery">')
for _, item in ipairs(items) do
local imageBase = (item.image or ""):gsub("%.png$", "")
local badge = trim(item.badge or "")
local wikitext = frame:expandTemplate{
title = "Galerie",
args = {
[1] = imageBase,
[2] = badge,
link = item.name
}
}
table.insert(out, wikitext)
end
table.insert(out, "</div>")
return table.concat(out, "\n")
end
p.main = p.render
return p