Module:Books

From Candypedia

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

local p = {}

-- Define the book groups and their entries
p.groups = {
    {
        name = "Volumes",
        list = {
            "Bittersweet Candy Bowl: Volume One|Volume One",
            "Bittersweet Candy Bowl: Starting Over|Starting Over (Volume Two)",
            "Bittersweet Candy Bowl: Volume Three|Volume Three",
            "Bittersweet Candy Bowl: Volume Four|Volume Four",
            "Bittersweet Candy Bowl: Volume Five|Volume Five",
            "Bittersweet Candy Bowl: Volume Six|Volume Six",
            "Bittersweet Candy Bowl: Volume Seven|Volume Seven",
        }
    },
    {
        name = "Other books",
        list = {
            "Bittersweet Candy Bowl: Omnibus 1|Omnibus 1",
            "Ask Roseville High (book)|Ask Roseville High",
        }
    }
}

-- Render a collapsible navbox for the books
function p.renderNav(frame)
    local state = frame.args.state or "autocollapse"
    local args = {
        "{{Navbox",
        "| name      = Books",
        "| title     = Books",
        "| state     = " .. state,
        "| listclass = hlist",
    }

    for i, grp in ipairs(p.groups) do
        table.insert(args, "| group" .. i .. " = " .. grp.name)
        local items = {}
        for _, entry in ipairs(grp.list) do
            local parts = mw.text.split(entry, "|", true)
            local target = parts[1]
            local label  = parts[2] or parts[1]
            table.insert(items, "* [[" .. target .. "|" .. label .. "]]")
        end
        table.insert(args, "| list" .. i .. " =\n" .. table.concat(items, "\n"))
    end

    table.insert(args, "}}")
    return frame:preprocess(table.concat(args, "\n"))
end

-- Render a plain list of books grouped into sections
function p.renderList(frame)
    local out = {}
    for _, grp in ipairs(p.groups) do
        table.insert(out, "== " .. grp.name .. " ==\n")
        table.insert(out, '<div style="column-count:3; column-gap:2em;">\n')
        for _, entry in ipairs(grp.list) do
            local parts = mw.text.split(entry, "|", true)
            local target = parts[1]
            local label  = parts[2] or parts[1]
            table.insert(out, "* [[" .. target .. "|" .. label .. "]]\n")
        end
        table.insert(out, "</div>\n")
    end
    return table.concat(out, "\n")
end

return p