Module:RequirementBox: Difference between revisions

No edit summary
Tag: Reverted
No edit summary
 
(11 intermediate revisions by the same user not shown)
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


     local out = ""
     local blocks = {}
    local current = nil


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


        -- Only process bullet lines
         if line:sub(1,1) == "*" then
         if line:sub(1, 1) == "*" then
             -- start new requirement block
             local item = mw.text.trim(line:sub(2))
            if current then
                table.insert(blocks, current)
            end
            current = mw.text.trim(line:sub(2))
        elseif current then
            -- continuation of previous requirement
            current = current .. "\n" .. line
        end
    end


            if item:find("||OR||", 1, true) then
    if current then
                -- OR requirement
        table.insert(blocks, current)
                local parts = mw.text.split(item, "||OR||", true)
    end
                out = out .. '<li class="vp-requirement-or">'
 
                for i, part in ipairs(parts) do
    local out = ""
                    out = out .. '<div class="vp-requirement-or-option">' ..
 
                        mw.text.trim(part) .. '</div>'
    for _, block in ipairs(blocks) do
                    if i < #parts then
        if block:find("%-%-%-OR%-%-%-") then
                        out = out .. '<div class="vp-requirement-or-divider">OR</div>'
            local parts = mw.text.split(block, "%-%-%-OR%-%-%-")
                    end
            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
            out = out .. '</li>'
        else
            out = out .. '<li>' .. block .. '</li>'
         end
         end
     end
     end
Line 41: Line 56:
</div>]], title, out)
</div>]], title, out)
end
end
function p.render(frame)
    local args = frame.args
    local title    = "Invalid Game"
    local game    = args.game or ""
    if game == "vp1" then
        title = "Viva Piñata"
    elseif game == "tip" or game == "vp2" then
        title = "Viva Piñata: Trouble in Paradise"
    elseif game == "pp" then
        title = "Viva Piñata: Pocket Paradise"   
    elseif game == "all" then
        title = "All games"   
    end
    local state    = args.state or ""
    local evolve_first = args.evolve_first 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 "")
    local mature = frame:preprocess(args.mature or "")
    -- build requirements HTML
    local html = ""
    if evolve_first == "true" then
        html = html .. renderCategory("Evolve", evolve)
    end
    html = html .. renderCategory("Appear", appear)
    html = html .. renderCategory("Unblock", unblock)
    html = html .. renderCategory("Bait", bait)
    html = html .. renderCategory("Visit", visit)
    html = html .. renderCategory("Resident", resident)
    if evolve_first ~= "true" then
        html = html .. renderCategory("Evolve", evolve)
    end
    html = html .. renderCategory("Romance", romance)
    html = html .. renderCategory("Tricks", tricks)
    html = html .. renderCategory("Growth", mature)
    -- 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

Latest revision as of 23:09, 14 January 2026

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

local p = {}

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

    local blocks = {}
    local current = nil

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

        if line:sub(1,1) == "*" then
            -- start new requirement block
            if current then
                table.insert(blocks, current)
            end
            current = mw.text.trim(line:sub(2))
        elseif current then
            -- continuation of previous requirement
            current = current .. "\n" .. line
        end
    end

    if current then
        table.insert(blocks, current)
    end

    local out = ""

    for _, block in ipairs(blocks) do
        if block:find("%-%-%-OR%-%-%-") then
            local parts = mw.text.split(block, "%-%-%-OR%-%-%-")
            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
            out = out .. '<li>' .. block .. '</li>'
        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



function p.render(frame)
    local args = frame.args
    local title    = "Invalid Game"
    local game     = args.game or ""
    if game == "vp1" then
        title = "Viva Piñata"
    elseif game == "tip" or game == "vp2" then
        title = "Viva Piñata: Trouble in Paradise"
    elseif game == "pp" then
        title = "Viva Piñata: Pocket Paradise"    
    elseif game == "all" then
        title = "All games"    
    end

    local state    = args.state or ""
    local evolve_first = args.evolve_first 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 "")
    local mature = frame:preprocess(args.mature or "")

    -- build requirements HTML
    local html = ""
    if evolve_first == "true" then
        html = html .. renderCategory("Evolve", evolve)
    end
    html = html .. renderCategory("Appear", appear)
    html = html .. renderCategory("Unblock", unblock)
    html = html .. renderCategory("Bait", bait)
    html = html .. renderCategory("Visit", visit)
    html = html .. renderCategory("Resident", resident)
    if evolve_first ~= "true" then
        html = html .. renderCategory("Evolve", evolve)
    end
    html = html .. renderCategory("Romance", romance)
    html = html .. renderCategory("Tricks", tricks)
    html = html .. renderCategory("Growth", mature)


    -- 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