Module:RequirementBox: Difference between revisions

From Piñata Journal
No edit summary
No edit summary
Tag: Reverted
Line 1: Line 1:
local p = {}
local function renderCategory(title, list)
local function renderCategory(title, list)
     if list == "" then return "" end
     if list == "" then return "" end


     -- Split list into individual <li> blocks
    local out = ""
    local items = {}
 
     for li in list:gmatch("<li>(.-)</li>") do
     -- Split by lines
         table.insert(items, li)
     for line in list:gmatch("[^\r\n]+") do
    end
         line = mw.text.trim(line)


    local out = ""
        -- Only process bullet lines
        if line:sub(1, 1) == "*" then
            local item = mw.text.trim(line:sub(2))


    for _, item in ipairs(items) do
            if item:find("||OR||", 1, true) then
        if item:find("||OR||") then
                -- OR requirement
            -- OR requirement
                local parts = mw.text.split(item, "||OR||", true)
            local parts = mw.text.split(item, "%s*||OR||%s*")
                out = out .. '<li class="vp-requirement-or">'
            out = out .. '<li class="vp-requirement-or">'
                for i, part in ipairs(parts) do
            for i, part in ipairs(parts) do
                    out = out .. '<div class="vp-requirement-or-option">' ..
                out = out .. '<div class="vp-requirement-or-option">' .. part .. '</div>'
                        mw.text.trim(part) .. '</div>'
                if i < #parts then
                    if i < #parts then
                    out = out .. '<div class="vp-requirement-or-divider">OR</div>'
                        out = out .. '<div class="vp-requirement-or-divider">OR</div>'
                    end
                 end
                 end
                out = out .. '</li>'
            else
                -- Normal requirement
                out = out .. '<li>' .. item .. '</li>'
             end
             end
            out = out .. '</li>'
        else
            -- Normal requirement
            out = out .. '<li>' .. item .. '</li>'
         end
         end
     end
     end
    if out == "" then return "" end


     return string.format([[
     return string.format([[
Line 38: Line 41:
</div>]], title, out)
</div>]], title, out)
end
end
function p.render(frame)
    local args = frame.args
    local title    = args.title or "Game"
    local game    = args.game or ""
    local state    = args.state or ""
    -- preprocess each category to allow raw wikitext
    local appear  = frame:preprocess(args.appear or "")
    local visit    = frame:preprocess(args.visit or "")
    local resident = frame:preprocess(args.resident or "")
    local romance  = frame:preprocess(args.romance or "")
    local tricks  = frame:preprocess(args.tricks or "")
    local unblock = frame:preprocess(args.unblock or "")
    local evolve = frame:preprocess(args.evolve or "")
    local bait = frame:preprocess(args.bait or "")
    -- build requirements HTML
    local html = renderCategory("Appear", appear)
    html = html .. renderCategory("Visit", visit)
    html = html .. renderCategory("Resident", resident)
    html = html .. renderCategory("Romance", romance)
    html = html .. renderCategory("Tricks", tricks)
    html = html .. renderCategory("Unblock", unblock)
    html = html .. renderCategory("Evolve", evolve)
    html = html .. renderCategory("Bait", bait)
    -- decide whether <details> is open
    local openAttr = ""
    if state ~= "mw-collapsed" then
        openAttr = ' open="open"'
    end
    -- final details box
    local details = string.format([[
<details class="vp-gamebox"%s data-game="%s">
  <summary class="vp-gamebox-header">%s</summary>
  <div class="vp-gamebox-content">
%s
  </div>
</details>]], openAttr, game, title, html)
    -- <<< RETURN RAW HTML >>>
    return frame:preprocess(details)
end
return p

Revision as of 17:18, 5 January 2026

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

local function renderCategory(title, list)
    if list == "" then return "" end

    local out = ""

    -- Split by lines
    for line in list:gmatch("[^\r\n]+") do
        line = mw.text.trim(line)

        -- Only process bullet lines
        if line:sub(1, 1) == "*" then
            local item = mw.text.trim(line:sub(2))

            if item:find("||OR||", 1, true) then
                -- OR requirement
                local parts = mw.text.split(item, "||OR||", true)
                out = out .. '<li class="vp-requirement-or">'
                for i, part in ipairs(parts) do
                    out = out .. '<div class="vp-requirement-or-option">' ..
                        mw.text.trim(part) .. '</div>'
                    if i < #parts then
                        out = out .. '<div class="vp-requirement-or-divider">OR</div>'
                    end
                end
                out = out .. '</li>'
            else
                -- Normal requirement
                out = out .. '<li>' .. item .. '</li>'
            end
        end
    end

    if out == "" then return "" end

    return string.format([[
<div class="vp-requirements-category">
  <div class="requirements-header">%s</div>
  <ul class="vp-requirements-list">
%s
  </ul>
</div>]], title, out)
end