Edge Rewrite
// HTMLRewriter · presentation

This page was redesigned at the edge.

Cloudflare fetched the original article and streamed it through HTMLRewriter to apply an entirely new visual system without rebuilding the source page.

Jump to content

Module:Build bracket/Config

Permanently protected module
From Wikipedia, the free encyclopedia
local Config = {}

-- ====================================
-- 1) INIT & DEFENSIVE ACCESSORS
-- ====================================
function Config.init(frame, state, config, Helpers)
    -- Defend against nil inputs; don't mutate caller refs
    state   = state   or {}
    config  = config  or {}
    Helpers = Helpers or {}

    -- Pull args from frame/parent (both optional)
    local fargs  = (frame and frame.args) or {}
    local parent = (frame and frame.getParent and frame:getParent()) or nil
    local pargs  = (parent and parent.args) or {}

    -- Keep originals for debugging
    config._fargs, config._pargs = fargs, pargs

    -- Install arg accessors (use provided installer if present)
    if Helpers.installArgAccessors then
        Helpers.installArgAccessors(fargs, pargs)
    else
        Helpers.getFArg = function(k) return fargs[k] end
        Helpers.getPArg = function(k) return pargs[k] end
        Helpers.bargs   = function(k)
            local v = pargs[k]
            if v == nil or v == "" then v = fargs[k] end
            return v
        end
    end

    -- Short aliases
    local bargs   = Helpers.bargs
    local getFArg = Helpers.getFArg
    local getPArg = Helpers.getPArg

    -- ====================================
    -- 2) YES/NO FALLBACKS (robust parsing)
    -- ====================================
    local function yesish(s)
        s = (s or ""):lower()
        return s == "y" or s == "yes" or s == "true" or s == "1"
    end
    local function noish(s)
        s = (s or ""):lower()
        return s == "n" or s == "no" or s == "false" or s == "0"
    end

    -- Prefer Helpers.yes/no if provided; fall back to yesish/noish
    local YES = Helpers.yes or yesish
    local NO  = Helpers.no  or noish

    -- ====================================
    -- 3) CORE DIMENSIONS & CLAMPING
    -- ====================================
    -- Rows: keep '' sentinel when user didn’t set |rows=|
    config.r = tonumber(getFArg("rows")) or ""

    -- Rounds: {{#invoke:..|rounds=}} with optional parent override maxround(s)
    local rounds_f = tonumber(getFArg("rounds")) or 1
    local maxc     = tonumber(getPArg("maxrounds")) or tonumber(getPArg("maxround")) or nil

    config.c = (type(maxc) == "number") and maxc or rounds_f

    -- minround -> base (clamped)
    local minr = tonumber(getPArg("minround")) or 1
    if minr < 1 then minr = 1 end
    if minr > config.c then minr = config.c end
    config.minc   = minr
    config.base   = minr - 1
    config.c_total = config.c  -- persist original visible round count

    -- ====================================
    -- 4) LAYOUT/STYLE FLAGS
    -- ====================================
    config.autocol    = YES(getFArg("autocol"))
    config.colspacing = tonumber(getFArg("col-spacing")) or 6
    config.height     = bargs("height")

    -- nowrap can be supplied in parent or direct call
    local nowrapArg = getPArg("nowrap") or getFArg("nowrap")
    config.nowrap = not NO(nowrapArg)

    -- ====================================
    -- 5) BOLDWINNER MODE/POLICY
    -- ====================================
    local bw = (bargs("boldwinner") or ""):lower()
    config.boldwinner           = bw
    config.boldwinner_mode      = "off"   -- 'high' | 'low' | 'off'
    config.boldwinner_aggonly   = false   -- only bold aggregate cell/row

    if bw == "low" then
        config.boldwinner_mode = "low"
    elseif bw == "high" or YES(bw) then
        config.boldwinner_mode = "high"
    elseif bw == "aggregate" or bw == "agg" or bw == "aggregate-high" or bw == "agg-high" then
        config.boldwinner_mode    = "high"
        config.boldwinner_aggonly = true
    elseif bw == "aggregate-low" or bw == "agg-low" then
        config.boldwinner_mode    = "low"
        config.boldwinner_aggonly = true
    end

    if YES(bargs("boldwinner-aggregate-only")) then
        config.boldwinner_aggonly = true
    end

    -- ====================================
    -- 6) SEEDS & AGGREGATE SETTINGS
    -- ====================================
    -- forceseeds: explicit truthy; seeds: true unless explicitly "no"
    local seedsArg       = bargs("seeds")
    config.forceseeds    = YES(seedsArg)
    config.seeds         = not NO(seedsArg)

    -- aggregate mode
    do
        local aval = (bargs("aggregate") or ""):lower()
        if aval == "sets" or aval == "legs" then
            config.aggregate_mode = "sets"
        elseif aval == "score" then
            config.aggregate_mode = "score"
        elseif YES(aval) then
            config.aggregate_mode = "manual"
        else
            config.aggregate_mode = "off"
        end
        config.aggregate = (config.aggregate_mode ~= "off")
    end

    -- show-bye-paths (default true; only explicit "no" disables)
    do
        local v = (bargs("show-bye-paths") or ""):lower()
        config.show_bye_paths = (v == "") and true or (not NO(v))
    end

    -- autolegs (explicit truthy)
    config.autolegs = YES(bargs("autolegs"))

    -- ====================================
    -- 7) PARAMSTYLE INFERENCE
    -- ====================================
    local styleArg = (bargs("paramstyle") or ""):lower()
    local function hasNumberedArgs()
        -- Scan a reasonable window of positional args from both parent/frame via bargs
        for n = 1, 48 do
            local v = bargs(tostring(n))
            if v ~= nil and v ~= "" then return true end
        end
        return false
    end

    if styleArg == "numbered" then
        config.paramstyle = "numbered"
    elseif styleArg == "indexed" then
        config.paramstyle = "indexed"
    else
        config.paramstyle = hasNumberedArgs() and "numbered" or "indexed"
    end

    -- ====================================
    -- 8) THEME CONSTANTS (DEFAULTS)
    -- ====================================
    config.COLORS = config.COLORS or {
        cell_bg_light   = "var(--background-color-neutral-subtle,#f8f9fa)",
        cell_bg_dark    = "var(--background-color-neutral,#eaecf0)",
        text_color      = "var(--color-base,#202122)",
        path_line_color = "gray",
        cell_border     = "var(--border-color-base,#a2a9b1)"
    }

    return state, config, Helpers
end

return Config