Content added Content deleted
m (actually prevent duplicates) |
mNo edit summary |
||
(8 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
local p = {} |
local p = {} |
||
local |
local tableIdKey = 'lyrics_table_id' |
||
local columnVariableKey = 'lyrics_columns' |
|||
Line 11: | Line 12: | ||
local args = getArgs(frame) |
local args = getArgs(frame) |
||
local |
local previousId = tonumber(VariablesLua.var(tableIdKey)) or 0 |
||
⚫ | |||
local |
local id = args['id'] or tostring(previousId + 1) |
||
local columnsStr = args['columns'] or '' |
|||
local splitColumns = stringToArray(',')(columnsStr) |
|||
local definedColumns = {} |
local definedColumns = {} |
||
local columns = {} |
local columns = {} |
||
Line 22: | Line 25: | ||
if columnTrimmed ~= '' then |
if columnTrimmed ~= '' then |
||
if definedColumns[columnTrimmed] then |
if definedColumns[columnTrimmed] then |
||
error( |
error(string.format( |
||
'Column "%s" already defined', |
|||
columnTrimmed |
|||
⚫ | |||
end |
end |
||
table.insert(columns, columnTrimmed) |
table.insert(columns, columnTrimmed) |
||
definedColumns[columnTrimmed] = true |
definedColumns[columnTrimmed] = true |
||
end |
|||
end |
|||
local labels = {} |
|||
for _,column in ipairs(columns) do |
|||
⚫ | |||
if label and label ~= '' then |
|||
labels[column] = label |
|||
else |
|||
error(string.format( |
|||
'Column "%s" needs a header', |
|||
column |
|||
)) |
|||
end |
end |
||
end |
end |
||
Line 34: | Line 54: | ||
:attr('data-id', id) |
:attr('data-id', id) |
||
:attr('data-languages', table.concat(columns, ',')) |
:attr('data-languages', table.concat(columns, ',')) |
||
:attr('data-labels', mw.text.jsonEncode( |
:attr('data-labels', mw.text.jsonEncode(labels)) |
||
['japanese'] = 'Japanese', |
|||
['romaji'] = 'Romaji', |
|||
['english'] = 'English' |
|||
⚫ | |||
local row = mw.html.create('tr') |
local row = mw.html.create('tr') |
||
Line 46: | Line 62: | ||
:addClass('lyrics-' .. column) |
:addClass('lyrics-' .. column) |
||
:attr('width', math.floor(100 / #columns) .. '%') |
:attr('width', math.floor(100 / #columns) .. '%') |
||
:css('min-width', '150px') |
|||
:wikitext(args[column]) |
:wikitext(args[column]) |
||
end |
end |
||
if tonumber(id) ~= nil then |
|||
⚫ | |||
VariablesLua.vardefine(tableIdKey, id) |
|||
⚫ | |||
end |
|||
⚫ | |||
return '<div>' .. |
|||
⚫ | |||
string.format('<table class="wikitable" id="lyrics-%s">', id) .. |
string.format('<table class="wikitable" id="lyrics-%s">', id) .. |
||
tostring(row) |
tostring(row) |
||
Line 68: | Line 89: | ||
local root = mw.html.create('tr'):addClass('lyrics-row') |
local root = mw.html.create('tr'):addClass('lyrics-row') |
||
local columnOrder = VariablesLua.var( |
local columnOrder = VariablesLua.var(columnVariableKey, '') |
||
local columns = stringToArray(',')(columnOrder) |
local columns = stringToArray(',')(columnOrder) |
||
Line 85: | Line 106: | ||
if args[1] then |
if args[1] then |
||
local columnOrder = VariablesLua.var( |
local columnOrder = VariablesLua.var(columnVariableKey, '') |
||
local columns = stringToArray(',')(columnOrder) |
local columns = stringToArray(',')(columnOrder) |
||
Line 93: | Line 114: | ||
:wikitext(args[1]) |
:wikitext(args[1]) |
||
return tostring(row) .. '</table>' |
return tostring(row) .. '</table>' .. '</div>' |
||
end |
end |
||
Latest revision as of 03:22, 15 April 2023
Documentation for this module may be created at Module:Lyrics/doc
local getArgs = require('Module:Arguments').getArgs
local stringToArray = require('Module:ProcessingFunctions').stringToArray
local VariablesLua = mw.ext.VariablesLua
local p = {}
local tableIdKey = 'lyrics_table_id'
local columnVariableKey = 'lyrics_columns'
function p.head(frame)
local args = getArgs(frame)
local previousId = tonumber(VariablesLua.var(tableIdKey)) or 0
local id = args['id'] or tostring(previousId + 1)
local columnsStr = args['columns'] or ''
local splitColumns = stringToArray(',')(columnsStr)
local definedColumns = {}
local columns = {}
for _,column in ipairs(splitColumns) do
local columnTrimmed = mw.text.trim(column)
if columnTrimmed ~= '' then
if definedColumns[columnTrimmed] then
error(string.format(
'Column "%s" already defined',
columnTrimmed
))
end
table.insert(columns, columnTrimmed)
definedColumns[columnTrimmed] = true
end
end
local labels = {}
for _,column in ipairs(columns) do
local label = args[column]
if label and label ~= '' then
labels[column] = label
else
error(string.format(
'Column "%s" needs a header',
column
))
end
end
local lyricsOptions = mw.html.create('div')
:addClass('lyrics-options')
:attr('data-id', id)
:attr('data-languages', table.concat(columns, ','))
:attr('data-labels', mw.text.jsonEncode(labels))
local row = mw.html.create('tr')
:addClass('lyrics-row')
for _,column in ipairs(columns) do
row:tag('th')
:addClass('lyrics-' .. column)
:attr('width', math.floor(100 / #columns) .. '%')
:css('min-width', '150px')
:wikitext(args[column])
end
if tonumber(id) ~= nil then
VariablesLua.vardefine(tableIdKey, id)
end
VariablesLua.vardefine(columnVariableKey, table.concat(columns, ','))
return '<div>' ..
tostring(lyricsOptions) ..
string.format('<table class="wikitable" id="lyrics-%s">', id) ..
tostring(row)
end
function p.line(frame)
local function formatArg(arg)
if arg and arg ~= '' then
return table.concat(stringToArray('\n')(arg), '<br>')
else
return '<br>'
end
end
local args = getArgs(frame)
local root = mw.html.create('tr'):addClass('lyrics-row')
local columnOrder = VariablesLua.var(columnVariableKey, '')
local columns = stringToArray(',')(columnOrder)
for _,column in ipairs(columns) do
root:tag('td')
:addClass('lyrics-' .. column)
:wikitext(formatArg(args[column])
)
end
return tostring(root)
end
function p.tail(frame)
local args = getArgs(frame)
if args[1] then
local columnOrder = VariablesLua.var(columnVariableKey, '')
local columns = stringToArray(',')(columnOrder)
local row = mw.html.create('tr')
:tag('th')
:attr('colspan', #columns)
:wikitext(args[1])
return tostring(row) .. '</table>' .. '</div>'
end
return '</table>'
end
return p