Module:Spell: Difference between revisions

From Kronopolis and the Flat Plane
m test
m test
Line 36: Line 36:
             if (spell['title'] ~= nil) then
             if (spell['title'] ~= nil) then
                 if (spell['title'] ~= frame.args['category']) and (string.find(spell['title'],'Category:') == nil) and (string.find(spell['title'],'Spell/List') == nil) then
                 if (spell['title'] ~= frame.args['category']) and (string.find(spell['title'],'Category:') == nil) and (string.find(spell['title'],'Spell/List') == nil) then
                     table.insert(out, frame:preprocess("<h2 class='link'>" .. spell['title'] .. "</h2><a>" .. spell['title'] .. "|Link</a>"))
                     table.insert(out, frame:preprocess("<h2 class='link'>" .. spell['title'] .. "</h2>[[" .. spell['title'] .. "|Link]]<hr>"))
                     table.insert(out, frame:expandTemplate({title=':' .. spell['title']}))
                     table.insert(out, frame:expandTemplate({title=':' .. spell['title']}))
                     table.insert(out, frame:preprocess("\n----"))
                     table.insert(out, frame:preprocess("\n----"))

Revision as of 08:47, 17 January 2024

Usage

  • {{#invoke:spell|list|domain=}}
  • {{#invoke:spell|list|category=}}

local p = {}

local util = require('Module:Utility')

function p.list(frame)
    local out = {}
    if frame.args.domain ~= nil then
        local json = 'Module:Spell/Domain/' .. util.firstToUpper(frame.args['domain']) .. '.json'
        local spells = mw.loadJsonData(json)
        
        table.insert(out, '{| class="wikitable"\n! style="text-align:center" | Level !! style="text-align:center" | Spells\n|-\n')

        for spell_level,_ in ipairs(spells) do
            table.insert(out, "| '''" .. util.formatLevel(spell_level-1) .. "''' \n|")
            for spell_index, spell in ipairs(spells[spell_level]) do

                if spell_index > 1 then 
                    table.insert(out, "<br>") 
                end
                table.insert(out, "[[" .. spell .. "]]")
                
            end
            table.insert(out, '\n|-\n')
        end

        table.insert(out, '|}')

    elseif frame.args.category ~= nil then
        local payload = {
            source=mw.site.server .. mw.site.scriptPath .. '/api.php?action=query&format=json&generator=categorymembers&gcmlimit=max&gcmtitle=Category:' .. util.firstToUpper(frame.args.category),
            format='json'
        }
        --table.insert(out, frame:expandTemplate({title='Spell/CSS'}))
        local json = mw.ext.externalData.getExternalData(payload)
        for _,spell in ipairs(json) do
            if (spell['title'] ~= nil) then
                if (spell['title'] ~= frame.args['category']) and (string.find(spell['title'],'Category:') == nil) and (string.find(spell['title'],'Spell/List') == nil) then
                    table.insert(out, frame:preprocess("<h2 class='link'>" .. spell['title'] .. "</h2>[[" .. spell['title'] .. "|Link]]<hr>"))
                    table.insert(out, frame:expandTemplate({title=':' .. spell['title']}))
                    table.insert(out, frame:preprocess("\n----"))
                end
            end
        end 
    end
    return table.concat(out)
end

return p