AlbumDonut
From RammWiki
More actions
Documentation for this module may be created at Module:AlbumDonut/doc
local p = {}
-- album definitions
local albums = {
{ key = "Herzeleid", link = "Herzeleid (album)", label = "Herzeleid" },
{ key = "Sehnsucht", link = "Sehnsucht (album)", label = "Sehnsucht" },
{ key = "Mutter", link = "Mutter (album)", label = "Mutter" },
{ key = "Reise, Reise", link = "Reise, Reise (album)", label = "Reise, Reise" },
{ key = "Rosenrot", link = "Rosenrot (album)", label = "Rosenrot" },
{ key = "Liebe ist für alle da", link = "Liebe ist für alle da (album)", label = "Liebe ist für alle da" },
{ key = "Untitled", link = "Rammstein (album)", label = "Untitled album" },
{ key = "Zeit", link = "Zeit (album)", label = "Zeit" },
{ key = "Skills in Pills", link = "Skills in Pills (album)", label = "Skills in Pills" },
{ key = "F und M", link = "F & M (album)", label = "F & M" },
{ key = "Zunge", link = "Zunge (album)", label = "Zunge" },
{ key = "Other", link = nil, label = "Other" },
}
-- helpers
local function albumOf(frame, song)
return frame:expandTemplate{ title = "AlbumList", args = { song } }
end
local function albumColor(frame, key)
return frame:expandTemplate{ title = "AlbumColor", args = { key } }
end
local function legendRow(frame, album, count)
local color = albumColor(frame, album.key)
local cellLink
if album.key == "Untitled" then
-- not italic
if album.link then
cellLink = string.format("[[%s|%s]]", album.link, album.label)
else
cellLink = album.label
end
elseif album.key == "Other" then
-- lowercase and not italic
cellLink = "other"
elseif album.link then
-- default: italic with wikilink
cellLink = string.format("[[%s|<i>%s</i>]]", album.link, album.label)
else
-- default: italic plain text
cellLink = "<i>" .. album.label .. "</i>"
end
local toggleId = mw.ustring.gsub(album.key, "%s+", "")
local outSongs = {}
for i=1,25 do
local song = frame.args["Song"..i]
if song and song ~= "" then
local assigned = albumOf(frame, song)
if assigned == album.key then
table.insert(outSongs, "<li><i>"..song.."</i></li>")
end
end
end
local row = mw.html.create("tr")
row:tag("td")
:css("width", "6px")
:css("background-color", color)
row:tag("td")
:css("padding-left", "0.5em")
:css("padding-right", "0.5em")
:wikitext(cellLink)
row:tag("td")
:wikitext(string.format(
'<div class="mw-customtoggle-%s">( <span style="color:var(--color-progressive)">%d</span> )</div>',
toggleId, count
))
row:tag("td")
:wikitext(string.format(
'<div class="mw-collapsible mw-collapsed" id="mw-customcollapsible-%s">' ..
'<div class="mw-collapsible-content"><ul><small>%s</small></ul></div></div>',
toggleId, table.concat(outSongs, "\n")
))
return tostring(row)
end
function p.render(frame)
local slices, legendRows = {}, {}
-- count songs per album
local counts = {}
for _,album in ipairs(albums) do
counts[album.key] = 0
end
for i=1,25 do
local song = frame.args["Song"..i]
if song and song ~= "" then
local assigned = albumOf(frame, song)
if counts[assigned] ~= nil then
counts[assigned] = counts[assigned] + 1
else
counts["Other"] = counts["Other"] + 1
end
end
end
-- build slices + legend
for _,album in ipairs(albums) do
local count = counts[album.key]
if count > 0 then
local color = albumColor(frame, album.key)
local link = album.link or ""
table.insert(slices, string.format("(%d:%s:%s:%s)", count, album.label, color, link))
table.insert(legendRows, legendRow(frame, album, count))
end
end
-- Chart (call Module:Chart directly)
local chartModule = require("Module:Chart")
local chart = chartModule["pie chart"]{
args = {
["radius"] = "100",
["inner percent"] = "70",
["slices"] = table.concat(slices, " ")
}
}
-- Legend built with mw.html
local legend = mw.html.create("table")
legend:css("margin-top", "0.5em")
legend:wikitext(table.concat(legendRows, "\n"))
return tostring(mw.html.create("div")
:css("padding", "1em")
:wikitext(chart .. tostring(legend)))
end
return p