Module:Sandbox/Mefisto1029/main: Difference between revisions

From Path of Exile 2 Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
 
(9 intermediate revisions by the same user not shown)
Line 1: Line 1:
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
--  
--
--                             Module:Keyword
--                               Module:Keyword
--  
--
-- This module implements Template:Keyword and Template:Keyword infobox
-- This module implements Template:Keyword and Template:Keyword infobox
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------
Line 18: Line 18:
-- easier to port this module to another wiki.
-- easier to port this module to another wiki.
--local cfg = use_sandbox and mw.loadData('Module:Keyword/config/sandbox') or mw.loadData('Module:Keyword/config')
--local cfg = use_sandbox and mw.loadData('Module:Keyword/config/sandbox') or mw.loadData('Module:Keyword/config')
local cfg = mw.loadData('Module:Sandbox/Mefisto1029/config')
local cfg = mw.loadData('Module:Sandbox/Mefisto1029/main/s1')
 
local i18n = cfg.i18n


-- The data table contains all informations, to make it
-- The data table contains all informations, to make it
-- easier to port this module to another wiki.
-- 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 m_data = mw.loadData('Module:Keyword/keywords')
local m_data = mw.loadData('Module:Sandbox/Mefisto1029/data')
local m_data = mw.loadData('Module:Sandbox/Mefisto1029/main/s2')


local i18n = cfg.i18n
--local m_data = mw.loadData('Module:Keyword/keywords_lookup')
local m_data_lookup = mw.loadData('Module:Sandbox/Mefisto1029/main/s3')


local _keyword  -- forward declaration
local _keyword  -- forward declaration
Line 56: Line 59:
-- Get data from submodule
-- Get data from submodule
function h.get_data(tpl_args)
function h.get_data(tpl_args)
     local data = m_data[tpl_args.id]
     local data = m_data[m_data_lookup[tpl_args.id]]
     if not data then
 
    -- Get error only for infobox, else just get input text
     if not data and tpl_args.options.caller == 'keyword_infobox' then
         error(i18n.errors.no_data)
         error(i18n.errors.no_data)
     end
     end
Line 65: Line 70:
-- Get link
-- Get link
function h.resolve_link(tpl_args, data)
function h.resolve_link(tpl_args, data)
     if data.no_link then
     if data.no_link or not data.title then
         return nil
         return nil
     end
     end
Line 71: Line 76:
     if data.links then
     if data.links then
         for _, v in ipairs(data.links) do
         for _, v in ipairs(data.links) do
             if type(v) == "table" and tpl_args.text == v[1] then
             if type(v) == 'table' and tpl_args.text == v[1] then
                 return v[2]
                 return v[2]
             elseif tpl_args.text == v then
             elseif tpl_args.text == v then
Line 79: Line 84:
     end
     end


     if data.default_link then
     if data.default then
         return data.default_link
         return data.default
     else
     else
         return data.title
         return data.title
Line 86: Line 91:
end
end


-- Finds template calls in a string and automatically
-- Finds keywords calls in a string and automatically
-- replace that template with the returned value
-- replace that keyword with the returned value
-- args are stored as one string
-- args are stored as one string
function h.process_templates(tpl_args, content)
function h.process_templates(tpl_args, description)
     local caller = tpl_args.options and tpl_args.options.caller or 'keyword'
     local caller = tpl_args.options and tpl_args.options.caller or 'keyword'
    description = description or ''


     -- match {{Template}} or {{Template|Arg}}
     -- match [keyword_id] or [keyword_id|text]
     local processed = content:gsub("{{(.-)}}", function(inner)
     local processed = description:gsub('%[(.-)%]', function(inner)
         -- split by | if present
         -- split by | if present
         local key, text = inner:match("^%s*([^|]+)%s*|?(.-)%s*$")
         local key, text = inner:match('^%s*([^|]+)%s*|?(.-)%s*$')
         key = key or ""
         key = key or ''
         text = text or ""
         text = text or ''
        local display = text ~= '' and text or key


         if caller == 'keyword' then
         if caller == 'keyword' then
             -- just return text: second part if present, else key
             -- just return text: second part if present, else key
             return text ~= "" and text or key
             return display
         elseif caller == 'keyword_infobox' then
         elseif caller == 'keyword_infobox' then
             -- call _keyword function dynamically
             -- call _keyword function dynamically
             return _keyword({
             return _keyword({
                 id = key,
                 id = key,
                 text = text ~= "" and text or key,
                 text = display,
             })
             })
         elseif caller == 'keyword_list' then
         elseif caller == 'keyword_list' then
             return string.format('[[#%s|%s]]', key, text ~= "" and text or key)
             return string.format('[[#%s|%s]]', key, display)
         else
         else
             -- fallback: return raw text
             -- fallback: return raw text
             return text ~= "" and text or key
             return display
         end
         end
     end)
     end)
