Jump to content

Module:OOOInfoBox: Difference between revisions

From Out of Ore Wiki
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local DOCS_TITLE = 'Template:Docs.json'


-- Load the JSON docs file
-- Try JSON first (Template:Docs.json), then Lua table (Module:DocsJSON)
local function load_docs()
local function load_docs()
     local t = mw.title.new(DOCS_TITLE)
    -- Preferred: JSON page with JSON content model
     if not t then return {} end
    local ok, data = pcall(mw.loadJsonData, 'Template:Docs.json')
    local content = t:getContent() or ''
    if ok and type(data) == 'table' then
    if content == '' then return {} end
        return data, 'Template:Docs.json (loadJsonData)'
    local ok, data = pcall(mw.text.jsonDecode, content)
    end
     return (ok and type(data) == 'table') and data or {}
 
    -- Fallback: decode raw content if page exists but not JSON model
     local t = mw.title.new('Template:Docs.json')
     if t then
        local content = t:getContent()
        if content and content ~= '' then
            local ok2, tbl = pcall(mw.text.jsonDecode, content)
            if ok2 and type(tbl) == 'table' then
                return tbl, 'Template:Docs.json (jsonDecode)'
            end
        end
     end
 
    -- Last resort: Lua table module
    local ok3, tbl2 = pcall(require, 'Module:DocsJSON')
    if ok3 and type(tbl2) == 'table' then
        return tbl2, 'Module:DocsJSON (require)'
    end
 
    return {}, '(no source found)'
end
end


local DOCS = load_docs()
local DOCS, SOURCE = load_docs()
 
local function trim(s)
    return s and mw.text.trim(tostring(s)) or s
end


-- Small helper
local function pick(args, data, key)
local function pick(args, data, key)
     local v = args[key]
     local v = args[key]
     if v and v ~= '' then return v end
     if v and trim(v) ~= '' then return trim(v) end
     v = data[key]
     v = data[key]
     if v ~= nil and v ~= '' then return v end
     if v ~= nil and tostring(v) ~= '' then return v end
end
 
local function warn(msg)
    return '<div class="mw-message-box mw-message-box-warning"><b>OOOInfoBox</b>: '..msg..'</div>'
end
end


function p.building(frame)
function p.building(frame)
     local args = frame:getParent().args or {}
     local args = frame:getParent().args or {}
     local art = args.art or args.artNumber or args.id
     local art = trim(args.art or args.artNumber or args.id)
     local data = art and DOCS[art]
    if not art or art == '' then
        return warn('Use |art=ART_NUMBER (e.g. 400113).')
    end
 
    if type(DOCS) ~= 'table' or next(DOCS) == nil then
        return warn('Could not load data. Tried: '..(SOURCE or '(unknown)')..'.')
    end
 
        -- JSON keys are strings; be forgiving with numeric inputs
     local data = DOCS[art] or DOCS[tostring(tonumber(art) or art)]
     if not data then
     if not data then
         return '⚠️ Use |art=ART_NUMBER (e.g. 400113) and ensure Data:Docs.json has that key.'
         return warn(('Key "%s" not found in %s'):format(art, SOURCE or '(unknown)'))
     end
     end


     local box = mw.html.create('table'):addClass('infobox')
     local box = mw.html.create('table'):addClass('infobox')


    -- Title
     local title = pick(args, data, 'displayName') or ('Item '..art)
     local title = pick(args, data, 'displayName') or ('Item ' .. art)
     box:tag('tr'):tag('th'):attr('colspan','2'):wikitext(title)
     box:tag('tr'):tag('th'):attr('colspan','2'):wikitext(title)


Line 51: Line 84:
     row('Capacity', 'capacity')
     row('Capacity', 'capacity')


    box:wikitext('<!-- OOOInfoBox source: '..(SOURCE or 'unknown')..' -->')
     return tostring(box)
     return tostring(box)
end
end


return p
return p

Revision as of 16:16, 6 October 2025

Documentation for this module may be created at Module:OOOInfoBox/doc

local p = {}

-- Try JSON first (Template:Docs.json), then Lua table (Module:DocsJSON)
	local function load_docs()
    -- Preferred: JSON page with JSON content model
    local ok, data = pcall(mw.loadJsonData, 'Template:Docs.json')
    if ok and type(data) == 'table' then
        return data, 'Template:Docs.json (loadJsonData)'
    end

    -- Fallback: decode raw content if page exists but not JSON model
    local t = mw.title.new('Template:Docs.json')
    if t then
        local content = t:getContent()
        if content and content ~= '' then
            local ok2, tbl = pcall(mw.text.jsonDecode, content)
            if ok2 and type(tbl) == 'table' then
                return tbl, 'Template:Docs.json (jsonDecode)'
            end
        end
    end

    -- Last resort: Lua table module
    local ok3, tbl2 = pcall(require, 'Module:DocsJSON')
    if ok3 and type(tbl2) == 'table' then
        return tbl2, 'Module:DocsJSON (require)'
    end

    return {}, '(no source found)'
end

local DOCS, SOURCE = load_docs()

local function trim(s)
    return s and mw.text.trim(tostring(s)) or s
end

local function pick(args, data, key)
    local v = args[key]
    if v and trim(v) ~= '' then return trim(v) end
    v = data[key]
    if v ~= nil and tostring(v) ~= '' then return v end
end

local function warn(msg)
    return '<div class="mw-message-box mw-message-box-warning"><b>OOOInfoBox</b>: '..msg..'</div>'
end

function p.building(frame)
    local args = frame:getParent().args or {}
    local art = trim(args.art or args.artNumber or args.id)
    if not art or art == '' then
        return warn('Use |art=ART_NUMBER (e.g. 400113).')
    end

    if type(DOCS) ~= 'table' or next(DOCS) == nil then
        return warn('Could not load data. Tried: '..(SOURCE or '(unknown)')..'.')
    end

        -- JSON keys are strings; be forgiving with numeric inputs
    local data = DOCS[art] or DOCS[tostring(tonumber(art) or art)]
    if not data then
        return warn(('Key "%s" not found in %s'):format(art, SOURCE or '(unknown)'))
    end

    local box = mw.html.create('table'):addClass('infobox')

    local title = pick(args, data, 'displayName') or ('Item '..art)
    box:tag('tr'):tag('th'):attr('colspan','2'):wikitext(title)

    local function row(label, key, fixed)
        local v = fixed or pick(args, data, key)
        if v ~= nil and tostring(v) ~= '' then
            local tr = box:tag('tr')
            tr:tag('th'):wikitext(label)
            tr:tag('td'):wikitext(v)
        end
    end

    row('Art Number', nil, art)
    row('Type', 'type')
    row('Throughput (m³/h)', 'throughput_m3h')
    row('Power (kW)', 'power_kw')
    row('Capacity', 'capacity')

    box:wikitext('<!-- OOOInfoBox source: '..(SOURCE or 'unknown')..' -->')
    return tostring(box)
end


return p