Modul:Item: Unterschied zwischen den Versionen
Keine Bearbeitungszusammenfassung |
Keine Bearbeitungszusammenfassung |
||
| Zeile 1: | Zeile 1: | ||
-- Modul:Item – Firestone-Version | -- Modul:Item – Firestone-Version | ||
-- | -- | ||
-- | -- Methoden über Vorlage:Item: | ||
-- | -- {{Item|title|key[|1]}} | ||
-- | -- {{Item|img|key[|amount][|1]}} | ||
-- | -- {{Item|link|key[|1]}} | ||
-- | -- {{Item|type|key[|1]}} | ||
-- | -- {{Item|desc|key[|1]}} | ||
-- | -- | ||
-- * | -- 3./4. Parameter: | ||
-- * | -- * Zahl → amount (nur bei img relevant) | ||
-- * "1"/"tooltip"/"tt"/"hover"/"true" → Tooltip einschalten | |||
-- | -- | ||
-- Datengrundlage: [[Modul:Item/Data]] | -- Datengrundlage: [[Modul:Item/Data]] | ||
| Zeile 25: | Zeile 26: | ||
-- ========================= | -- ========================= | ||
local DEFAULT_404_IMG = '404' | |||
local DEFAULT_404_LINK = 'Modul:Item/Data' | |||
local DEFAULT_404_TYPE = 'unknown' | |||
local DEFAULT_TOOLTIP_MAX = 140 | |||
local DEFAULT_404_IMG | |||
local DEFAULT_404_LINK | |||
local DEFAULT_404_TYPE | |||
local DEFAULT_TOOLTIP_MAX = 140 | |||
local TYPE_LABELS = { | |||
currencies = 'Währung', | |||
unknown = 'Unbekannt', | |||
} | |||
-- ========================= | -- ========================= | ||
-- | -- Helpers | ||
-- ========================= | -- ========================= | ||
| Zeile 47: | Zeile 44: | ||
return nil | return nil | ||
end | end | ||
local s = ustr.lower(tostring(name)) | |||
s = s:gsub('%s+', '') | |||
return | return s | ||
end | end | ||
| Zeile 103: | Zeile 100: | ||
if item.title and item.title ~= '' then | if item.title and item.title ~= '' then | ||
return item.title | return item.title | ||
end | end | ||
return capitalize(item.key) | return capitalize(item.key) | ||
| Zeile 111: | Zeile 106: | ||
local function imgPngExists(imgname) | local function imgPngExists(imgname) | ||
if type(imgname) == 'string' and #imgname ~= 0 then | if type(imgname) == 'string' and #imgname ~= 0 then | ||
local t = mw.title.new('Datei:' .. imgname .. '.png') | local t = mw.title.new('Datei:' .. imgname .. '.png') | ||
return t and t.file and t.file.exists or false | return t and t.file and t.file.exists or false | ||
| Zeile 123: | Zeile 117: | ||
end | end | ||
local guess = title2link(getTitle(item)) | |||
local | if imgPngExists(guess) then | ||
if imgPngExists( | return guess | ||
return | |||
end | end | ||
| Zeile 132: | Zeile 125: | ||
end | end | ||
local function | local function getLinkTarget(item) | ||
if item.link and item.link ~= '' then | if item.link and item.link ~= '' then | ||
return item.link | return item.link | ||
end | end | ||
return title2link(getTitle(item)) | |||
return | |||
end | end | ||
local function getType(item) | local function getType(item) | ||
local group = ItemType.typenameForKey(item.key) | |||
local label = TYPE_LABELS[group] | |||
if label and label ~= '' then | |||
return label | |||
end | |||
return group or DEFAULT_404_TYPE | |||
end | end | ||
-- ---------- Markup → Plaintext (für Tooltip) ---------- | |||
-- | |||
-- Markup | |||
-- | |||
local function stripMarkup(str) | local function stripMarkup(str) | ||
| Zeile 159: | Zeile 153: | ||
str = str:gsub('%[%[([^%]]+)%]%]', '%1') | str = str:gsub('%[%[([^%]]+)%]%]', '%1') | ||
-- externe Links [ | -- externe Links [url Label] | ||
str = str:gsub('%[[^%s%]]+%s+([^%]]+)%]', '%1') | str = str:gsub('%[[^%s%]]+%s+([^%]]+)%]', '%1') | ||
-- Hervorhebungen | -- Hervorhebungen | ||
str = str:gsub("''+", '') | str = str:gsub("''+", '') | ||
-- Templates {{...}} | -- Templates {{...}} | ||
str = str:gsub('{{.-}}', '') | str = str:gsub('{{.-}}', '') | ||
-- HTML-Tags | -- HTML-Tags | ||
str = str:gsub('<.->', '') | str = str:gsub('<.->', '') | ||
-- Whitespace | -- Whitespace | ||
str = str:gsub('%s+', ' ') | str = str:gsub('%s+', ' ') | ||
if text and text.trim then | if text and text.trim then | ||
| Zeile 184: | Zeile 178: | ||
max = tonumber(max) or 0 | max = tonumber(max) or 0 | ||
if max <= 0 | if max <= 0 or ustr.len(str) <= max then | ||
return str | return str | ||
end | end | ||
| Zeile 201: | Zeile 191: | ||
end | end | ||
-- ---------- Tooltip ---------- | |||
-- | |||
-- Tooltip- | |||
-- | |||
local function buildTooltip(item, opts) | local function buildTooltip(item, opts) | ||
opts = opts or {} | opts = opts or {} | ||
local max = opts.max or DEFAULT_TOOLTIP_MAX | local max = opts.max or DEFAULT_TOOLTIP_MAX | ||
local label = opts.label | |||
local title = getTitle(item) | local title = getTitle(item) | ||
| Zeile 214: | Zeile 202: | ||
local shortDesc = shorten(desc, max) | local shortDesc = shorten(desc, max) | ||
local img = getImage(item) | local img = getImage(item) | ||
local parts = {} | local parts = {} | ||
-- Wrapper | -- Wrapper | ||
table.insert(parts, '<span class=" | table.insert(parts, '<span class="fs-item">') | ||
-- | -- sichtbarer Inhalt | ||
if | if label and label ~= '' then | ||
table.insert(parts, | table.insert(parts, label) | ||
else | else | ||
table.insert(parts, title) | local target = getLinkTarget(item) | ||
table.insert(parts, '[[' .. target .. '|' .. title .. ']]') | |||
end | end | ||
-- Tooltip-Box | -- Tooltip-Box | ||
table.insert(parts, '<span class=" | table.insert(parts, '<span class="tooltip">') | ||
-- Header: Icon + Titel | -- Header: Icon + Titel | ||
table.insert(parts, '<span class=" | table.insert(parts, '<span class="header">') | ||
if img and img ~= '' then | if img and img ~= '' then | ||
table.insert(parts, '[[Datei:' .. img .. '.png|24x24px|class= | table.insert(parts, '[[Datei:' .. img .. '.png|24x24px|class=icon]] ') | ||
end | end | ||
table.insert(parts, '<span class=" | table.insert(parts, '<span class="title">') | ||
table.insert(parts, title) | table.insert(parts, title) | ||
table.insert(parts, '</span></span>') -- title + header | table.insert(parts, '</span></span>') -- title + header | ||
-- | -- Text | ||
if shortDesc ~= '' then | if shortDesc ~= '' then | ||
table.insert(parts, '<span class=" | table.insert(parts, '<span class="body">') | ||
table.insert(parts, shortDesc) | table.insert(parts, shortDesc) | ||
table.insert(parts, '</span>') | table.insert(parts, '</span>') | ||
end | end | ||
table.insert(parts, '</span>') -- | table.insert(parts, '</span>') -- tooltip | ||
table.insert(parts, '</span>') -- | table.insert(parts, '</span>') -- wrapper | ||
return table.concat(parts) | return table.concat(parts) | ||
end | end | ||
-- ---------- Frame-Args ---------- | |||
-- | |||
-- | |||
-- | |||
local function extractKey(args) | local function extractKey(args) | ||
| Zeile 290: | Zeile 275: | ||
end | end | ||
local function isTooltipFlag(v) | |||
if not v then | |||
return false | |||
end | |||
local s = tostring(v):lower() | |||
return s == '1' or s == 'true' or s == 'yes' | |||
or s == 'y' or s == 'tooltip' or s == 'tt' or s == 'hover' | |||
end | |||
-- ========================= | -- ========================= | ||
-- | -- optionale direkte #invoke-Funktionen | ||
-- ========================= | -- ========================= | ||
| Zeile 301: | Zeile 294: | ||
end | end | ||
function p. | function p.img(frame) | ||
local key = select(1, getArgKey(frame)) | local key = select(1, getArgKey(frame)) | ||
local item = getItem(key) | local item = getItem(key) | ||
return | return getImage(item) | ||
end | end | ||
| Zeile 316: | Zeile 303: | ||
local key = select(1, getArgKey(frame)) | local key = select(1, getArgKey(frame)) | ||
local item = getItem(key) | local item = getItem(key) | ||
return | local target = getLinkTarget(item) | ||
return '[[' .. target .. '|' .. getTitle(item) .. ']]' | |||
end | end | ||
function p. | function p.type(frame) | ||
local key = select(1, getArgKey(frame)) | local key = select(1, getArgKey(frame)) | ||
local item = getItem(key) | local item = getItem(key) | ||
return | return getType(item) | ||
end | end | ||
function p. | function p.desc(frame) | ||
local key = select(1, getArgKey(frame)) | local key = select(1, getArgKey(frame)) | ||
local item = getItem(key) | local item = getItem(key) | ||
return | return item.describe or item.description or '-' | ||
end | end | ||
function p.tooltip(frame) | function p.tooltip(frame) | ||
local key, args = getArgKey(frame) | local key, args = getArgKey(frame) | ||
local item = getItem(key) | local item = getItem(key) | ||
local max = args.max or args.len or args.limit | local max = tonumber(args.max or args.len or args.limit) or DEFAULT_TOOLTIP_MAX | ||
return buildTooltip(item, { max = | return buildTooltip(item, { max = max }) | ||
end | end | ||
-- ========================= | -- ========================= | ||
-- Zentrale Dispatch-Funktion | -- Zentrale Dispatch-Funktion | ||
-- ========================= | -- ========================= | ||
function p.main(frame) | function p.main(frame) | ||
local args = frame.args or {} | local args = frame.args or {} | ||
local method = (args[1] or ''):lower() | local method = (args[1] or ''):lower() | ||
local key = args[2] | local key = args[2] | ||
local max = args.max or args | local arg3 = args[3] | ||
local arg4 = args[4] | |||
local max = tonumber(args.max or args.len or args.limit) or DEFAULT_TOOLTIP_MAX | |||
local item = getItem(key) | local item = getItem(key) | ||
local useTooltip = false | |||
local amount | |||
if method == 'img' then | |||
if arg3 then | |||
if isTooltipFlag(arg3) then | |||
useTooltip = true | |||
elseif tonumber(arg3) then | |||
amount = tonumber(arg3) | |||
end | |||
end | |||
if arg4 then | |||
if isTooltipFlag(arg4) then | |||
useTooltip = true | |||
elseif (not amount) and tonumber(arg4) then | |||
amount = tonumber(arg4) | |||
end | |||
end | |||
else | |||
if isTooltipFlag(arg3) or isTooltipFlag(arg4) then | |||
useTooltip = true | |||
end | |||
end | |||
if method == 'title' then | if method == 'title' then | ||
local label = getTitle(item) | |||
if useTooltip then | |||
return buildTooltip(item, { label = label, max = max }) | |||
end | |||
return | return label | ||
elseif method == 'link' then | elseif method == 'link' then | ||
return | local target = getLinkTarget(item) | ||
elseif method == 'img' | local label = '[[' .. target .. '|' .. getTitle(item) .. ']]' | ||
if useTooltip then | |||
return buildTooltip(item, { label = label, max = max }) | |||
end | |||
return label | |||
elseif method == 'img' then | |||
local img = getImage(item) | |||
local target = getLinkTarget(item) | |||
local title = getTitle(item) | |||
local parts = {} | |||
if amount then | |||
table.insert(parts, tostring(amount) .. '× ') | |||
end | |||
table.insert(parts, '[[Datei:' .. img .. '.png|24x24px|class=inline-icon|link=' .. target .. ']] ') | |||
table.insert(parts, '[[' .. target .. '|' .. title .. ']]') | |||
local label = table.concat(parts) | |||
if useTooltip then | |||
return buildTooltip(item, { label = label, max = max }) | |||
end | |||
return label | |||
elseif method == 'type' then | elseif method == 'type' then | ||
return | local label = getType(item) | ||
if useTooltip then | |||
return buildTooltip(item, { label = label, max = max }) | |||
end | |||
return label | |||
elseif method == 'desc' then | |||
local label = item.describe or item.description or '-' | |||
if useTooltip then | |||
return buildTooltip(item, { label = label, max = max }) | |||
end | |||
return label | |||
elseif method == 'tooltip' or method == '' then | elseif method == 'tooltip' or method == '' then | ||
return buildTooltip(item, { max = | return buildTooltip(item, { max = max }) | ||
end | end | ||
return 'ERROR: unknown Item method "' .. tostring(method) .. '"' | |||
end | end | ||
return p | return p | ||
Version vom 29. November 2025, 11:35 Uhr
Die Dokumentation für dieses Modul kann unter Modul:Item/Doku erstellt werden
-- Modul:Item – Firestone-Version
--
-- Methoden über Vorlage:Item:
-- {{Item|title|key[|1]}}
-- {{Item|img|key[|amount][|1]}}
-- {{Item|link|key[|1]}}
-- {{Item|type|key[|1]}}
-- {{Item|desc|key[|1]}}
--
-- 3./4. Parameter:
-- * Zahl → amount (nur bei img relevant)
-- * "1"/"tooltip"/"tt"/"hover"/"true" → Tooltip einschalten
--
-- Datengrundlage: [[Modul:Item/Data]]
-- Typgruppen: [[Modul:Item/Types]] + [[Modul:Item/Type]]
local p = {}
local DATA = mw.loadData('Modul:Item/Data')
local ItemType = require('Modul:Item/Type')
local ustr = mw.ustring
local text = mw.text
-- =========================
-- Konfiguration
-- =========================
local DEFAULT_404_IMG = '404'
local DEFAULT_404_LINK = 'Modul:Item/Data'
local DEFAULT_404_TYPE = 'unknown'
local DEFAULT_TOOLTIP_MAX = 140
local TYPE_LABELS = {
currencies = 'Währung',
unknown = 'Unbekannt',
}
-- =========================
-- Helpers
-- =========================
local function normalizeKey(name)
if not name then
return nil
end
local s = ustr.lower(tostring(name))
s = s:gsub('%s+', '')
return s
end
local function clone(tbl)
local copy = {}
for k, v in pairs(tbl or {}) do
copy[k] = v
end
return copy
end
local function object404(name)
name = tostring(name or '?')
return {
key = '404',
title = 'ERROR: ' .. name,
link = DEFAULT_404_LINK,
img = DEFAULT_404_IMG,
type = DEFAULT_404_TYPE,
describe = '',
}
end
local function getItem(raw)
local key = normalizeKey(raw or '')
if not key or key == '' then
return object404(raw)
end
local base = DATA[key]
if not base then
return object404(key)
end
local item = clone(base)
item.key = key
return item
end
local function capitalize(str)
if not str then
return ''
end
local s = string.lower(tostring(str))
return string.gsub(' ' .. s, '%W%l', string.upper):sub(2)
end
local function title2link(title)
return (tostring(title or '')):gsub('%s', '_')
end
local function getTitle(item)
if item.title and item.title ~= '' then
return item.title
end
return capitalize(item.key)
end
local function imgPngExists(imgname)
if type(imgname) == 'string' and #imgname ~= 0 then
local t = mw.title.new('Datei:' .. imgname .. '.png')
return t and t.file and t.file.exists or false
end
return false
end
local function getImage(item)
if item.img and imgPngExists(item.img) then
return item.img
end
local guess = title2link(getTitle(item))
if imgPngExists(guess) then
return guess
end
return object404(item.key).img
end
local function getLinkTarget(item)
if item.link and item.link ~= '' then
return item.link
end
return title2link(getTitle(item))
end
local function getType(item)
local group = ItemType.typenameForKey(item.key)
local label = TYPE_LABELS[group]
if label and label ~= '' then
return label
end
return group or DEFAULT_404_TYPE
end
-- ---------- Markup → Plaintext (für Tooltip) ----------
local function stripMarkup(str)
if not str or str == '' then
return ''
end
str = tostring(str)
-- interne Links [[Seite|Label]] / [[Seite]]
str = str:gsub('%[%[([^%]|]+)|([^%]]+)%]%]', '%2')
str = str:gsub('%[%[([^%]]+)%]%]', '%1')
-- externe Links [url Label]
str = str:gsub('%[[^%s%]]+%s+([^%]]+)%]', '%1')
-- Hervorhebungen
str = str:gsub("''+", '')
-- Templates {{...}}
str = str:gsub('{{.-}}', '')
-- HTML-Tags
str = str:gsub('<.->', '')
-- Whitespace
str = str:gsub('%s+', ' ')
if text and text.trim then
str = text.trim(str)
end
return str
end
local function shorten(str, max)
str = stripMarkup(str)
max = tonumber(max) or 0
if max <= 0 or ustr.len(str) <= max then
return str
end
local cut = ustr.sub(str, 1, max)
local lastSpace = ustr.find(cut, ' [^ ]*$')
if lastSpace and lastSpace > max * 0.6 then
cut = ustr.sub(cut, 1, lastSpace - 1)
end
return cut .. '…'
end
-- ---------- Tooltip ----------
local function buildTooltip(item, opts)
opts = opts or {}
local max = opts.max or DEFAULT_TOOLTIP_MAX
local label = opts.label
local title = getTitle(item)
local desc = item.describe or item.description or ''
local shortDesc = shorten(desc, max)
local img = getImage(item)
local parts = {}
-- Wrapper
table.insert(parts, '<span class="fs-item">')
-- sichtbarer Inhalt
if label and label ~= '' then
table.insert(parts, label)
else
local target = getLinkTarget(item)
table.insert(parts, '[[' .. target .. '|' .. title .. ']]')
end
-- Tooltip-Box
table.insert(parts, '<span class="tooltip">')
-- Header: Icon + Titel
table.insert(parts, '<span class="header">')
if img and img ~= '' then
table.insert(parts, '[[Datei:' .. img .. '.png|24x24px|class=icon]] ')
end
table.insert(parts, '<span class="title">')
table.insert(parts, title)
table.insert(parts, '</span></span>') -- title + header
-- Text
if shortDesc ~= '' then
table.insert(parts, '<span class="body">')
table.insert(parts, shortDesc)
table.insert(parts, '</span>')
end
table.insert(parts, '</span>') -- tooltip
table.insert(parts, '</span>') -- wrapper
return table.concat(parts)
end
-- ---------- Frame-Args ----------
local function extractKey(args)
if not args then
return nil
end
return args[1] or args.name or args.key or args.id or args.nameid
end
local function getArgKey(frame)
if not frame then
return nil, {}
end
local args = frame.args or {}
local key = extractKey(args)
if key and key ~= '' then
return key, args
end
if frame.getParent then
local parent = frame:getParent()
if parent and parent.args then
args = parent.args
key = extractKey(args)
if key and key ~= '' then
return key, args
end
end
end
return nil, args
end
local function isTooltipFlag(v)
if not v then
return false
end
local s = tostring(v):lower()
return s == '1' or s == 'true' or s == 'yes'
or s == 'y' or s == 'tooltip' or s == 'tt' or s == 'hover'
end
-- =========================
-- optionale direkte #invoke-Funktionen
-- =========================
function p.title(frame)
local key = select(1, getArgKey(frame))
local item = getItem(key)
return getTitle(item)
end
function p.img(frame)
local key = select(1, getArgKey(frame))
local item = getItem(key)
return getImage(item)
end
function p.link(frame)
local key = select(1, getArgKey(frame))
local item = getItem(key)
local target = getLinkTarget(item)
return '[[' .. target .. '|' .. getTitle(item) .. ']]'
end
function p.type(frame)
local key = select(1, getArgKey(frame))
local item = getItem(key)
return getType(item)
end
function p.desc(frame)
local key = select(1, getArgKey(frame))
local item = getItem(key)
return item.describe or item.description or '-'
end
function p.tooltip(frame)
local key, args = getArgKey(frame)
local item = getItem(key)
local max = tonumber(args.max or args.len or args.limit) or DEFAULT_TOOLTIP_MAX
return buildTooltip(item, { max = max })
end
-- =========================
-- Zentrale Dispatch-Funktion
-- =========================
function p.main(frame)
local args = frame.args or {}
local method = (args[1] or ''):lower()
local key = args[2]
local arg3 = args[3]
local arg4 = args[4]
local max = tonumber(args.max or args.len or args.limit) or DEFAULT_TOOLTIP_MAX
local item = getItem(key)
local useTooltip = false
local amount
if method == 'img' then
if arg3 then
if isTooltipFlag(arg3) then
useTooltip = true
elseif tonumber(arg3) then
amount = tonumber(arg3)
end
end
if arg4 then
if isTooltipFlag(arg4) then
useTooltip = true
elseif (not amount) and tonumber(arg4) then
amount = tonumber(arg4)
end
end
else
if isTooltipFlag(arg3) or isTooltipFlag(arg4) then
useTooltip = true
end
end
if method == 'title' then
local label = getTitle(item)
if useTooltip then
return buildTooltip(item, { label = label, max = max })
end
return label
elseif method == 'link' then
local target = getLinkTarget(item)
local label = '[[' .. target .. '|' .. getTitle(item) .. ']]'
if useTooltip then
return buildTooltip(item, { label = label, max = max })
end
return label
elseif method == 'img' then
local img = getImage(item)
local target = getLinkTarget(item)
local title = getTitle(item)
local parts = {}
if amount then
table.insert(parts, tostring(amount) .. '× ')
end
table.insert(parts, '[[Datei:' .. img .. '.png|24x24px|class=inline-icon|link=' .. target .. ']] ')
table.insert(parts, '[[' .. target .. '|' .. title .. ']]')
local label = table.concat(parts)
if useTooltip then
return buildTooltip(item, { label = label, max = max })
end
return label
elseif method == 'type' then
local label = getType(item)
if useTooltip then
return buildTooltip(item, { label = label, max = max })
end
return label
elseif method == 'desc' then
local label = item.describe or item.description or '-'
if useTooltip then
return buildTooltip(item, { label = label, max = max })
end
return label
elseif method == 'tooltip' or method == '' then
return buildTooltip(item, { max = max })
end
return 'ERROR: unknown Item method "' .. tostring(method) .. '"'
end
return p