Modul:BündelGalerie: Unterschied zwischen den Versionen

Aus Dreamlight Valley Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
Zeile 8: Zeile 8:


local function escapeLuaPattern(s)
local function escapeLuaPattern(s)
-- escaped Sonderzeichen für Lua-Patterns
return (mw.ustring.gsub(tostring(s), "([%%%^%$%(%)%.%[%]%*%+%-%?])", "%%%1"))
return (mw.ustring.gsub(tostring(s), "([%%%^%$%(%)%.%[%]%*%+%-%?])", "%%%1"))
end
end


-- liest "Item; Item2; Item3" ODER "Item|Bild.png; Item2; Item3|Bild2.png"
-- liest:
-- und liefert eine Tabelle mit { name = "...", image = "....png" }
--  "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 function splitItems(s)
local out = {}
local out = {}
Zeile 20: Zeile 26:
end
end


-- Trennung: Semikolon, mit optionalen Leerzeichen drumherum
for part in mw.text.gsplit(s, "%s*;%s*") do
for part in mw.text.gsplit(s, "%s*;%s*") do
part = trim(part)
part = trim(part)
if part ~= "" then
if part ~= "" then
-- optionales Format: name|image
local a, b, c = part:match("^(.-)%|(.-)%|(.-)$")
local name, image = part:match("^(.-)%|(.-)$")
if a then
if name then
-- name|image|badge
name = trim(name)
local name = trim(a)
image = trim(image)
local image = trim(b)
-- falls jemand ".png" vergisst, ergänzen wir es sanft
local badge = trim(c)
 
if image ~= "" and not image:lower():match("%.png$") then
if image ~= "" and not image:lower():match("%.png$") then
image = image .. ".png"
image = image .. ".png"
end
end
table.insert(out, { name = name, image = image })
 
table.insert(out, { name = name, image = image, badge = badge })
 
else
else
-- Standard: Bildname = Itemname + .png
local x, y = part:match("^(.-)%|(.-)$")
table.insert(out, { name = part, image = part .. ".png" })
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
end
Zeile 44: Zeile 76:
end
end


-- holt Bündeldaten aus Vorlage:BündelDaten (Wikitext)
local function getBundleData(bundleKey)
local function getBundleData(bundleKey)
local tpl = mw.title.new("Vorlage:BündelDaten")
local tpl = mw.title.new("Vorlage:BündelDaten")
Zeile 54: Zeile 85:
bundleKey = trim(bundleKey)
bundleKey = trim(bundleKey)


-- Robust: Zeilenanfang + Key (auch mit Leerzeichen/"") + optional spaces + = + value bis Zeilenende
local keyEsc = escapeLuaPattern(bundleKey)
local keyEsc = escapeLuaPattern(bundleKey)
local pattern = "\n%s*" .. keyEsc .. "%s*=%s*(.-)%s*\n"
local pattern = "\n%s*" .. keyEsc .. "%s*=%s*(.-)%s*\n"
Zeile 66: Zeile 96:
end
end


-- Hauptfunktion: {{#invoke:BündelGalerie|render|bundle=...}}
function p.render(frame)
function p.render(frame)
local args = frame.args or {}
local args = frame.args or {}
local parent = frame:getParent()
local parent = frame:getParent()


-- Parent-Args übernehmen (für Aufruf über Vorlage)
if parent and parent.args then
if parent and parent.args then
for k, v in pairs(parent.args) do
for k, v in pairs(parent.args) do
Zeile 85: Zeile 113:
end
end


-- optionaler Titel (Wiki-Überschrift)
local title = trim(args.title or "")
local title = trim(args.title or "")
-- optional: Badge-Ecke im Galerie (premium / sternenpfad)
local badge = trim(args.badge or "")


local raw, err = getBundleData(bundle)
local raw, err = getBundleData(bundle)
Zeile 102: Zeile 127:
local out = {}
local out = {}


-- optionaler Titel
if title ~= "" then
if title ~= "" then
table.insert(out, "==" .. title .. "==")
table.insert(out, "==" .. title .. "==")
end
end


-- Galerie-Wrapper (via CSS stylen)
table.insert(out, '<div class="bundle-gallery">')
table.insert(out, '<div class="bundle-gallery">')


for _, item in ipairs(items) do
for _, item in ipairs(items) do
-- Galerie erwartet als 1. Parameter den Dateinamen OHNE .png
local imageBase = (item.image or ""):gsub("%.png$", "")
local imageBase = item.image:gsub("%.png$", "")
local badge = trim(item.badge or "")
 
local wikitext = frame:expandTemplate{
local wikitext = frame:expandTemplate{
title = "Galerie",
title = "Galerie",
Zeile 129: Zeile 153:
end
end


-- optionaler Alias: falls irgendwo noch |main aufgerufen wird
p.main = p.render
p.main = p.render


return p
return p

Aktuelle Version vom 24. Februar 2026, 17:30 Uhr

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