Module:MapBranchingTable

Revision as of 23:39, 6 November 2016 by がか (talk | contribs) (Created page with "local getArgs = require("Module:GetArgs") local format = require("Module:StringInterpolation").format local templates = { table_header = [[{| class="wikitable mw-collaps...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

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

local getArgs = require("Module:GetArgs")
local format = require("Module:StringInterpolation").format

local templates = {

    table_header = [[{| class="wikitable mw-collapsible mw-collapsed typography-xl-optout" style="width:100%;"
|-
!colspan="3"|<span style="float:left;padding-left:10px;">Branching Rules</span>
|-
!colspan="2"|Nodes||Rules
|-
]],

    table_footer = "\n|}",

    table_row_start = [[|rowspan="${rowspan}" style="text-align:center;vertical-align:middle;width:10%"|${from}]],

    table_row = [[${separator}|style="text-align:center;"|${to}||${rules}]],

}

function formatTable(args)
    local branching = {}
    for route, rules in pairs(args) do
        local from, to = route:match("(%S+)%s*->%s*(%S+)")
        if branching[from] then
            branching[from][to] = rules
        else
            branching[from] = {}
            branching[from][to] = rules
        end
    end
    local rows = {}
    for from, tos in pairs(branching) do
        table.insert(rows, format{
            templates.table_row_start,
            rowspan = 3,
            from = from,
        })
        local first = true
        for to, rules in pairs(tos) do
            table.insert(rows, format{
                templates.table_row,
                separator = first and "|-\n" or "",
                to = to,
                rules = rules,
            })
            first = false
        end
    end
    return templates.table_header .. table.concat(rows, "\n") .. templates.table_footer
end

local MapBranchingTable = {}

function MapBranchingTable.format(frame, args_)
    local args = args_ or getArgs{frame = frame:getParent()}
    return formatTable(args)
end

-- MapBranchingTable.t = MapBranchingTable.format(nil, { ["1 -> 2"] = "..." })

return MapBranchingTable