Module:Sports club sections

.mw-parser-output .documentation,.mw-parser-output .documentation-metadata{border:1px solid var(--border-color-base,#a2a9b1);background-color:#ecfcf4;color:inhe

Module:Sports club sections
local p = {}

local function notEmpty(s)
    return s and s:match("%S")
end

local function normalizeSport(s)
    if not notEmpty(s) then
        return nil
    end
    s = mw.ustring.gsub(s, "^%s+", "")
    s = mw.ustring.gsub(s, "%s+$", "")
    s = mw.ustring.lower(s)
    return s
end

local function findMaxIndex(args, prefix)
    local max = 0
    for k, _ in pairs(args) do
        local n = tostring(k):match("^" .. prefix .. "(%d+)$")
        if n then
            n = tonumber(n)
            if n > max then
                max = n
            end
        end
    end
    return max
end

local function collectSports(args, prefix)
    local sports = {}
    for k, v in pairs(args) do
        local i = tostring(k):match("^" .. prefix .. "(%d+)$")
        if i and notEmpty(v) then
            sports[normalizeSport(v)] = true
        end
    end
    return sports
end

local function splitSport(text, threshold)
    if not text then
        return text, false
    end

    local words = {}
    for w in mw.text.gsplit(text, "%s+", false) do
        table.insert(words, w)
    end

    if #words <= 1 then
        return text, mw.ustring.len(text) > threshold
    end

    if mw.ustring.len(text) <= threshold then
        return text, false
    end

    local bestIndex = 1
    local bestDiff = math.huge
    local totalLen = mw.ustring.len(text)
    local target = totalLen / 2
    local currentLen = 0

    for i = 1, #words - 1 do
        currentLen = currentLen + mw.ustring.len(words[i]) + 1
        local diff = math.abs(currentLen - target)
        if diff < bestDiff then
            bestDiff = diff
            bestIndex = i
        end
    end

    local first = table.concat(words, " ", 1, bestIndex)
    local second = table.concat(words, " ", bestIndex + 1)
    local longest = math.max(mw.ustring.len(first), mw.ustring.len(second))

    return first .. "<br />" .. second, longest > threshold
end

local function resolveFile(icon, image, sport)
    if notEmpty(icon) then
        if mw.ustring.match(icon, "%.[A-Za-z0-9]+$") then
            return icon
        else
            return icon .. " pictogram.svg"
        end
    elseif notEmpty(image) then
        return image
    else
        return sport .. " pictogram.svg"
    end
end

local function buildTable(cells, wide)
    if #cells == 0 then
        return ""
    end

    local width = wide and "120%" or "100%"
    local colwidth = wide and "33.33%" or "33.33%"

    local out = {}
    table.insert(out, '{| style="width:' .. width .. '; text-align:center; table-layout:fixed;"')

    for i, cell in ipairs(cells) do
        if (i - 1) % 3 == 0 then
            table.insert(out, "|-")
        end
        table.insert(out, '| style="width:' .. colwidth .. '; vertical-align:top;" | ' .. cell)
    end

    table.insert(out, "|}")

    return table.concat(out, "\n")
end

local function buildLabel(args, i, sport, team, currentTitle, forceGender, threshold)
    local mteam = args["mteam" .. i]
    local wteam = args["wteam" .. i] or args["fteam" .. i]

    local sportWrapped, overflow = splitSport(sport, threshold)

    if forceGender then
        if notEmpty(team) then
            return string.format("[[%s|%s]]", team, sportWrapped), overflow
        elseif notEmpty(mteam) then
            return string.format("[[%s|%s<br />(men's)]]", mteam, sportWrapped), overflow
        elseif notEmpty(wteam) then
            return string.format("[[%s|%s<br />(women's)]]", wteam, sportWrapped), overflow
        else
            return sportWrapped, overflow
        end
    end

    local label

    if notEmpty(team) then
        if currentTitle and team == currentTitle then
            label = string.format("'''%s'''", sportWrapped)
        else
            label = string.format("[[%s|%s]]", team, sportWrapped)
        end

    elseif notEmpty(mteam) and not notEmpty(wteam) then
        label = string.format("[[%s|%s]]", mteam, sportWrapped)

    elseif notEmpty(wteam) and not notEmpty(mteam) then
        label = string.format("[[%s|%s]]", wteam, sportWrapped)

    elseif notEmpty(mteam) and notEmpty(wteam) then
        label = table.concat({
            sportWrapped,
            string.format("[[%s|(men's)]]", mteam),
            string.format("[[%s|(women's)]]", wteam)
        }, "<br />")

    else
        label = sportWrapped
    end

    return label, overflow
end

function p.render(frame)
    local parent = frame:getParent()
    local args = parent.args
    local currentTitle = mw.title.getCurrentTitle().prefixedText

    local threshold = 15

    local max = findMaxIndex(args, "sport")
    local cells = {}
    local wide = false

    local openSports = collectSports(args, "sport")
    local closedSports = collectSports(args, "closedsport")

    for i = 1, max do
        local sport = args["sport" .. i]

        if notEmpty(sport) then
            local team = args["team" .. i]
            local icon = args["icon" .. i]
            local image = args["image" .. i]

            local file = resolveFile(icon, image, sport)

            local forceGender = closedSports[normalizeSport(sport)]

            local label, overflow = buildLabel(args, i, sport, team, currentTitle, forceGender, threshold)

            if overflow then
                wide = true
            end

            table.insert(cells,
                string.format(
                    '<span class="skin-invert">[[File:%s|30px|link=]]</span><br />%s',
                    file,
                    label
                )
            )
        end
    end

    return buildTable(cells, wide)
end

function p.renderClosedSidebar(frame)
    local parent = frame:getParent()
    local args = parent.args

    local threshold = 15

    local max = findMaxIndex(args, "closedsport")
    local cells = {}
    local wide = false

    local openSports = collectSports(args, "sport")

    for i = 1, max do
        local sport = args["closedsport" .. i]

        if notEmpty(sport) then
            local team = args["closedteam" .. i]
            local icon = args["closedicon" .. i]
            local image = args["closedimage" .. i]

            local file = resolveFile(icon, image, sport)

            local tempArgs = {
                ["mteam" .. i] = args["closedmteam" .. i],
                ["wteam" .. i] = args["closedwteam" .. i] or args["closedfteam" .. i]
            }

            local forceGender = openSports[normalizeSport(sport)]

            local label, overflow = buildLabel(tempArgs, i, sport, team, nil, forceGender, threshold)

            if overflow then
                wide = true
            end

            table.insert(cells,
                string.format(
                    '<span class="skin-invert">[[File:%s|30px|link=]]</span><br />%s',
                    file,
                    label
                )
            )
        end
    end

    if #cells == 0 then
        return ""
    end

    local tableContent = buildTable(cells, wide)

    local titlestyle = "font-size:125%; background-color:" .. (args.bgcolor or "") .. "; color:" .. (args.textcolor or "") .. ";"

    if notEmpty(args.bordercolor) then
        local border = frame:expandTemplate{
            title = "box-shadow border",
            args = {
                "a",
                args.bordercolor,
                args.borderwidth or "1px"
            }
        }
        titlestyle = titlestyle .. " " .. border .. ";"
    end

    return frame:expandTemplate{
        title = "sidebar",
        args = {
            titlestyle = titlestyle,
            title = (args.closedtitle or "Closed departments of") .. ' ' ..
                frame:expandTemplate{ title = 'nowrap', args = { args.clubname } },
            contentstyle = "padding-top:0.5em;",
            content1 = tableContent,
            fullwidth = "y"
        }
    }
end

return p

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.

  1. 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:
  2. 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.
  3. 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.
  4. 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.
  5. Responsible use. Any risk arising from the use of information from this website is entirely the responsibility of the user.