Module:OOOInfoBox: Difference between revisions
No edit summary |
No edit summary |
||
Line 2: | Line 2: | ||
-- Try JSON first (Template:Docs.json), then Lua table (Module:DocsJSON) | -- Try JSON first (Template:Docs.json), then Lua table (Module:DocsJSON) | ||
local function load_docs() | |||
local ok, data = pcall(mw.loadJsonData, 'Template:Docs.json') | local ok, data = pcall(mw.loadJsonData, 'Template:Docs.json') | ||
if ok and type(data) == 'table' then | if ok and type(data) == 'table' then | ||
Line 9: | Line 8: | ||
end | end | ||
local t = mw.title.new('Template:Docs.json') | local t = mw.title.new('Template:Docs.json') | ||
if t then | if t then | ||
Line 21: | Line 19: | ||
end | end | ||
local ok3, tbl2 = pcall(require, 'Module:DocsJSON') | local ok3, tbl2 = pcall(require, 'Module:DocsJSON') | ||
if ok3 and type(tbl2) == 'table' then | if ok3 and type(tbl2) == 'table' then | ||
Line 32: | Line 29: | ||
local DOCS, SOURCE = load_docs() | local DOCS, SOURCE = load_docs() | ||
local function trim(s) | local function trim(s) return s and mw.text.trim(tostring(s)) or s end | ||
end | |||
local function pick(args, data, key) | local function pick(args, data, key) | ||
local v = args[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 | |||
v = data[key] | |||
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 | |||
-- more forgiving key lookup + debug | |||
local function find_record(art) | |||
-- direct lookups | |||
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 | end | ||
Line 53: | Line 61: | ||
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 | if type(DOCS) ~= 'table' or next(DOCS) == nil then | ||
return warn('Could not load data. Tried: '..(SOURCE or '(unknown)')..'.') | return warn('Could not load data. Tried: '..(SOURCE or '(unknown)')..'.') | ||
end | end | ||
local data = find_record(art) | |||
local data = | |||
if not data then | if not data then | ||
return warn(('Key "%s" not found in %s'):format(art, SOURCE or '(unknown)')) | -- show a few keys to diagnose quickly | ||
local samples, count = {}, 0 | |||
for k, _ in pairs(DOCS) do | |||
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 | end | ||
local box = mw.html.create('table'):addClass('infobox') | local box = mw.html.create('table'):addClass('infobox') | ||
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 87: | Line 100: | ||
return tostring(box) | return tostring(box) | ||
end | end | ||
return p | return p |