Documentation for this module may be created at Module:Lyrics/doc
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 = "! width=1% | "
for section in mw.text.gsplit(line, "/") do
text = text .. section .. " !! width=1% | "
cols = cols + 1
end
maxCols = math.max(maxCols, cols)
return text:sub(1, -16) .. 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 "<nowiki>"
.. formatted
.. "\n|}\nMAXCOLS="
.. tostring(maxCols)
.. "</nowiki>"
else
return formatted:gsub("$MAXCOLS", maxCols) .. "\n|}"
end
end,
}