Modul:Get: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 10: | Zeile 10: | ||
local function fileExists(name) | local function fileExists(name) | ||
if not name or name == '' then | if not name or name == '' then | ||
return | return nil | ||
end | |||
local function check(n) | |||
local titleObj = mw.title.makeTitle('File', n) | |||
if not titleObj then | |||
return nil | |||
end | |||
local fileObj = titleObj.file | |||
if fileObj and fileObj.exists then | |||
return titleObj.text -- z.B. "CurrencyGold.png" | |||
end | |||
return nil | |||
end | end | ||
local | -- 1. Versuch: Name wie er übergeben wird | ||
if | local found = check(name) | ||
return | if found then | ||
return found | |||
end | end | ||
-- 2. Versuch: wenn keine Endung angegeben → .png anhängen | |||
if not | if not name:match('%.') then | ||
return | found = check(name .. '.png') | ||
if found then | |||
return found | |||
end | |||
end | end | ||
return | return nil | ||
end | end | ||
| Zeile 138: | Zeile 154: | ||
local desc = applyPlaceholder(raw.desc, title) | local desc = applyPlaceholder(raw.desc, title) | ||
local link = applyPlaceholder(raw.link, title) | local link = applyPlaceholder(raw.link, title) | ||
method = tostring(method):lower() | method = tostring(method):lower() | ||
| Zeile 159: | Zeile 166: | ||
return wrapWithTooltip(short, link, desc, noTooltip) | return wrapWithTooltip(short, link, desc, noTooltip) | ||
elseif method == 'img' then | |||
local imgName | |||
if raw.img then | |||
imgName = fileExists(raw.img) | |||
end | |||
if not imgName and title then | |||
imgName = fileExists(title) | |||
end | |||
if not imgName then | |||
return '' | |||
end | |||
return imgName | |||
elseif method == 'desc' then | elseif method == 'desc' then | ||
Version vom 8. Dezember 2025, 23:17 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Get/Doku erstellt werden
-- Modul:Get
-- Allgemeiner Zugriff auf strukturierte Daten (Item/Event/Hero/Guardian/…)
local p = {}
local DATA_MODULE = 'Modul:Get/Data'
local data = require(DATA_MODULE)
-- Datei-Existenz prüfen
local function fileExists(name)
if not name or name == '' then
return nil
end
local function check(n)
local titleObj = mw.title.makeTitle('File', n)
if not titleObj then
return nil
end
local fileObj = titleObj.file
if fileObj and fileObj.exists then
return titleObj.text -- z.B. "CurrencyGold.png"
end
return nil
end
-- 1. Versuch: Name wie er übergeben wird
local found = check(name)
if found then
return found
end
-- 2. Versuch: wenn keine Endung angegeben → .png anhängen
if not name:match('%.') then
found = check(name .. '.png')
if found then
return found
end
end
return nil
end
-- $1 → title
local function applyPlaceholder(text, title)
if not text or text == '' then
return nil
end
-- %$1 = literal "$1"
return text:gsub('%$1', function()
return title or ''
end)
end
-- Tooltip + optional Link
local function wrapWithTooltip(labelText, linkTarget, desc, noTooltip)
if not labelText or labelText == '' then
return ''
end
local out = labelText
-- Link bauen, falls vorhanden
if linkTarget and linkTarget ~= '' then
if linkTarget:match('^%[%[') or linkTarget:match('^%[') then
-- bereits fertiger Wiki-/Externer Link
out = linkTarget
else
-- interner Link: [[Seite|Label]]
out = '[[' .. linkTarget .. '|' .. labelText .. ']]'
end
end
-- Tooltip über title-Attribut
if desc and desc ~= '' and not noTooltip then
local escDesc = desc:gsub('"', '"')
out = '<span class="kr-get" title="' .. escDesc .. '">' .. out .. '</span>'
end
return out
end
-- Keys normalisieren: trim, lower, Leerzeichen → _
local function normalizeKey(key)
if not key then
return nil
end
key = mw.text.trim(tostring(key))
if key == '' then
return nil
end
key = mw.ustring.lower(key)
key = key:gsub('[%s_]+', '_')
return key
end
-- Datensatz-Namen (Item/Items, Hero/Heroes…) normalisieren
local function normalizeDatasetKey(datasetRaw)
local datasetKey = normalizeKey(datasetRaw)
if not datasetKey then
return nil
end
if datasetKey == 'items' then
datasetKey = 'item'
elseif datasetKey == 'events' then
datasetKey = 'event'
elseif datasetKey == 'heroes' then
datasetKey = 'hero'
elseif datasetKey == 'guardians' then
datasetKey = 'guardian'
end
return datasetKey
end
-- --------------------------------------------------------
-- Hauptfunktion
-- --------------------------------------------------------
function p.main(frame)
local args = frame.args
local datasetRaw = args.dataset or args[1]
local method = args.method or args[2] or 'link'
local keyRaw = args.key or args[3]
local noTooltip = args.noTooltip == '1' or args.notooltip == '1'
local datasetKey = normalizeDatasetKey(datasetRaw)
local key = normalizeKey(keyRaw)
if not datasetKey or not key then
return ''
end
local group = data[datasetKey]
if not group then
-- unbekannter Datensatz → gib originalen Key zurück (zerstört Seite nicht)
return keyRaw or ''
end
local raw = group[key]
if not raw then
return keyRaw or ''
end
local displayKey = keyRaw or key
local title = raw.title or displayKey
local short = raw.short or title
-- Platzhalter anwenden
local desc = applyPlaceholder(raw.desc, title)
local link = applyPlaceholder(raw.link, title)
method = tostring(method):lower()
if method == 'title' then
return wrapWithTooltip(title, nil, desc, noTooltip)
elseif method == 'short' then
return wrapWithTooltip(short, nil, desc, noTooltip)
elseif method == 'link' then
return wrapWithTooltip(short, link, desc, noTooltip)
elseif method == 'img' then
local imgName
if raw.img then
imgName = fileExists(raw.img)
end
if not imgName and title then
imgName = fileExists(title)
end
if not imgName then
return ''
end
return imgName
elseif method == 'desc' then
return desc or ''
else
-- Fallback: wie "link"
return wrapWithTooltip(short, link, desc, noTooltip)
end
end
return p