Zum Inhalt springen

Modul:Item: Unterschied zwischen den Versionen

Aus Firestone Idle Rpg Wiki
Keine Bearbeitungszusammenfassung
Keine Bearbeitungszusammenfassung
Zeile 5: Zeile 5:
--  #invoke:Item|short|key      -> Kurzname oder Titel
--  #invoke:Item|short|key      -> Kurzname oder Titel
--  #invoke:Item|desc|key        -> Beschreibung (describe/description)
--  #invoke:Item|desc|key        -> Beschreibung (describe/description)
--  #invoke:Item|type|key        -> Deutscher Typ (z.B. "Währung")
--  #invoke:Item|img|key        -> Bild-Basename oder '' (wenn kein Bild)
--  #invoke:Item|img|key        -> Bild-Basename ohne "Datei:" / ".png"
--  #invoke:Item|link|key        -> Seitentitel, z.B. "Währung#Gold" oder "Blob"
--  #invoke:Item|link|key        -> Seitentitel, z.B. "Währung#Gold"
--  #invoke:Item|tooltip|key|... -> fertige Tooltip-Ausgabe (Label über |label=)
--  #invoke:Item|tooltip|key|... -> fertige Tooltip-Ausgabe (Label über |label=)
--
--
-- Datengrundlage: [[Modul:Item/Data]]
-- Datengrundlage: [[Modul:Item/Data]]
-- Typgruppen:    [[Modul:Item/Types]] + [[Modul:Item/Type]]


local p       = {}
local p   = {}
local DATA     = mw.loadData('Modul:Item/Data')
local DATA = mw.loadData( 'Modul:Item/Data' )
local ItemType = require('Modul:Item/Type')


local ustr = mw.ustring
local ustr = mw.ustring
Zeile 24: Zeile 21:
-- =========================
-- =========================


local DEFAULT_404_IMG    = '404'
local DEFAULT_404_LINK    = 'Modul:Item/Data'
local DEFAULT_404_LINK    = 'Modul:Item/Data'
local DEFAULT_404_TYPE    = 'unknown'
local DEFAULT_TOOLTIP_MAX = 140
local DEFAULT_TOOLTIP_MAX = 140
-- interne Typen -> deutsche Labels
local TYPE_LABELS = {
    currencies = 'Währung',
    unknown    = 'Unbekannt',
}


-- =========================
-- =========================
Zeile 39: Zeile 28:
-- =========================
-- =========================


local function normalizeKey(name)
local function normalizeKey( name )
     if not name then
     if not name then
         return nil
         return nil
     end
     end
     local s = ustr.lower(tostring(name))
     local s = ustr.lower( tostring( name ) )
     s = s:gsub('%s+', '')
     s = s:gsub( '%s+', '' )
     return s
     return s
end
end


local function clone(tbl)
local function clone( tbl )
     local copy = {}
     local copy = {}
     for k, v in pairs(tbl or {}) do
     for k, v in pairs( tbl or {} ) do
         copy[k] = v
         copy[k] = v
     end
     end
Zeile 56: Zeile 45:
end
end


local function object404(name)
local function capitalize( str )
    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
     if not str then
         return ''
         return ''
     end
     end
     local s = string.lower(tostring(str))
     local s = string.lower( tostring( str ) )
     return string.gsub(' ' .. s, '%W%l', string.upper):sub(2)
     return string.gsub( ' ' .. s, '%W%l', string.upper ):sub( 2 )
end
end


local function title2link(title)
local function title2link( title )
     return (tostring(title or '')):gsub('%s', '_')
     return ( tostring( title or '' ) ):gsub( '%s', '_' )
end
end


local function getTitle(item)
local function getTitle( item )
     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 )
end
end


local function getShort(item)
local function getShort( item )
     if item.short and item.short ~= '' then
     if item.short and item.short ~= '' then
         return item.short
         return item.short
     end
     end
     return getTitle(item)
     return getTitle( item )
end
end


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
     end
     end
Zeile 118: Zeile 79:
end
end


local function getImage(item)
-- Fallback-Objekt nur für "kaputte" Aufrufe (kein Key o.ä.)
     if item.img and imgPngExists(item.img) then
local function object404( raw )
         return item.img
     raw = tostring( raw or '?' )
    return {
        key      = '404',
        title    = 'ERROR: ' .. raw,
        link    = DEFAULT_404_LINK,
        img     = nil,  -- kein 404-Icon mehr
        describe = '',
    }
