Actions

Module

Chain: Difference between revisions

From Project Rebearth

Created page with "local p = {} function p.render(frame) local args = frame.args local parent = frame:getParent() local pargs = parent and parent.args or {} local items = {} for i = 1, 15 do local val = args[i] or pargs[i] if val then val = mw.text.trim(val) if val ~= '' then items[#items + 1] = val end end end if #items < 1 then return '' end local title = args.title..."
 
No edit summary
 
(One intermediate revision by the same user not shown)
Line 21: Line 21:
     end
     end


     local title = args.title or pargs.title or ''
     local title = mw.text.trim(args.title or pargs.title or '')
     title = mw.text.trim(title)
     local direction = mw.text.trim(args.direction or pargs.direction or ''):lower()
 
    local direction = args.direction or pargs.direction or ''
    direction = mw.text.trim(direction):lower()
     local isVertical = (direction == 'vertical')
     local isVertical = (direction == 'vertical')


     local root = mw.html.create('div')
     local root = mw.html.create('div'):addClass('rb-chain')
        :addClass('rb-chain')
     if isVertical then
     if isVertical then
         root:addClass('rb-chain-vertical')
         root:addClass('rb-chain-vertical')
Line 41: Line 37:


     for i, name in ipairs(items) do
     for i, name in ipairs(items) do
         if i > 1 then
         if i > 1 then
             root:tag('div')
             root:tag('div')
Line 48: Line 43:
         end
         end


         local isResource = (i % 2 == 1)
         local isResource = (i % 2 == 1)
 
         local parts = isResource and mw.text.split(name, '+') or nil
         local step = root:tag('div')
            :addClass('rb-chain-step')
 
        if isResource then
            step:addClass('rb-chain-resource')


             local icon = frame:expandTemplate{ title = 'Icon', args = { name } }
        if parts and #parts > 1 then
            step:tag('span')
             local group = root:tag('div'):addClass('rb-chain-group')
                :addClass('rb-chain-icon')
            for _, part in ipairs(parts) do
                 :wikitext(icon)
                 group:tag('div')
            step:tag('span')
                    :addClass('rb-chain-step')
                :addClass('rb-chain-label')
                    :addClass('rb-chain-resource')
                :wikitext('[[' .. name .. ']]')
                    :wikitext(frame:expandTemplate{ title = 'Icon', args = { mw.text.trim(part) } })
            end
         else
         else
             step:addClass('rb-chain-building')
             local step = root:tag('div')
                :addClass('rb-chain-step')
                :addClass(isResource and 'rb-chain-resource' or 'rb-chain-building')


             step:tag('span')
             local label = isResource
                 :addClass('rb-chain-label')
                 and frame:expandTemplate{ title = 'Icon', args = { name } }
                 :wikitext('[[' .. name .. ']]')
                 or  '[[' .. name .. ']]'
            step:wikitext(label)
         end
         end
     end
     end

Latest revision as of 14:12, 26 February 2026

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

local p = {}

function p.render(frame)
    local args = frame.args
    local parent = frame:getParent()
    local pargs = parent and parent.args or {}

    local items = {}
    for i = 1, 15 do
        local val = args[i] or pargs[i]
        if val then
            val = mw.text.trim(val)
            if val ~= '' then
                items[#items + 1] = val
            end
        end
    end

    if #items < 1 then
        return ''
    end

    local title = mw.text.trim(args.title or pargs.title or '')
    local direction = mw.text.trim(args.direction or pargs.direction or ''):lower()
    local isVertical = (direction == 'vertical')

    local root = mw.html.create('div'):addClass('rb-chain')
    if isVertical then
        root:addClass('rb-chain-vertical')
    end

    if title ~= '' then
        root:tag('div')
            :addClass('rb-chain-title')
            :wikitext(title)
    end

    for i, name in ipairs(items) do
        if i > 1 then
            root:tag('div')
                :addClass('rb-chain-arrow')
                :wikitext('→')
        end

        local isResource = (i % 2 == 1)
        local parts = isResource and mw.text.split(name, '+') or nil

        if parts and #parts > 1 then
            local group = root:tag('div'):addClass('rb-chain-group')
            for _, part in ipairs(parts) do
                group:tag('div')
                    :addClass('rb-chain-step')
                    :addClass('rb-chain-resource')
                    :wikitext(frame:expandTemplate{ title = 'Icon', args = { mw.text.trim(part) } })
            end
        else
            local step = root:tag('div')
                :addClass('rb-chain-step')
                :addClass(isResource and 'rb-chain-resource' or 'rb-chain-building')

            local label = isResource
                and frame:expandTemplate{ title = 'Icon', args = { name } }
                or  '[[' .. name .. ']]'
            step:wikitext(label)
        end
    end

    return tostring(root)
end

return p