Module:Build bracket/Helpers
local Helpers = {}
-- ====================================
-- 1) ARG ACCESS / BARGS WIRING
-- ====================================
local _bargs = nil
function Helpers.setBargs(fn)
_bargs = fn
end
-- If Config.init doesn't call this, Helpers installs fallbacks below.
function Helpers.installArgAccessors(fargs, pargs)
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
Helpers.setBargs(Helpers.bargs)
end
-- Default bargs (so unit tests don’t explode without Config.init)
Helpers.bargs = function(key)
if _bargs then return _bargs(key) end
return nil
end
-- ====================================
-- 2) BASIC CHECKS & YES/NO PARSERS
-- ====================================
function Helpers.isempty(s) return s == nil or s == "" end
function Helpers.notempty(s) return s ~= nil and s ~= "" end
-- tolerant, case-insensitive
function Helpers.yes(val)
if val == nil then return false end
val = tostring(val):lower()
return val == "y" or val == "yes" or val == "true" or val == "1"
end
function Helpers.no(val)
if val == nil then return false end
val = tostring(val):lower()
return val == "n" or val == "no" or val == "false" or val == "0"
end
-- ====================================
-- 3) STRING HELPERS
-- ====================================
-- 1 -> 'a', 2 -> 'b', ...; wraps beyond 26; clamps nil/invalid to 'a'
function Helpers.toChar(num)
num = tonumber(num) or 1
if num < 1 then num = 1 end
-- wrap to 1..26 to avoid errors if headers exceed 26
num = ((num - 1) % 26) + 1
return string.char(string.byte("a") + num - 1)
end
-- Escape single characters for use inside a Lua pattern character class.
local function _esc_cc(ch)
-- dash, caret, percent, closing bracket need escaping inside [...]
if ch == "-" or ch == "^" or ch == "]" or ch == "%" then
return "%" .. ch
end
return ch
end
-- Fast split over single-char delimiters; drops empties; optional number-cast.
function Helpers.split(str, delim, tonum)
if Helpers.isempty(str) then return {} end
local result, buf = {}, {}
for i = 1, #delim do buf[#buf + 1] = _esc_cc(delim[i]) end
local patt = "[^" .. table.concat(buf) .. "]+"
for w in string.gmatch(str, patt) do
result[#result + 1] = (tonum == true) and tonumber(w) or w
end
return result
end
-- Remove bold inside (...) and [...] 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 PREFIX = "__WIKILINK__"
local counter, placeholders = 0, {}
-- 1) Extract wikilinks to placeholders
text = text:gsub("%[%[(.-)%]%]", function(link)
counter = counter + 1
local key = PREFIX .. counter .. "__"
placeholders[key] = link
return key
end)
-- 2) Wrap balanced (...) and [...]
text = text:gsub("(%b())", function(m) return string.format(STYLE_NORMAL, m) end)
:gsub("(%b[])", function(m) return string.format(STYLE_NORMAL, m) end)
-- 3) Restore wikilinks
for key, link in pairs(placeholders) do
text = text:gsub(key, "[[" .. link .. "]]")
end
return text
end
-- ====================================
-- 4) STYLE & DIMENSIONS
-- ====================================
-- Border mask is {top, right, bottom, left}
function Helpers.cellBorder(b)
-- assume well-formed input; callers always pass 4 numbers
return b[1] .. "px " .. b[2] .. "px " .. b[3] .. "px " .. b[4] .. "px"
end
-- Normalize CSS length: numbers -> "Npx"; strings with units pass through.
function Helpers.toCssLength(v, defaultVal)
if v == nil or v == "" then return defaultVal end
local n = tonumber(v)
if n then return tostring(n) .. "px" end
return v
end
-- Resolve widths from args with normalization & default
function Helpers.getWidth(ctype, defaultVal)
local raw = Helpers.bargs(ctype .. "-width")
return Helpers.toCssLength(raw, defaultVal)
end
return Helpers
Content Disclaimer
Informasi ini disarikan dari Wikipedia dan disajikan kembali untuk tujuan edukasi. Konten tersedia di bawah lisensi CC BY-SA 3.0. Kami tidak bertanggung jawab atas ketidakakuratan data yang bersumber dari kontribusi publik tersebut.
- The information displayed on this website is sourced in part or in whole from Wikipedia and has been adapted for the purpose of restating it. We strive to provide accurate and relevant information, however:
- There is no guarantee of absolute accuracy. Wikipedia is an open, collaborative project that can be edited by anyone, so information is subject to change.
- It is not intended to constitute professional advice. The content displayed is for informational and educational purposes only. For important decisions (e.g., medical, legal, or financial), please consult a professional.
- Content copyright. Wikipedia is licensed under the Creative Commons Attribution-ShareAlike License (CC BY-SA). This means that content may be reused with appropriate attribution and shared under a similar license.
- Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.