end
 
-- Holt Item aus DATA oder baut ein "virtuelles" Item,
-- wenn kein Eintrag existiert (z.B. {{Icon|blob}}).
local function getItem( raw )
    local key = normalizeKey( raw or '' )
    if not key or key == '' then
         return object404( raw )
     end
     end


     local guess = title2link(getTitle(item))
     local base = DATA[key]
     if imgPngExists(guess) then
     if base then
         return guess
        local item = clone( base )
        item.key = key
         return item
     end
     end


     return object404(item.key).img
    -- virtuelles Item: nur Titel/Link aus dem Key ableiten, kein Bild
    local title = capitalize( key )
     return {
        key      = key,
        title    = title,
        link    = title2link( title ),
        img     = nil,
        describe = '',
    }
end
end


local function getLinkTarget(item)
-- Link-Ziel für [[…]]
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 title2link( getTitle( item ) )
end
end


local function getType(item)
-- Liefert Bild-Basename oder '' wenn keins gefunden wird.
     local group = ItemType.typenameForKey(item.key)
local function getImage( item )
     local label = TYPE_LABELS[group]
     -- explizit in Data definiert?
     if label and label ~= '' then
    if item.img and imgPngExists( item.img ) then
         return label
        return item.img
    end
 
    -- Versuch: aus Titel raten (z.B. Gold -> Gold.png)
     local guess = title2link( getTitle( item ) )
     if imgPngExists( guess ) then
         return guess
     end
     end
     return group or DEFAULT_404_TYPE
 
    -- kein Bild → leer zurückgeben, KEIN 404
     return ''
end
end


-- ---------- Markup → Plaintext (für Tooltip) ----------
-- ---------- Markup → Plaintext (für Tooltip) ----------


local function stripMarkup(str)
local function stripMarkup( str )
     if not str or str == '' then
     if not str or str == '' then
         return ''
         return ''
     end
     end
     str = tostring(str)
     str = tostring( str )


     -- interne Links [[Seite|Label]] / [[Seite]]
     -- interne Links [[Seite|Label]] / [[Seite]]
     str = str:gsub('%[%[([^%]|]+)|([^%]]+)%]%]', '%2')
     str = str:gsub( '%[%[([^%]|]+)|([^%]]+)%]%]', '%2' )
     str = str:gsub('%[%[([^%]]+)%]%]', '%1')
     str = str:gsub( '%[%[([^%]]+)%]%]', '%1' )


     -- externe Links [url Label]
     -- 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
         str = text.trim(str)
         str = text.trim( str )
     end
     end


Zeile 180: Zeile 175:
end
end


local function shorten(str, max)
local function shorten( str, max )
     str = stripMarkup(str)
     str = stripMarkup( str )
     max = tonumber(max) or 0
     max = tonumber( max ) or 0


     if max <= 0 or ustr.len(str) <= max then
     if max <= 0 or ustr.len( str ) <= max then
         return str
         return str
     end
     end


     local cut = ustr.sub(str, 1, max)
     local cut = ustr.sub( str, 1, max )
     local lastSpace = ustr.find(cut, ' [^ ]*$')
     local lastSpace = ustr.find( cut, ' [^ ]*$' )
     if lastSpace and lastSpace > max * 0.6 then
     if lastSpace and lastSpace > max * 0.6 then
         cut = ustr.sub(cut, 1, lastSpace - 1)
         cut = ustr.sub( cut, 1, lastSpace - 1 )
     end
     end


Zeile 199: Zeile 194:
-- ---------- 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 label = opts.label


     local title    = getTitle(item)
     local title    = getTitle( item )
     local desc      = item.describe or item.description or ''
     local desc      = item.describe or item.description or ''
     local shortDesc = shorten(desc, max)
     local shortDesc = shorten( desc, max )
     local img      = getImage(item)
     local img      = getImage( item )


     local out = {}
     local out = {}


     table.insert(out, '<span class="fs-item">')
     table.insert( out, '<span class="fs-tip">' )


     -- sichtbares Label
     -- sichtbares Label
     if label and label ~= '' then
     if label and label ~= '' then
         table.insert(out, label)
         table.insert( out, label )
     else
     else
         local target = getLinkTarget(item)
         local target = getLinkTarget( item )
         table.insert(out, '[[' .. target .. '|' .. title .. ']]')
         table.insert( out, '[[' .. target .. '|' .. title .. ']]' )
     end
     end


     table.insert(out, '<span class="tooltip">')
     table.insert( out, '<span class="content">' )
 
