Modul:Get: Unterschied zwischen den Versionen
Die Seite wurde neu angelegt: „-- Modul:Get -- Allgemeiner Zugriff auf strukturierte Daten (Item/Event/Hero/Guardian/…) -- Aufruf über Vorlage:Get → {{Get|DATA|METHODE|KEY}} local p = {} local DATA_MODULE = 'Modul:Get/Data' local data = mw.loadData( DATA_MODULE ) -- -------------------------------------------------------- -- Hilfsfunktionen -- -------------------------------------------------------- local function fileExists(name) if not name or name == '' then ret…“ |
Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
-- Modul:Get | -- Modul:Get | ||
-- Allgemeiner Zugriff auf strukturierte Daten (Item/Event/Hero/Guardian/…) | -- Allgemeiner Zugriff auf strukturierte Daten (Item/Event/Hero/Guardian/…) | ||
local p = {} | local p = {} | ||
local DATA_MODULE = 'Modul:Get/Data' | local DATA_MODULE = 'Modul:Get/Data' | ||
local data = mw.loadData( DATA_MODULE ) | local data = mw.loadData(DATA_MODULE) | ||
-- Datei-Existenz prüfen | |||
local function fileExists(name) | local function fileExists(name) | ||
if not name or name == '' then | if not name or name == '' then | ||
| Zeile 17: | Zeile 13: | ||
end | end | ||
local titleObj = mw.title.makeTitle('File', name) | local titleObj = mw.title.makeTitle('File', name) | ||
if not titleObj then | if not titleObj then | ||
| Zeile 31: | Zeile 26: | ||
end | end | ||
-- $1 → title | |||
local function applyPlaceholder(text, title) | local function applyPlaceholder(text, title) | ||
if not text or text == '' then | if not text or text == '' then | ||
| Zeile 36: | Zeile 32: | ||
end | end | ||
-- | -- %$1 = literal "$1" | ||
return text:gsub('$1', function() | return text:gsub('%$1', function() | ||
return title or '' | return title or '' | ||
end) | end) | ||
end | end | ||
-- Tooltip + optional Link | |||
local function wrapWithTooltip(labelText, linkTarget, desc, noTooltip) | local function wrapWithTooltip(labelText, linkTarget, desc, noTooltip) | ||
if not labelText or labelText == '' then | if not labelText or labelText == '' then | ||
| Zeile 51: | Zeile 48: | ||
-- Link bauen, falls vorhanden | -- Link bauen, falls vorhanden | ||
if linkTarget and linkTarget ~= '' then | if linkTarget and linkTarget ~= '' then | ||
if linkTarget:match('^%[%[') or linkTarget:match('^%[') then | if linkTarget:match('^%[%[') or linkTarget:match('^%[') then | ||
-- bereits fertiger Wiki-/Externer Link | |||
out = linkTarget | out = linkTarget | ||
else | else | ||
| Zeile 61: | Zeile 57: | ||
end | end | ||
-- Tooltip | -- Tooltip über title-Attribut | ||
if desc and desc ~= '' and not noTooltip then | if desc and desc ~= '' and not noTooltip then | ||
local escDesc = desc:gsub('"', '"') | local escDesc = desc:gsub('"', '"') | ||
| Zeile 68: | Zeile 64: | ||
return out | 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 | end | ||
| Zeile 75: | Zeile 107: | ||
function p.main(frame) | function p.main(frame) | ||
local args = frame.args | |||
local args | local datasetRaw = args.dataset or args[1] | ||
local | local method = args.method or args[2] or 'link' | ||
local method | local keyRaw = args.key or args[3] | ||
local | local noTooltip = args.noTooltip == '1' or args.notooltip == '1' | ||
local noTooltip = args.noTooltip == '1' or args.notooltip == '1' | |||
if not | local datasetKey = normalizeDatasetKey(datasetRaw) | ||
local key = normalizeKey(keyRaw) | |||
if not datasetKey or not key then | |||
return '' | return '' | ||
end | end | ||
local group = data[ | local group = data[datasetKey] | ||
if not group then | if not group then | ||
-- unbekannter Datensatz → gib | -- unbekannter Datensatz → gib originalen Key zurück (zerstört Seite nicht) | ||
return | return keyRaw or '' | ||
end | end | ||
local raw = group[key] | local raw = group[key] | ||
if not raw then | if not raw then | ||
return keyRaw or '' | |||
return | |||
end | end | ||
local title = raw.title or | local displayKey = keyRaw or key | ||
local short = raw.short or | local title = raw.title or displayKey | ||
local short = raw.short or title | |||
-- Platzhalter | -- Platzhalter anwenden | ||
local desc = applyPlaceholder(raw.desc, title) | local desc = applyPlaceholder(raw.desc, title) | ||
local link = applyPlaceholder(raw.link, title) | local link = applyPlaceholder(raw.link, title) | ||
-- Bild | -- Bild bestimmen | ||
local chosenImg | local chosenImg | ||
if raw.img and fileExists(raw.img) then | if raw.img and fileExists(raw.img) then | ||
chosenImg = raw.img | chosenImg = raw.img | ||
elseif fileExists(title) then | elseif fileExists(title) then | ||
-- | -- Fallback: Datei mit gleichem Namen wie der Titel | ||
chosenImg = title | chosenImg = title | ||
end | end | ||
method = tostring(method):lower() | method = tostring(method):lower() | ||
if method == 'title' then | if method == 'title' then | ||
return wrapWithTooltip(title, nil, desc, noTooltip) | return wrapWithTooltip(title, nil, desc, noTooltip) | ||
elseif method == 'short' then | elseif method == 'short' then | ||
return wrapWithTooltip(short, nil, desc, noTooltip) | return wrapWithTooltip(short, nil, desc, noTooltip) | ||
elseif method == 'link' then | elseif method == 'link' then | ||
return wrapWithTooltip(short, link, desc, noTooltip) | return wrapWithTooltip(short, link, desc, noTooltip) | ||
elseif method == 'img' then | elseif method == 'img' then | ||
if not chosenImg then | if not chosenImg then | ||
return '' | return '' | ||
| Zeile 138: | Zeile 165: | ||
local alt = title or '' | local alt = title or '' | ||
if link and link ~= '' and not | if link and link ~= '' and not link:match('^%[') then | ||
-- internes | -- internes Linkziel | ||
return '[[Datei:' .. chosenImg .. '|link=' .. link .. '|alt=' .. alt .. ']]' | return '[[Datei:' .. chosenImg .. '|link=' .. link .. '|alt=' .. alt .. ']]' | ||
else | else | ||
return '[[Datei:' .. chosenImg .. '|alt=' .. alt .. ']]' | return '[[Datei:' .. chosenImg .. '|alt=' .. alt .. ']]' | ||
end | end | ||
elseif method == 'desc' then | elseif method == 'desc' then | ||
return desc or '' | return desc or '' | ||
else | else | ||
-- | -- Fallback: wie "link" | ||
return wrapWithTooltip(short, link, desc, noTooltip) | return wrapWithTooltip(short, link, desc, noTooltip) | ||
end | end | ||
Version vom 8. Dezember 2025, 22:07 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 = mw.loadData(DATA_MODULE)
-- Datei-Existenz prüfen
local function fileExists(name)
if not name or name == '' then
return false
end
local titleObj = mw.title.makeTitle('File', name)
if not titleObj then
return false
end
local fileObj = titleObj.file
if not fileObj or not fileObj.exists then
return false
end
return true
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)
-- Bild bestimmen
local chosenImg
if raw.img and fileExists(raw.img) then
chosenImg = raw.img
elseif fileExists(title) then
-- Fallback: Datei mit gleichem Namen wie der Titel
chosenImg = title
end
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
if not chosenImg then
return ''
end
local alt = title or ''
if link and link ~= '' and not link:match('^%[') then
-- internes Linkziel
return '[[Datei:' .. chosenImg .. '|link=' .. link .. '|alt=' .. alt .. ']]'
else
return '[[Datei:' .. chosenImg .. '|alt=' .. alt .. ']]'
end
elseif method == 'desc' then
return desc or ''
else
-- Fallback: wie "link"
return wrapWithTooltip(short, link, desc, noTooltip)
end
end
return p