|
|
Line 1: |
Line 1: |
| | local DATA = require('Module:OOO/Data') |
| local p = {} | | local p = {} |
|
| |
| -- Try JSON first (Template:Docs.json), then Lua table (Module:DocsJSON)
| |
| local function load_docs()
| |
| local ok, data = pcall(mw.loadJsonData, 'Template:Docs.json')
| |
| if ok and type(data) == 'table' then
| |
| return data, 'Template:Docs.json (loadJsonData)'
| |
| end
| |
|
| |
| 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
| |
|
| |
| 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 trim(s) return s and mw.text.trim(tostring(s)) or s end |
| local function pick(args, data, key) | | local function pick(args, data, key) |
| local v = args[key]; if v and trim(v) ~= '' then return trim(v) end
| | 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
| | v = data and data[key]; if v ~= nil and tostring(v) ~= '' then return v end |
| end | | end |
|
| |
|
| local function warn(msg) | | local function warn(msg) |
| return '<div class="mw-message-box mw-message-box-warning"><b>OOOInfoBox</b>: '..msg..'</div>'
| | return '<div class="mw-message-box mw-message-box-warning"><b>OOOInfoBox</b>: '..msg..'</div>' |
| end | | end |
|
| |
|
| -- more forgiving key lookup + debug
| | function p.building(frame) |
| local function find_record(art)
| | local parent = frame:getParent() or frame |
| -- direct lookups
| | local args = parent.args or {} |
| local rec = DOCS[art] or DOCS[tostring(tonumber(art) or art)]
| |
| if rec then return rec end
| |
| | |
| -- trim/loose match on keys (handles stray spaces/non-visible chars)
| |
| local art_trim = trim(art)
| |
| for k, v in pairs(DOCS) do
| |
| if trim(tostring(k)) == art_trim then
| |
| return v
| |
| end
| |
| end
| |
| return nil
| |
| end
| |
|
| |
|
| function p.building(frame)
| | -- Required inputs |
| local args = frame:getParent().args or {}
| | local domain = trim(args.type or args.domain or 'Buildings') -- default to Buildings for convenience |
| local art = trim(args.art or args.artNumber or args.id)
| | local art = trim(args.art or args.artNumber or args.id) |
| if not art or art == '' then
| | if not art or art == '' then |
| return warn('Use |art=ART_NUMBER (e.g. 400113).')
| | return warn('Use |art=ART_NUMBER (e.g. 400113).') |
| end
| | end |
| if type(DOCS) ~= 'table' or next(DOCS) == nil then
| |
| return warn('Could not load data. Tried: '..(SOURCE or '(unknown)')..'.')
| |
| end
| |
|
| |
|
| local data = find_record(art)
| | -- Lookup record |
| if not data then
| | local data = DATA.get(domain, art) |
| -- show a few keys to diagnose quickly
| | if not data then |
| local samples, count = {}, 0
| | return warn(('No record for %s:%s.'):format(domain, art)) |
| for k, _ in pairs(DOCS) do
| | end |
| count = count + 1
| |
| if #samples < 5 then table.insert(samples, '"'..tostring(k)..'"') end
| |
| end
| |
| local sample_txt = (#samples > 0) and table.concat(samples, ', ') or '(no keys)'
| |
| return warn(('Key "%s" not found in %s. Sample keys: %s (count: %d)')
| |
| :format(art, SOURCE or '(unknown)', sample_txt, count))
| |
| end
| |
|
| |
|
| local box = mw.html.create('table'):addClass('infobox')
| | -- Build table |
| local title = pick(args, data, 'displayName') or ('Item '..art)
| | local box = mw.html.create('table'):addClass('infobox') |
| box:tag('tr'):tag('th'):attr('colspan','2'):wikitext(title)
| | 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 function row(label, key, fixed) |
| local v = fixed or pick(args, data, key)
| | local v = fixed or pick(args, data, key) |
| if v ~= nil and tostring(v) ~= '' then
| | if v ~= nil and tostring(v) ~= '' then |
| local tr = box:tag('tr')
| | local tr = box:tag('tr') |
| tr:tag('th'):wikitext(label)
| | tr:tag('th'):wikitext(label) |
| tr:tag('td'):wikitext(v)
| | tr:tag('td'):wikitext(v) |
| end
| |
| end | | end |
| | end |
|
| |
|
| row('Art Number', nil, art)
| | row('Domain', nil, domain) |
| row('Type', 'type')
| | row('Art Number', nil, art) |
| row('Throughput (m³/h)', 'throughput_m3h')
| | row('Type', 'type') |
| row('Power (kW)', 'power_kw')
| | row('Throughput (m³/h)', 'throughput_m3h') |
| row('Capacity', 'capacity')
| | row('Power (kW)', 'power_kw') |
| | row('Capacity', 'capacity') |
|
| |
|
| box:wikitext('<!-- OOOInfoBox source: '..(SOURCE or 'unknown')..' -->')
| | return tostring(box) |
| return tostring(box)
| |
| end | | end |
|
| |
|
| return p | | return p |