Modul:BündelGalerie: Unterschied zwischen den Versionen

Aus Dreamlight Valley Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
 
(7 dazwischenliegende Versionen desselben Benutzers werden nicht angezeigt)
Zeile 1: Zeile 1:
local p = {}
local p = {}


-- kleine Helfer
local function trim(s)
local function trim(s)
if not s then return "" end
if not s then return "" end
return (mw.text.trim(s))
return mw.text.trim(tostring(s))
end
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 function splitItems(s)
local out = {}
local out = {}
if not s or s == "" then return out end
if not s or s == "" then
return out
end
 
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
table.insert(out, part)
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
end
end
return out
return out
end
end


local function findBundleLine(content, key)
local function getBundleData(bundleKey)
key = mw.ustring.lower(trim(key or ""))
local tpl = mw.title.new("Vorlage:BündelDaten")
if key == "" then return nil end
if not tpl or not tpl.exists then
return nil, 'Vorlage "BündelDaten" existiert nicht.'
end
 
local content = tpl:getContent() or ""
bundleKey = trim(bundleKey)


for line in mw.text.gsplit(content or "", "\n", true) do
local keyEsc = escapeLuaPattern(bundleKey)
local raw = trim(line)
local pattern = "\n%s*" .. keyEsc .. "%s*=%s*(.-)%s*\n"
if raw ~= "" and raw:sub(1,1) ~= "<" then
local value = mw.ustring.match("\n" .. content .. "\n", pattern)
local k, v = raw:match("^([^=]+)=%s*(.+)$")
 
if k and v then
if not value then
k = mw.ustring.lower(trim(k))
return nil, 'Bündel "' .. bundleKey .. '" nicht gefunden.'
if k == key then
return trim(v)
end
end
end
end
end
return nil
 
return trim(value), nil
end
end


function p.main(frame)
function p.render(frame)
local args = frame:getParent() and frame:getParent().args or frame.args
local args = frame.args or {}
local bundle = args[1] or args.name or ""
local parent = frame:getParent()
bundle = trim(bundle)


local elementTemplate = trim(args.element or "Galerie") -- Name deiner Galerie-Vorlage
if parent and parent.args then
local dataPage = trim(args.data or "Vorlage:BündelDaten")
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
if bundle == "" then
return '<span class="error">BündelGalerie: Kein Bündelname angegeben.</span>'
return '<span class="error">Lua-Fehler: Kein Bündel angegeben. Nutze bundle=...</span>'
end
 
local title = mw.title.new(dataPage)
if not title then
return '<span class="error">BündelGalerie: Datenseite nicht gefunden: ' .. mw.text.nowiki(dataPage) .. '</span>'
end
end


local content = title:getContent() or ""
local title = trim(args.title or "")
local itemsStr = findBundleLine(content, bundle)


if not itemsStr or itemsStr == "" then
local raw, err = getBundleData(bundle)
return '<span class="error">BündelGalerie: Bündel nicht gefunden: ' .. mw.text.nowiki(bundle) .. '</span>'
if err then
return '<span class="error">Lua-Fehler: ' .. mw.text.nowiki(err) .. "</span>"
end
end


local items = splitItems(itemsStr)
local items = splitItems(raw)
if #items == 0 then
if #items == 0 then
return '<span class="error">BündelGalerie: Bündel ist leer: ' .. mw.text.nowiki(bundle) .. '</span>'
return '<span class="error">Lua-Fehler: Bündel "' .. mw.text.nowiki(bundle) .. '" ist leer.</span>'
end
end


local out = {}
local out = {}
if title ~= "" then
table.insert(out, "==" .. title .. "==")
end
table.insert(out, '<div class="bundle-gallery">')
for _, item in ipairs(items) do
for _, item in ipairs(items) do
table.insert(out, frame:expandTemplate{
local imageBase = (item.image or ""):gsub("%.png$", "")
title = elementTemplate,
local badge = trim(item.badge or "")
args = { item }
 
})
local wikitext = frame:expandTemplate{
title = "Galerie",
args = {
[1] = imageBase,
[2] = badge,
link = item.name
}
}
table.insert(out, wikitext)
end
end
table.insert(out, "</div>")


return table.concat(out, "\n")
return table.concat(out, "\n")
end
end
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