Jump to content

Module:Build bracket/Helpers

Permanently protected module
From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Pbrks (talk | contribs) at 02:03, 13 August 2025 (Created page with 'local Helpers = {} -- =========== -- Arg access -- =========== local _bargs = nil function Helpers.setBargs(fn) _bargs = fn end -- Default bargs (no-op) so unit tests don’t explode without Config.init. Helpers.bargs = function(key) if _bargs then return _bargs(key) end return nil end -- ========================= -- Basic truthy/empty checks -- ========================= function Helpers.isempty(s) return s == nil or s == '' end function He...'). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
(diff) ← Previous revision | Latest revision (diff) | Newer revision → (diff)
local Helpers = {}

-- ===========
-- Arg access
-- ===========
local _bargs = nil

function Helpers.setBargs(fn)
    _bargs = fn
end

-- Default bargs (no-op) so unit tests don’t explode without Config.init.
Helpers.bargs = function(key)
    if _bargs then return _bargs(key) end
    return nil
end

-- =========================
-- Basic truthy/empty checks
-- =========================
function Helpers.isempty(s) return s == nil or s == '' end 
function Helpers.notempty(s) return s ~= nil and s ~= '' end
function Helpers.yes(val) return val == 'y' or val == 'yes' end
function Helpers.no(val)  return val == 'n' or val == 'no'  end

-- ===============
-- String helpers
-- ===============
function Helpers.toChar(num) -- 1 -> 'a', 2 -> 'b', ...
    return string.char(string.byte("a") + num - 1)                    -- :contentReference[oaicite:5]{index=5}
end

-- Fast split that accepts a table of single-char delimiters (as in your code)
function Helpers.split(str, delim, tonum)
    local result = {}
    local a = "[^"..table.concat(delim).."]+"
    for w in string.gmatch(str, a) do
        if tonum == true then
            table.insert(result, tonumber(w))
        else
            table.insert(result, w)
        end
    end
    return result
end

-- Remove bold effect in parentheticals/brackets while preserving wikilinks
function Helpers.unboldParenthetical(text)
    if Helpers.isempty(text) then return text end

    local STYLE_NORMAL = '<span style="font-weight:normal">%s</span>'
    local PLACEHOLDER_PREFIX = '__WIKILINK__'

    -- Step 1: Extract wikilinks
    local counter = 0
    local placeholders = {}
    text = text:gsub('%[%[(.-)%]%]', function(link)
        counter = counter + 1
        local key = PLACEHOLDER_PREFIX .. counter .. '__'
        placeholders[key] = link
        return key
    end)

    -- Step 2: Wrap (...) and [...] portions
    text = text:gsub("(%b())", function(match) return string.format(STYLE_NORMAL, match) end)
    text = text:gsub("(%b[])", function(match) return string.format(STYLE_NORMAL, match) end)

    -- Step 3: Restore wikilinks
    for key, link in pairs(placeholders) do
        text = text:gsub(key, '[[' .. link .. ']]')
    end

    return text
end

-- =========================
-- Style & dimension helpers
-- =========================
-- Border mask is {top, right, bottom, left}
function Helpers.cellBorder(b)
    return b[1] .. 'px ' .. b[2] .. 'px ' .. b[3] .. 'px ' .. b[4] .. 'px'
end

-- Reads width from args like "<ctype>-width"; numeric -> "Npx", else raw.
-- Uses injected Helpers.bargs from Config.init.
function Helpers.getWidth(ctype, defaultVal)
    local bargs = Helpers.bargs
    local result = bargs(ctype .. '-width')
    if Helpers.isempty(result) then return defaultVal end
    if tonumber(result) ~= nil then return result .. 'px' end
    return result
end

return Helpers