Line 125: Line 132:
_keyword = function(tpl_args)
_keyword = function(tpl_args)
     h.preset_args(tpl_args)
     h.preset_args(tpl_args)
    tpl_args.options.caller = 'keyword'


     local data = h.get_data(tpl_args)
     local data = h.get_data(tpl_args)


     tpl_args.options.caller = 'keyword'
     -- return input text if no keyword found
     local content = h.process_templates(tpl_args, data.content)
    if not data then
        return tpl_args.text or tpl_args.id
    end
 
     local description = h.process_templates(tpl_args, data.desc)


     local link = h.resolve_link(tpl_args, data)
     local link = h.resolve_link(tpl_args, data)
     if not link then
     if not link then
         return m_util.html.hoverbox(tpl_args.text or tpl_args.id, content)
         return m_util.html.hoverbox(tpl_args.text or tpl_args.id, description)
     end
     end


     return m_util.html.hoverbox(string.format('[[%s|%s]]', link, tpl_args.text or tpl_args.id), content)
     return m_util.html.hoverbox(string.format('[[%s|%s]]', link, tpl_args.text or tpl_args.id), description)
end
end


local function _keyword_infobox(tpl_args)
local function _keyword_infobox(tpl_args)
     h.preset_args(tpl_args)
     h.preset_args(tpl_args)
    tpl_args.options.caller = 'keyword_infobox'


     local data = h.get_data(tpl_args)
     local data = h.get_data(tpl_args)


    tpl_args.options.caller = 'keyword_infobox'
     local description = h.process_templates(tpl_args, data.desc)
     local content = h.process_templates(tpl_args, data.content)


     local infocard_args = {}
     local infocard_args = {}
Line 152: Line 164:
     infocard_args.subheading = i18n.tooltips.tooltip
     infocard_args.subheading = i18n.tooltips.tooltip


     infocard_args[1] = content
     infocard_args[1] = description
     infocard_args['1class'] = 'tc -normal'
     infocard_args['1class'] = 'tc -normal'


Line 161: Line 173:
     tpl_args.options = tpl_args.options or {}
     tpl_args.options = tpl_args.options or {}
     tpl_args.options.caller = 'keyword_list'
     tpl_args.options.caller = 'keyword_list'
   
 
     -- Create a sortable wikitable with headers and rows
     -- Create a sortable wikitable with headers and rows
     local tbl = mw.html.create('table')
     local tbl = mw.html.create('table')
Line 167: Line 179:


     -- Headers
     -- Headers
     local headers = {'Id', 'Title', 'Content'}
     local headers = {'Id', 'Title', 'Description'}
     local header_row = tbl:tag('tr')
     local header_row = tbl:tag('tr')
     for _, h in ipairs(headers) do
     for _, h in ipairs(headers) do
Line 173: Line 185:
             :wikitext(h)
             :wikitext(h)
     end
     end
   
 
     -- Rows
     -- Rows
     for id, keyword in pairs(m_data) do
     for id, keyword in pairs(m_data) do
