Module:Sandbox/The JoTS/Lyrics

-- WARNING -- This module is not final, but will be utilized on the Daft Punk Wiki. -- Make sure to move to Module:Lyrics (or similar) when finalized.

local HEADERCELL = "\n! style='width:1%;text-align:left;' |\n" local ENDROW = "\n|-\n"

local cols   = 1 -- current num of columns, as determined through createHeader local maxCols = 1 -- the max number of columns throughout lyrics

-- Formatting functions local grayElem = function(text) return mw.html.create("span") :css("color", "#666") :wikitext('(' .. text .. ')') end

local function createHeader(line) if not line then return end cols = 0 local text = '' for section in mw.text.gsplit(line, "/") do       text = text .. HEADERCELL .. section cols = cols + 1 end maxCols = math.max(maxCols, cols) return text .. ENDROW end

local function createRow(line) if not line then return end local nItems = 1 -- number of items in row local row = line:gsub("([/(&])(.-)[/)&]", function(token, backing)        if token == "/" then            nItems = nItems + 1            return " || width=1% | " .. ( (backing:match("^%(.-%)$")) -- for /(lyrics)/ and tostring(grayElem(backing:sub(2,-2))) or backing ) --     elseif token == "(" then --         return tostring(grayElem(backing)) elseif token == "&" then return "style='text-align:center;' " .. "colspan=" .. tostring(cols) .. " | " .. backing end end)

maxCols = math.max(maxCols, nItems)

return (maxCols > 1 and "| " or "| colspan=$MAXCOLS | ") .. row .. ENDROW end

local function createSpacer return "| " .. ENDROW end

-- Invokable module return { main = function(frame) local args = frame.args local lyrics = assert(args.lyrics, '"lyrics" param not passed') local formatted = "{|\n" for line in mw.text.gsplit(lyrics, "\n") do           formatted = formatted ..(createHeader(line:match("^;(.+)"))               or createRow(line:match("^%s*%S.+"))                or createSpacer            ) end if args.debug then return " " .. formatted .. "\n|}\nMAXCOLS=" .. tostring(maxCols) .. " "       else return formatted:gsub("$MAXCOLS", maxCols) .. "\n|}" end end, }