Content added Content deleted
mNo edit summary |
No edit summary |
||
(45 intermediate revisions by the same user not shown) | |||
Line 5: | Line 5: | ||
local function extract(str) |
local function extract(str) |
||
local col = nil |
local col = nil |
||
local title_col = string. |
local title_col = string.find(str, "^title%d+$") |
||
local singer_col = string. |
local singer_col = string.find(str, "^singer%d+$") |
||
local length_col = string. |
local length_col = string.find(str, "^length%d+$") |
||
local idx = string.match(str, "%d") |
local idx = string.match(str, "(%d+)") |
||
if title_col then |
|||
col = 'title' |
|||
elseif singer_col then |
|||
col = 'singer' |
|||
elseif length_col then |
|||
col = 'length' |
|||
end |
|||
if col and idx then |
if col and idx then |
||
return { |
return { |
||
[' |
['column'] = col, |
||
[' |
['index'] = idx |
||
} |
} |
||
end |
end |
||
Line 23: | Line 29: | ||
function p.main(frame) |
function p.main(frame) |
||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
local rows = {} |
local rows = {} |
||
local total_length = 0 |
|||
local root = mw.html.create() |
local root = mw.html.create() |
||
for k,v in pairs(args) do |
for k,v in pairs(args) do |
||
local col_idx = extract(k) |
local col_idx = extract(k) |
||
x = x .. ',' .. mw.text.jsonEncode(col_idx) |
|||
if col_idx then |
if col_idx then |
||
local col = col_idx[' |
local col = col_idx['column'] |
||
local idx = col_idx[' |
local idx = col_idx['index'] |
||
⚫ | |||
⚫ | |||
if rows[idx] then |
|||
⚫ | |||
else |
|||
rows[idx] = { |
|||
['number'] = tonumber(idx), |
|||
[col] = v |
|||
} |
|||
end |
|||
if col == 'length' then |
|||
local mins, secs = string.match(v, "(%d+):(%d+)") |
|||
total_length = total_length + tonumber(mins) * 60 + tonumber(secs) |
|||
end |
|||
end |
end |
||
end |
end |
||
local root_table = root:tag('table') |
local root_table = root:tag('table') |
||
:addClass(' |
:addClass('tracklist') |
||
:css({ |
:css({ |
||
['margin-bottom'] = 0 |
['margin-bottom'] = 0 |
||
}) |
}) |
||
:tag('tr') |
:tag('tr') |
||
:addClass('tlist-head') |
|||
⚫ | |||
:tag('th' |
:tag('th') |
||
⚫ | |||
⚫ | |||
['text-align'] = 'right' |
|||
⚫ | |||
}) |
|||
⚫ | |||
⚫ | |||
⚫ | |||
:css({ |
|||
['text-align'] = 'left' |
|||
}) |
|||
⚫ | |||
:done() |
|||
:tag('th') |
|||
:css({ |
|||
['text-align'] = 'left' |
|||
}) |
|||
⚫ | |||
:done() |
|||
:tag('th') |
|||
:css({ |
|||
['text-align'] = 'left' |
|||
}) |
|||
⚫ | |||
:done() |
|||
:done() |
:done() |
||
⚫ | |||
⚫ | |||
local sorted_rows = {} |
|||
⚫ | |||
for k,v in pairs(rows) do table.insert(sorted_rows, v) end |
|||
local no = k |
|||
table.sort(sorted_rows, function (t1, t2) |
|||
return t1.number < t2.number |
|||
end) |
|||
⚫ | |||
⚫ | |||
local title = v['title'] |
local title = v['title'] |
||
local singer = v['singer'] |
local singer = v['singer'] |
||
Line 59: | Line 102: | ||
root_table:tag('tr') |
root_table:tag('tr') |
||
:addClass('tlist-song') |
|||
⚫ | |||
⚫ | |||
⚫ | |||
:tag('td') |
:tag('td') |
||
:css({ |
:css({ |
||
['text-align'] = ' |
['text-align'] = 'right' |
||
}) |
|||
:wikitext(no) |
|||
:done() |
|||
:tag('td') |
|||
⚫ | |||
:done() |
|||
:tag('td') |
|||
⚫ | |||
:done() |
|||
:tag('td') |
|||
:css({ |
|||
['text-align'] = 'right' |
|||
}) |
}) |
||
:wikitext(length) |
:wikitext(length) |
||
:done() |
:done() |
||
end |
end |
||
⚫ | |||
local minutes = math.floor(total_length / 60) |
|||
local seconds = total_length % 60 |
|||
root_table:tag('tr') |
root_table:tag('tr') |
||
:tag(' |
:tag('td') |
||
:attr('colspan', 3) |
:attr('colspan', 3) |
||
:css({ |
:css({ |
||
Line 79: | Line 135: | ||
:done() |
:done() |
||
:tag('th') |
:tag('th') |
||
: |
:css({ |
||
['text-align'] = 'right' |
|||
⚫ | |||
}) |
|||
⚫ | |||
:wikitext(string.format( |
|||
⚫ | |||
"%d:%02d", |
|||
:wikitext('[[Template:Track List|View]] • [[Template talk:Track List|Talk]]') |
|||
minutes, |
|||
seconds) |
|||
) |
|||
:done() |
:done() |
||
⚫ | |||
return tostring(root) |
return tostring(root) |
||
end |
end |
||
Latest revision as of 04:27, 11 June 2022
Documentation for this module may be created at Module:Track list/doc
local getArgs = require('Module:Arguments').getArgs
local p = {}
local function extract(str)
local col = nil
local title_col = string.find(str, "^title%d+$")
local singer_col = string.find(str, "^singer%d+$")
local length_col = string.find(str, "^length%d+$")
local idx = string.match(str, "(%d+)")
if title_col then
col = 'title'
elseif singer_col then
col = 'singer'
elseif length_col then
col = 'length'
end
if col and idx then
return {
['column'] = col,
['index'] = idx
}
end
return nil
end
function p.main(frame)
local args = getArgs(frame, { wrappers = 'Template:Track list' })
local rows = {}
local total_length = 0
local root = mw.html.create()
for k,v in pairs(args) do
local col_idx = extract(k)
if col_idx then
local col = col_idx['column']
local idx = col_idx['index']
if rows[idx] then
rows[idx][col] = v
else
rows[idx] = {
['number'] = tonumber(idx),
[col] = v
}
end
if col == 'length' then
local mins, secs = string.match(v, "(%d+):(%d+)")
total_length = total_length + tonumber(mins) * 60 + tonumber(secs)
end
end
end
local root_table = root:tag('table')
:addClass('tracklist')
:css({
['margin-bottom'] = 0
})
:tag('tr')
:addClass('tlist-head')
:tag('th')
:css({
['text-align'] = 'right'
})
:wikitext('No.')
:done()
:tag('th')
:css({
['text-align'] = 'left'
})
:wikitext('Title')
:done()
:tag('th')
:css({
['text-align'] = 'left'
})
:wikitext('Singer(s)')
:done()
:tag('th')
:css({
['text-align'] = 'left'
})
:wikitext('Length')
:done()
:done()
local sorted_rows = {}
for k,v in pairs(rows) do table.insert(sorted_rows, v) end
table.sort(sorted_rows, function (t1, t2)
return t1.number < t2.number
end)
for i,v in ipairs(sorted_rows) do
local no = v['number']
local title = v['title']
local singer = v['singer']
local length = v['length']
root_table:tag('tr')
:addClass('tlist-song')
:tag('td')
:css({
['text-align'] = 'right'
})
:wikitext(no)
:done()
:tag('td')
:wikitext(title)
:done()
:tag('td')
:wikitext(singer)
:done()
:tag('td')
:css({
['text-align'] = 'right'
})
:wikitext(length)
:done()
end
local minutes = math.floor(total_length / 60)
local seconds = total_length % 60
root_table:tag('tr')
:tag('td')
:attr('colspan', 3)
:css({
['text-align'] = 'right'
})
:wikitext('Total length:')
:done()
:tag('th')
:css({
['text-align'] = 'right'
})
:wikitext(string.format(
"%d:%02d",
minutes,
seconds)
)
:done()
return tostring(root)
end
return p