Module:Item Information

From Heavenly Wiki
Revision as of 00:52, 3 May 2025 by Medici (talk | contribs) (Created page with "local p = {} local paramTest = require('Module:Paramtest') local onMain = require('Module:Mainonly').on_main local yesNo = require('Module:Yesno') -- Add error checking for SMW local smw = mw.smw if not smw then mw.log('SMW Lua bridge not available') smw = { set = function() return nil end -- Stub function that does nothing } end function buildRow(item, info) local row = mw.html.create('tr') row:tag('td'):wikitext(item) row:tag('td'):w...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

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

local p = {}

local paramTest = require('Module:Paramtest')
local onMain = require('Module:Mainonly').on_main
local yesNo = require('Module:Yesno')

-- Add error checking for SMW
local smw = mw.smw
if not smw then
    mw.log('SMW Lua bridge not available')
    smw = {
        set = function() return nil end  -- Stub function that does nothing
    }
end

function buildRow(item, info)
    local row = mw.html.create('tr')
    row:tag('td'):wikitext(item)
    row:tag('td'):wikitext(info)
    return row
end

function p._main(args)
    local ret = mw.html.create('table'):addClass('wikitable sticky-header')
    
    if(paramTest.has_content(args.title)) then
        ret:tag('caption'):wikitext(args.title)
    end
    
    ret:tag('tr')
        :tag('th'):wikitext('Item'):done()
        :tag('th'):wikitext('Additional Information'):done()
    
    local itemInputs = {}
    -- Allow up to 20 item-info pairs by default
    local maxItems = paramTest.default_to(args.rows, 20)
    
    for i = 1, maxItems do
        local item = args['item' .. i]
        local info = args['info' .. i]
        
        if(paramTest.has_content(item) and paramTest.has_content(info)) then
            ret:node(buildRow(item, info))
            itemInputs[i] = {item = item, info = info}
        end
    end
    
    local useSmw = yesNo(args.smw or '', true)
    if(useSmw and onMain() and smw) then
        local success, err = pcall(function()
            local jsonObject = { ['Item Information'] = itemInputs, title = args.title}
            local smwmap = {
                ['Item Information JSON'] = mw.text.jsonEncode(jsonObject),
            }
            smw.set(smwmap)
        end)
        if not success then
            mw.log('SMW error: ' .. (err or 'unknown error'))
        end
    end
    
    return ret
end

function p.main(frame)
    local args = frame:getParent().args    
    return p._main(args)
end

return p