Module:Sandbox/Mefisto1029/main

From Path of Exile 2 Wiki
Jump to navigation Jump to search
Module documentation[create] [purge]
-------------------------------------------------------------------------------
-- 
--                             Module:Keyword
-- 
-- This module implements Template:Keyword and Template:Keyword infobox
-------------------------------------------------------------------------------

require('strict')
local m_util = require('Module:Util')

-- Should we use the sandbox version of our submodules?
--local use_sandbox = m_util.misc.maybe_sandbox('Keyword')

-- Lazy loading
local f_infocard -- require('Module:Infocard')._main

-- The cfg table contains all localisable strings and configuration, to make it
-- easier to port this module to another wiki.
--local m_data = use_sandbox and mw.loadData('Module:Keyword/data/sandbox') or mw.loadData('Module:Keyword/data')
--local cfg = use_sandbox and mw.loadData('Module:Keyword/config/sandbox') or mw.loadData('Module:Keyword/config')
local m_data = mw.loadData('Module:Sandbox/Mefisto1029/data')
local cfg = mw.loadData('Module:Sandbox/Mefisto1029/config')

local i18n = cfg.i18n

-- ----------------------------------------------------------------------------
-- Helper functions
-- ----------------------------------------------------------------------------

local h = {}

-- Lazy loading for Module:Infocard
function h.infocard(args)
    if not f_infocard then
        f_infocard = require('Module:Infocard')._main
    end
    return f_infocard(args)
end

function h.set_args(tpl_args)
    tpl_args.id = tpl_args.id or tpl_args[1]
    tpl_args.text = tpl_args.text or tpl_args[2]
    tpl_args.layer = tpl_args.layer or 1

    if not tpl_args.id then
        error(i18n.errors.missing_id)
    end
end

-- ----------------------------------------------------------------------------
-- Main functions
-- ----------------------------------------------------------------------------

local function _keyword(tpl_args)
    h.set_args(tpl_args)

    local data = m_data[tpl_args.id]
    if not data then
        error(i18n.errors.no_data)
    end
    
    local content
    if type(data.content) == 'string' then
        content = data.content
    else
        --function
        content = data.content(tpl_args.layer)
    end
    
    if tpl_args.layer > 1 then
        return tpl_args.text or tpl_args.id
    end

    local link
    if data.no_link then
        return m_util.html.hoverbox(tpl_args.text or tpl_args.id, content)
    elseif data.links then
        for i, v in ipairs(data.links) do
            if type(v) == 'table' and tpl_args.text == v[1] then
                link = v[2]
                break
            elseif tpl_args.text == v then
                link = tpl_args.text
                break
            end
        end
    end

    if not link then
        if data.fallback_link then
            link = data.fallback_link
        else
            link = data.title
        end
    end

    return m_util.html.hoverbox('[[' .. link .. '|' .. (tpl_args.text or tpl_args.id) .. ']]', content)
end

local function _keyword_infobox(tpl_args)
    h.set_args(tpl_args)

    local data = m_data[tpl_args.id]
    if not data then
        error(i18n.errors.no_data)
    end

    local infocard_args = {}

    infocard_args.header = data.title
    infocard_args.subheading = i18n.tooltips.tooltip

    infocard_args[1] = data.content
    infocard_args['1class'] = 'tc -normal'

    return h.infocard(infocard_args)
end

-- ----------------------------------------------------------------------------
-- Exported functions
-- ----------------------------------------------------------------------------

local p = {}

--
-- Template:Keyword
-- 
p.keyword = m_util.misc.invoker_factory(_keyword, {
    wrappers = cfg.wrappers.keyword,
})

--
-- Template:Keyword infobox
-- 
p.keyword_infobox = m_util.misc.invoker_factory(_keyword_infobox, {
    wrappers = cfg.wrappers.keyword_infobox,
})

return p