table.insert( out, '<span class="box">' )
     table.insert(out, '<span class="header">')
     table.insert( out, '<span class="text">' )
     if img and img ~= '' then
     if img and img ~= '' then
         table.insert(out, '[[Datei:' .. img .. '.png|24x24px|class=icon]] ')
         table.insert( out, '[[Datei:' .. img .. '.png|48x48px]] ' )
     end
     end
     table.insert(out, '<span class="title">')
     table.insert( out, '<span class="title">' )
     table.insert(out, title)
     table.insert( out, title )
     table.insert(out, '</span></span>') -- title + header
     table.insert( out, '</span></span>' ) -- title + header


     if shortDesc ~= '' then
     if shortDesc ~= '' then
         table.insert(out, '<span class="body">')
         table.insert( out, '<span class="desc">' )
         table.insert(out, shortDesc)
         table.insert( out, shortDesc )
         table.insert(out, '</span>')
         table.insert( out, '</span>' )
     end
     end


     table.insert(out, '</span>') -- tooltip
     table.insert( out, '</span></span>' ) -- tooltip
     table.insert(out, '</span>') -- fs-item
     table.insert( out, '</span>' ) -- fs-item


     return table.concat(out)
     return table.concat( out )
end
end


-- ---------- Frame-Args ----------
-- ---------- Frame-Args ----------


local function extractKey(args)
local function extractKey( args )
     if not args then
     if not args then
         return nil
         return nil
Zeile 252: Zeile 247:
end
end


local function getArgKey(frame)
local function getArgKey( frame )
     if not frame then
     if not frame then
         return nil, {}
         return nil, {}
Zeile 258: Zeile 253:


     local args = frame.args or {}
     local args = frame.args or {}
     local key  = extractKey(args)
     local key  = extractKey( args )
     if key and key ~= '' then
     if key and key ~= '' then
         return key, args
         return key, args
Zeile 267: Zeile 262:
         if parent and parent.args then
         if parent and parent.args then
             args = parent.args
             args = parent.args
             key  = extractKey(args)
             key  = extractKey( args )
             if key and key ~= '' then
             if key and key ~= '' then
                 return key, args
                 return key, args
Zeile 281: Zeile 276:
-- =========================
-- =========================


function p.title(frame)
function p.title( frame )
     local key = select(1, getArgKey(frame))
     local key = select( 1, getArgKey( frame ) )
     local item = getItem(key)
     local item = getItem( key )
     return getTitle(item)
     return getTitle( item )
end
end


function p.short(frame)
function p.short( frame )
     local key = select(1, getArgKey(frame))
     local key = select( 1, getArgKey( frame ) )
     local item = getItem(key)
     local item = getItem( key )
     return getShort(item)
     return getShort( item )
end
end


function p.desc(frame)
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 item.describe or item.description or '-'
     return item.describe or item.description or '-'
end
end


function p.type(frame)
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 getType(item)
     -- Basename oder '' zurückgeben
end
     return getImage( item ) or ''
 
function p.img(frame)
     local key = select(1, getArgKey(frame))
     local item = getItem(key)
     -- Nur der Basename; .png + Datei: kommen in der Vorlage
     return getImage(item)
end
end


function p.link(frame)
function p.link( frame )
     local key = select(1, getArgKey(frame))
     local key = select( 1, getArgKey( frame ) )
     local item = getItem(key)
     local item = getItem( key )
     -- Nur das Seitentarget, z.B. "Währung#Gold"
     -- Nur das Seitentarget, z.B. "Währung#Gold" oder "Blob"
     return getLinkTarget(item)
     return getLinkTarget( item )
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   = tonumber(args.max or args.len or args.limit) or DEFAULT_TOOLTIP_MAX
     local max       = tonumber( args.max or args.len or args.limit ) or DEFAULT_TOOLTIP_MAX
     local label = args.label
     local label     = args.label
     return buildTooltip(item, { max = max, label = label })
     return buildTooltip( item, { max = max, label = label } )
end
end


return p
return p

Version vom 1. Dezember 2025, 18:41 Uhr

Die Dokumentation für dieses Modul kann unter Modul:Item/Doku erstellt werden

