AlbumChart
From RammWiki
More actions
Documentation for this module may be created at Module:AlbumChart/doc
local p = {}
-- Albums we care about (in order)
local albums = {
{ key = "Herzeleid", link = "Herzeleid (album)" },
{ key = "Sehnsucht", link = "Sehnsucht (album)" },
{ key = "Mutter", link = "Mutter (album)" },
{ key = "Reise, Reise", link = "Reise, Reise (album)" },
{ key = "Rosenrot", link = "Rosenrot (album)" },
{ key = "Liebe ist für alle da", link = "Liebe ist für alle da (album)" },
{ key = "Untitled", link = "Rammstein (album)", label = "Untitled album" },
{ key = "Zeit", link = "Zeit (album)" },
{ key = "Skills in Pills", link = "Skills in Pills (album)" },
{ key = "F und M", link = "F & M (album)" },
{ key = "Zunge", link = "Zunge (album)" },
{ key = "Other", link = nil, label = "Other" },
}
-- Helper: run a parser function
local function pf(frame, name, args)
return frame:callParserFunction{ name = name, args = args }
end
-- Get album color once
local function albumColor(frame, key)
return frame:expandTemplate{ title = "AlbumColor", args = { key } }
end
-- Build chart + legend
function p.albumChart(frame)
local out = {}
local slices = {}
local legend = {}
-- loop albums
for _,a in ipairs(albums) do
local count = tonumber(pf(frame, "#count", { pf(frame,"#argmap",{"AlbumRow||Song"}), a.key })) or 0
if count > 0 then
-- resolve color only once
local color = albumColor(frame, a.key)
local name = a.label or a.key
local link = a.link or ""
-- slice for chart
table.insert(slices, string.format("(%d:%s:%s:%s)", count, name, color, link))
-- legend row
local linkText = a.link and ("[[" .. a.link .. "|" .. (a.label or a.key) .. "]]") or (a.label or a.key)
local toggleId = mw.ustring.gsub(a.key, "%s+", "") -- collapse ID without spaces
table.insert(legend, string.format([[
|-
| style="width:6px;background-color:%s" |
| style="padding-left:0.5em;padding-right:0.5em;" | <i>%s</i>
| <div class="mw-customtoggle-%s">( <span style="color:var(--color-progressive)">%d</span> )</div>
| <div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-%s">
<div class="mw-collapsible-content"><ul><small>
%s
</small></ul></div></div>
]], color, linkText, toggleId, count, toggleId, p.listSongs(frame, a.key)))
end
end
-- render chart (donut)
table.insert(out, frame:expandTemplate{
title = "#invoke:Chart|pie chart",
args = {
["radius"] = "100",
["inner percent"] = "70",
["slices"] = table.concat(slices, " ")
}
})
-- render legend
table.insert(out, "{|\n" .. table.concat(legend, "\n") .. "\n|}")
return table.concat(out, "\n")
end
-- Build <li> list of songs belonging to an album
function p.listSongs(frame, album)
local out = {}
for i = 1, 25 do
local song = frame.args["Song" .. i]
if song and song ~= "" then
local assigned = pf(frame, "AlbumList", { song })
if assigned == album then
table.insert(out, "<li><i>" .. song .. "</i></li>")
end
end
end
return table.concat(out, "\n")
end
return p