Module:KnownRecordings: Difference between revisions
No edit summary Tag: Reverted |
Tags: Undo Reverted |
||
Line 8: | Line 8: | ||
duplicate = "#cccccc", -- Grey | duplicate = "#cccccc", -- Grey | ||
unknown = nil -- No color (no background) | unknown = nil -- No color (no background) | ||
} | |||
-- Define the fixed column order | |||
local columnOrder = { | |||
"performers", -- Performer(s) | |||
"title", -- Title | |||
"filename", -- Filename | |||
"date", -- Date of recording | |||
"location", -- Recording location | |||
"length", -- Length | |||
"appearedOn", -- Appeared on | |||
"notes", -- Notes | |||
"status" -- Status (for color) | |||
} | } | ||
Line 18: | Line 31: | ||
-- Dynamically generate rows until no more data is found | -- Dynamically generate rows until no more data is found | ||
while args[index] do | while args[index] do | ||
local rowData = {} | local rowData = {} | ||
-- | -- Loop over the column order and assign data to the corresponding field | ||
for i, column in ipairs(columnOrder) do | |||
rowData[column] = args[index] or "" -- Get data for each column | |||
index = index + 1 -- Move to the next piece of data | |||
end | end | ||
-- Extract | -- Extract each column value from the rowData table | ||
local performers = rowData | local performers = rowData.performers or "" | ||
local title = rowData | local title = rowData.title or "" | ||
local filename = rowData | local filename = rowData.filename or "" | ||
local date = rowData | local date = rowData.date or "" | ||
local location = rowData | local location = rowData.location or "" | ||
local length = rowData | local length = rowData.length or "" | ||
local appearedOn = rowData | local appearedOn = rowData.appearedOn or "" | ||
local notes = rowData | local notes = rowData.notes or "" | ||
local status = rowData.status or "unknown" -- Default to "unknown" if no status is provided | |||
local status = rowData | |||
-- Determine the background color based on the status | |||
local color = statusColors[status:lower()] or statusColors["unknown"] -- Use the statusColors map | |||
-- Wrap filename with <samp> for monospace display | -- Wrap filename with <samp> for monospace display | ||
local filenameFormatted = string.format('<samp>%s</samp>', filename) | local filenameFormatted = string.format('<samp>%s</samp>', filename) | ||
Line 52: | Line 59: | ||
local row = string.format( | local row = string.format( | ||
'<tr%s><td>%s</td><td>%s</td><td style="white-space: nowrap;">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>', | '<tr%s><td>%s</td><td>%s</td><td style="white-space: nowrap;">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>', | ||
color and (' style="background-color:' .. color .. ';"') or "", -- Apply | color and (' style="background-color:' .. color .. ';"') or "", -- Apply color if it exists | ||
performers, title, filenameFormatted, date, location, length, appearedOn, notes | performers, title, filenameFormatted, date, location, length, appearedOn, notes | ||
) | ) | ||
table.insert(rows, row) | table.insert(rows, row) | ||
end | end | ||
Revision as of 12:50, 27 November 2024
Documentation for this module may be created at Module:KnownRecordings/doc
local p = {}
-- Define the color map based on the status
local statusColors = {
released = "#aaffaa", -- Green
unreleased = "#ffaaaa", -- Red
["partially released"] = "#ffffaa", -- Yellow
duplicate = "#cccccc", -- Grey
unknown = nil -- No color (no background)
}
-- Define the fixed column order
local columnOrder = {
"performers", -- Performer(s)
"title", -- Title
"filename", -- Filename
"date", -- Date of recording
"location", -- Recording location
"length", -- Length
"appearedOn", -- Appeared on
"notes", -- Notes
"status" -- Status (for color)
}
-- Main function to render the table
function p.renderTable(frame)
local args = frame:getParent().args
local rows = {}
local index = 1 -- Start with the first row
-- Dynamically generate rows until no more data is found
while args[index] do
local rowData = {}
-- Loop over the column order and assign data to the corresponding field
for i, column in ipairs(columnOrder) do
rowData[column] = args[index] or "" -- Get data for each column
index = index + 1 -- Move to the next piece of data
end
-- Extract each column value from the rowData table
local performers = rowData.performers or ""
local title = rowData.title or ""
local filename = rowData.filename or ""
local date = rowData.date or ""
local location = rowData.location or ""
local length = rowData.length or ""
local appearedOn = rowData.appearedOn or ""
local notes = rowData.notes or ""
local status = rowData.status or "unknown" -- Default to "unknown" if no status is provided
-- Determine the background color based on the status
local color = statusColors[status:lower()] or statusColors["unknown"] -- Use the statusColors map
-- Wrap filename with <samp> for monospace display
local filenameFormatted = string.format('<samp>%s</samp>', filename)
-- Create table row with background color and no-wrap for the filename column
local row = string.format(
'<tr%s><td>%s</td><td>%s</td><td style="white-space: nowrap;">%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',
color and (' style="background-color:' .. color .. ';"') or "", -- Apply color if it exists
performers, title, filenameFormatted, date, location, length, appearedOn, notes
)
table.insert(rows, row)
end
-- Create table header
local header = [[
<tr>
<th>Performer(s)</th>
<th>Title</th>
<th>Filename</th>
<th>Date of recording</th>
<th>Recording location</th>
<th>Length</th>
<th>Appeared on</th>
<th>Notes</th>
</tr>
]]
-- Add caption below the table using MediaWiki-compatible syntax
local caption = mw.text.tag(
'caption',
{ style = 'caption-side: bottom; text-align: right;' },
mw.text.tag(
'small',
nil,
mw.text.tag(
'i',
nil,
'[[' .. 'Weezerpedia:Known recordings|about this table' .. ']]'
)
)
)
-- Combine header, caption, and rows into a styled table
return string.format(
'<table class="wikitable" style="margin:0.5em; font-size:.8em;">%s%s%s</table>',
caption,
header,
table.concat(rows)
)
end
return p