Module:Infocard: Difference between revisions

From Path of Exile Wiki
Jump to navigation Jump to search
(This pattern allows the same function to be called, regardless of whether we're calling it via #invoke or from within another module.)
(Template styles cause issues when output is stored in a Cargo field. Use MediaWiki:Sitewide.css instead. https://discord.com/channels/872882365406523452/877595309792968715/947275003332788284)
 
(2 intermediate revisions by the same user not shown)
Line 3: Line 3:
--                            Module:Infocard
--                            Module:Infocard
--  
--  
-- This module implements Template:Infocard2 and is used by a number of other
-- This module implements Template:Infocard and is used by a number of other
-- modules to display info cards.
-- modules to display info cards.
-------------------------------------------------------------------------------
-------------------------------------------------------------------------------


local getArgs -- Lazy load require('Module:Arguments').getArgs
require('Module:No globals')
local m_util = require('Module:Util')


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


-- Wrapper templates
-- The cfg table contains all localisable strings and configuration, to make it
cfg.wrappers = {
-- easier to port this module to another wiki.
    'Template:Infocard',
local cfg = use_sandbox and mw.loadData('Module:Infocard/config/sandbox') or mw.loadData('Module:Infocard/config')
    'Template:Infocard2',
 
}
local i18n = cfg.i18n


-- ----------------------------------------------------------------------------
-- ----------------------------------------------------------------------------
Line 22: Line 24:


local function _main(args)
local function _main(args)
     local container = mw.html.create('div')
    args.heading = args.heading or args.header
         :attr( 'class', 'infocard ' .. (args.class or '') )
    args.subheading = args.subheading or args.subheader
     local topbar = mw.html.create('div')
     local html = mw.html.create('div')
         :attr('class', 'topbar')
         :addClass('info-card')
     topbar
    if args.class then
        html:addClass(args.class)
    end
    if args.above then
        html
            :tag('div')
                :addClass('info-card__above')
                :node(args.above)
    end
     local card = mw.html.create('div')
         :addClass('info-card__card')
    local header = mw.html.create('div')
        :addClass('info-card__header')
     header
         :tag('div')
         :tag('div')
             :attr('class', 'left')
             :addClass('left')
             :wikitext(args.headerleft)
             :wikitext(args.headerleft or '')
            :done()
     local middle = mw.html.create('div')
     local middle = mw.html.create('div')
         :attr('class', 'middle')
         :addClass('middle')
     middle
     middle
         :tag('div')
         :tag('div')
             :attr('class', 'header')
             :addClass('heading')
             :wikitext(args.header)
             :wikitext(args.heading)
            :done()
     if args.subheading then
     if args.subheader then
         middle
         middle
             :tag('div')
             :tag('div')
                 :attr('class', 'subheader')
                 :addClass('subheading')
                 :wikitext(args.subheader)
                 :wikitext(args.subheading)
                :done()
     end
     end
     topbar
     header
         :node(middle)
         :node(middle)
     topbar
     header
         :tag('div')
         :tag('div')
             :attr('class', 'right')
             :addClass('right')
             :wikitext(args.headerright)
             :wikitext(args.headerright or '')
            :done()
     card:node(header)
     container
     local body = mw.html.create('div')
        :node(topbar)
         :addClass('info-card__body')
     local inner = mw.html.create('div')
         :attr('class', 'inner')
     local block
     local block
     local i = 1
     for i=1, math.huge do -- repeat until no more blocks are found
    while args[i] do
        if args[i] == nil then
            break
        end
         block = mw.html.create('div')
         block = mw.html.create('div')
             :attr( 'class', 'block ' .. (args[i .. 'class'] or '') )
             :addClass('block')
        if type(args[i]) == 'table' then
        if args[i .. 'class'] then
             block:node(args[i])
             block:addClass(args[i .. 'class'])
        elseif type(args[i]) == 'string' then
            block:wikitext(args[i])
         end
         end
         inner:node(block)
         block:node(args[i])
         i = i + 1
         body:node(block)
     end
     end
     container:node(inner)
     card:node(body)
     return tostring(container)
    html:node(card)
    if args.below then
        html
            :tag('div')
                :addClass('info-card__below')
                :node(args.below)
    end
     return tostring(html)
end
end


Line 79: Line 96:
local p = {}
local p = {}


function p.main(frame)
p.main = m_util.misc.invoker_factory(_main, {
    local args
    wrappers = cfg.wrappers,
    if type(frame.args) == 'table' then
})
        -- Called via #invoke, so use getArgs().
        getArgs = require('Module:Arguments').getArgs
        args = getArgs(frame, {
            wrappers = cfg.wrappers
        })
    else
        -- Called from another module or from the debug console, so assume args
        -- are passed in directly.
        args = frame
    end
    return _main(args)
end


p.infocard = p.main
p.infocard = p.main

Latest revision as of 23:42, 26 February 2022

Module documentation[view] [edit] [history] [purge]


Lua logo

This module depends on the following other modules:

This module implements {{infocard}}.

-------------------------------------------------------------------------------
-- 
--                            Module:Infocard
-- 
-- This module implements Template:Infocard and is used by a number of other
-- modules to display info cards.
-------------------------------------------------------------------------------

require('Module:No globals')
local m_util = require('Module:Util')

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

-- 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:Infocard/config/sandbox') or mw.loadData('Module:Infocard/config')

local i18n = cfg.i18n

-- ----------------------------------------------------------------------------
-- Main function
-- ----------------------------------------------------------------------------

local function _main(args)
    args.heading = args.heading or args.header
    args.subheading = args.subheading or args.subheader
    local html = mw.html.create('div')
        :addClass('info-card')
    if args.class then
        html:addClass(args.class)
    end
    if args.above then
        html
            :tag('div')
                :addClass('info-card__above')
                :node(args.above)
    end
    local card = mw.html.create('div')
        :addClass('info-card__card')
    local header = mw.html.create('div')
        :addClass('info-card__header')
    header
        :tag('div')
            :addClass('left')
            :wikitext(args.headerleft or '')
    local middle = mw.html.create('div')
        :addClass('middle')
    middle
        :tag('div')
            :addClass('heading')
            :wikitext(args.heading)
    if args.subheading then
        middle
            :tag('div')
                :addClass('subheading')
                :wikitext(args.subheading)
    end
    header
        :node(middle)
    header
        :tag('div')
            :addClass('right')
            :wikitext(args.headerright or '')
    card:node(header)
    local body = mw.html.create('div')
        :addClass('info-card__body')
    local block
    for i=1, math.huge do -- repeat until no more blocks are found
        if args[i] == nil then
            break
        end
        block = mw.html.create('div')
            :addClass('block')
        if args[i .. 'class'] then
            block:addClass(args[i .. 'class'])
        end
        block:node(args[i])
        body:node(block)
    end
    card:node(body)
    html:node(card)
    if args.below then
        html
            :tag('div')
                :addClass('info-card__below')
                :node(args.below)
    end
    return tostring(html)
end

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

local p = {}

p.main = m_util.misc.invoker_factory(_main, {
    wrappers = cfg.wrappers,
})

p.infocard = p.main
p._main = p.main -- Contingency for modules that are still calling p._main()

return p