-- Modul:Item – Firestone-Version
--
-- Bietet (für andere Vorlagen):
--   #invoke:Item|title|key       -> "Gold"
--   #invoke:Item|short|key       -> Kurzname oder Titel
--   #invoke:Item|desc|key        -> Beschreibung (describe/description)
--   #invoke:Item|img|key         -> Bild-Basename oder '' (wenn kein Bild)
--   #invoke:Item|link|key        -> Seitentitel, z.B. "Währung#Gold" oder "Blob"
--   #invoke:Item|tooltip|key|... -> fertige Tooltip-Ausgabe (Label über |label=)
--
-- Datengrundlage: [[Modul:Item/Data]]

local p    = {}
local DATA = mw.loadData( 'Modul:Item/Data' )

local ustr = mw.ustring
local text = mw.text

-- =========================
-- Konfiguration
-- =========================

local DEFAULT_404_LINK    = 'Modul:Item/Data'
local DEFAULT_TOOLTIP_MAX = 140

-- =========================
-- 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 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 getShort( item )
    if item.short and item.short ~= '' then
        return item.short
    end
    return getTitle( item )
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

-- Fallback-Objekt nur für "kaputte" Aufrufe (kein Key o.ä.)
local function object404( raw )
    raw = tostring( raw or '?' )
    return {
        key      = '404',
        title    = 'ERROR: ' .. raw,
        link     = DEFAULT_404_LINK,
        img      = nil,   -- kein 404-Icon mehr
        describe = '',
    }
end

-- Holt Item aus DATA oder baut ein "virtuelles" Item,
-- wenn kein Eintrag existiert (z.B. {{Icon|blob}}).
local function getItem( raw )
    local key = normalizeKey( raw or '' )
    if not key or key == '' then
        return object404( raw )
    end

    local base = DATA[key]
    if base then
        local item = clone( base )
        item.key = key
        return item
    end

    -- virtuelles Item: nur Titel/Link aus dem Key ableiten, kein Bild
    local title = capitalize( key )
    return {
        key      = key,
        title    = title,
        link     = title2link( title ),
        img      = nil,
        describe = '',
    }
end

-- Link-Ziel für [[…]]
local function getLinkTarget( item )
    if item.link and item.link ~= '' then
        return item.link
    end
    return title2link( getTitle( item ) )
end

-- Liefert Bild-Basename oder '' wenn keins gefunden wird.
local function getImage( item )
    -- explizit in Data definiert?
    if item.img and imgPngExists( item.img ) then
        return item.img
    end

    -- Versuch: aus Titel raten (z.B. Gold -> Gold.png)
    local guess = title2link( getTitle( item ) )
    if imgPngExists( guess ) then
        return guess
    end

    -- kein Bild → leer zurückgeben, KEIN 404
    return ''
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 out = {}

    table.insert( out, '<span class="fs-tip">' )

    -- sichtbares Label
    if label and label ~= '' then
        table.insert( out, label )
    else
        local target = getLinkTarget( item )
        table.insert( out, '[[' .. target .. '|' .. title .. ']]' )
    end

    table.insert( out, '<span class="content">' )
	table.insert( out, '<span class="box">' )
    table.insert( out, '<span class="text">' )
    if img and img ~= '' then
        table.insert( out, '[[Datei:' .. img .. '.png|48x48px]] ' )
    end
    table.insert( out, '<span class="title">' )
    table.insert( out, title )
    table.insert( out, '</span></span>' ) -- title + header

    if shortDesc ~= '' then
        table.insert( out, '<span class="desc">' )
        table.insert( out, shortDesc )
        table.insert( out, '</span>' )
    end

    table.insert( out, '</span></span>' ) -- tooltip
    table.insert( out, '</span>' ) -- fs-item

    return table.concat( out )
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

-- =========================
-- Öffentliche Funktionen
-- =========================

function p.title( frame )
    local key  = select( 1, getArgKey( frame ) )
    local item = getItem( key )
    return getTitle( item )
end

function p.short( frame )
    local key  = select( 1, getArgKey( frame ) )
    local item = getItem( key )
    return getShort( 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.img( frame )
    local key  = select( 1, getArgKey( frame ) )
    local item = getItem( key )
    -- Basename oder '' zurückgeben
    return getImage( item ) or ''
end

function p.link( frame )
    local key  = select( 1, getArgKey( frame ) )
    local item = getItem( key )
    -- Nur das Seitentarget, z.B. "Währung#Gold" oder "Blob"
    return getLinkTarget( item )
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
    local label     = args.label
    return buildTooltip( item, { max = max, label = label } )
end

return p