Module:Track list: Difference between revisions

From Sekaipedia
Content added Content deleted
mNo edit summary
(initialize before accessing)
Line 20: Line 20:
if col and idx then
if col and idx then
return {
return {
['col'] = col,
['column'] = col,
['idx'] = idx
['index'] = idx
}
}
end
end
Line 40: Line 40:
if col_idx then
if col_idx then
local col = col_idx['col']
local col = col_idx['column']
local idx = col_idx['idx']
local idx = col_idx['index']
rows[idx] = v
if rows[idx] then
rows[idx][col] = v
else
rows[idx] = {
[col] = v
}
end
end
end
end
end

Revision as of 06:47, 17 October 2021

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 x = ''
	
	local args = getArgs(frame)
	local rows = {}
	local root = mw.html.create()
	
	for k,v in pairs(args) do
		local col_idx = extract(k)
		x = x .. ',' .. mw.text.jsonEncode(col_idx)
		
		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] = {
					[col] = v
				}
			end
		end
	end
	
	local root_table = root:tag('table')
		:addClass('wikitable')
		:css({
			['margin-bottom'] = 0
		})
		:tag('tr')
			:tag('th'):wikitext('No.'):done()
			:tag('th'):wikitext('Title'):done()
			:tag('th'):wikitext('Singer(s)'):done()
			:tag('th'):wikitext('Length'):done()
			:done()
			
	for k,v in pairs(rows) do
		local no = k
		local title = v['title']
		local singer = v['singer']
		local length = v['length']
		
		root_table:tag('tr')
			:tag('th'):wikitext('1'):done()
			:tag('td'):wikitext(title):done()
			:tag('td'):wikitext(singer):done()
			:tag('td')
				:css({
					['text-align'] = 'center'
				})
				:wikitext(length)
				:done()
	end
	
	root_table:tag('tr')
		:tag('th')
			:attr('colspan', 3)
			:css({
				['text-align'] = 'right'
			})
			:wikitext('Total length:')
			:done()
		:tag('th')
			:wikitext()
			:done()
	
	root:tag('sup')
			:wikitext('[[Template:Track List|View]] • [[Template talk:Track List|Talk]]')
			:done()
		:done()
	
	return tostring(root) .. x .. mw.text.jsonEncode(rows)
end

return p
Cookies help us deliver our services. By using our services, you agree to our use of cookies.