Line 183: Line 195:
             :wikitext(keyword.title or '')
             :wikitext(keyword.title or '')
         row:tag('td')
         row:tag('td')
             :wikitext(h.process_templates(tpl_args, keyword.content or ''))
             :wikitext(h.process_templates(tpl_args, keyword.desc or ''))
     end
     end


Line 197: Line 209:
--
--
-- Template:Keyword
-- Template:Keyword
--  
--
p.keyword = m_util.misc.invoker_factory(_keyword, {
p.keyword = m_util.misc.invoker_factory(_keyword, {
     wrappers = cfg.wrappers.keyword,
     wrappers = cfg.wrappers.keyword,
Line 204: Line 216:
--
--
-- Template:Keyword infobox
-- Template:Keyword infobox
--  
--
p.keyword_infobox = m_util.misc.invoker_factory(_keyword_infobox, {
p.keyword_infobox = m_util.misc.invoker_factory(_keyword_infobox, {
     wrappers = cfg.wrappers.keyword_infobox,
     wrappers = cfg.wrappers.keyword_infobox,
Line 211: Line 223:
--
--
-- Template:Keyword list
-- Template:Keyword list
--  
--
p.keyword_list = m_util.misc.invoker_factory(_keyword_list)
p.keyword_list = m_util.misc.invoker_factory(_keyword_list)
-- ----------------------------------------------------------------------------
-- Debug
-- ----------------------------------------------------------------------------
p.debug = {}
function p.debug.test_keyword()
    -- Args
    local tpl_args = {}
    tpl_args[1] = 'Total'
    tpl_args[2] = 'Over break'
    -- Print in console
    mw.log(_keyword(tpl_args))
    return
end
function p.debug.test_keyword_infobox()
    -- Args
    local tpl_args = {}
    tpl_args[1] = 'ArmourOverbreak'
    tpl_args[2] = 'Over break'
    -- Print in console
    mw.log(_keyword_infobox(tpl_args))
    return
end
function p.debug.test_keyword_list()
    -- Args
    local tpl_args = {}
    -- Print in console
    mw.log(_keyword_list(tpl_args))
    return
end
function p.debug.test()
    -- Print in console
    --mw.log(p.debug.test_keyword())
    mw.log(p.debug.test_keyword_infobox())
    return
end


return p
return p

Latest revision as of 20:11, 6 October 2025

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 cfg = use_sandbox and mw.loadData('Module:Keyword/config/sandbox') or mw.loadData('Module:Keyword/config')
local cfg = mw.loadData('Module:Sandbox/Mefisto1029/main/s1')

local i18n = cfg.i18n

-- The data table contains all informations, to make it
-- easier to port this module to another wiki.
--local m_data = mw.loadData('Module:Keyword/keywords')
local m_data = mw.loadData('Module:Sandbox/Mefisto1029/main/s2')

--local m_data = mw.loadData('Module:Keyword/keywords_lookup')
local m_data_lookup = mw.loadData('Module:Sandbox/Mefisto1029/main/s3')

local _keyword  -- forward declaration

-- ----------------------------------------------------------------------------
-- 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

-- Preset basic args
function h.preset_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.options = tpl_args.options or {}

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

-- Get data from submodule
function h.get_data(tpl_args)
    local data = m_data[m_data_lookup[tpl_args.id]]

    -- Get error only for infobox, else just get input text
    if not data and tpl_args.options.caller == 'keyword_infobox' then
        error(i18n.errors.no_data)
    end
    return data
end

-- Get link
function h.resolve_link(tpl_args, data)
    if data.no_link or not data.title then
        return nil
    end

    if data.links then
        for _, v in ipairs(data.links) do
            if type(v) == 'table' and tpl_args.text == v[1] then
                return v[2]
            elseif tpl_args.text == v then
                return tpl_args.text
            end
        end
    end

    if data.default then
        return data.default
    else
        return data.title
    end
end

-- Finds keywords calls in a string and automatically
-- replace that keyword with the returned value
-- args are stored as one string
function h.process_templates(tpl_args, description)
    local caller = tpl_args.options and tpl_args.options.caller or 'keyword'
    description = description or ''

    -- match [keyword_id] or [keyword_id|text]
    local processed = description:gsub('%[(.-)%]', function(inner)
        -- split by | if present
        local key, text = inner:match('^%s*([^|]+)%s*|?(.-)%s*$')
        key = key or ''
        text = text or ''
        local display = text ~= '' and text or key

        if caller == 'keyword' then
            -- just return text: second part if present, else key
            return display
        elseif caller == 'keyword_infobox' then
            -- call _keyword function dynamically
            return _keyword({
                id = key,
                text = display,
            })
        elseif caller == 'keyword_list' then
            return string.format('[[#%s|%s]]', key, display)
        else
            -- fallback: return raw text
            return display
        end
    end)

    return processed
end

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

_keyword = function(tpl_args)
    h.preset_args(tpl_args)
    tpl_args.options.caller = 'keyword'

    local data = h.get_data(tpl_args)

    -- return input text if no keyword found
    if not data then
        return tpl_args.text or tpl_args.id
    end

    local description = h.process_templates(tpl_args, data.desc)

    local link = h.resolve_link(tpl_args, data)
    if not link then
        return m_util.html.hoverbox(tpl_args.text or tpl_args.id, description)
    end

    return m_util.html.hoverbox(string.format('[[%s|%s]]', link, tpl_args.text or tpl_args.id), description)
end

local function _keyword_infobox(tpl_args)
    h.preset_args(tpl_args)
    tpl_args.options.caller = 'keyword_infobox'

    local data = h.get_data(tpl_args)

    local description = h.process_templates(tpl_args, data.desc)

    local infocard_args = {}

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

    infocard_args[1] = description
    infocard_args['1class'] = 'tc -normal'

    return h.infocard(infocard_args)
end

local function _keyword_list(tpl_args)
    tpl_args.options = tpl_args.options or {}
    tpl_args.options.caller = 'keyword_list'

    -- Create a sortable wikitable with headers and rows
    local tbl = mw.html.create('table')
        :addClass('wikitable sortable')

    -- Headers
    local headers = {'Id', 'Title', 'Description'}
    local header_row = tbl:tag('tr')
    for _, h in ipairs(headers) do
        header_row:tag('th')
            :wikitext(h)
    end

    -- Rows
    for id, keyword in pairs(m_data) do
        local row = tbl:tag('tr')
            :attr('id', id)
        row:tag('td')
            :wikitext(id)
        row:tag('td')
            :wikitext(keyword.title or '')
        row:tag('td')
            :wikitext(h.process_templates(tpl_args, keyword.desc or ''))
    end

    return tbl
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,
})

--
-- Template:Keyword list
--
p.keyword_list = m_util.misc.invoker_factory(_keyword_list)

-- ----------------------------------------------------------------------------
-- Debug
-- ----------------------------------------------------------------------------

p.debug = {}

function p.debug.test_keyword()
    -- Args
    local tpl_args = {}
    tpl_args[1] = 'Total'
    tpl_args[2] = 'Over break'

    -- Print in console
    mw.log(_keyword(tpl_args))

    return
end

function p.debug.test_keyword_infobox()
    -- Args
    local tpl_args = {}
    tpl_args[1] = 'ArmourOverbreak'
    tpl_args[2] = 'Over break'

    -- Print in console
    mw.log(_keyword_infobox(tpl_args))

    return
end

function p.debug.test_keyword_list()
    -- Args
    local tpl_args = {}

    -- Print in console
    mw.log(_keyword_list(tpl_args))

    return
end

function p.debug.test()
    -- Print in console
    --mw.log(p.debug.test_keyword())
    mw.log(p.debug.test_keyword_infobox())

    return
end

return p