Module:Sandbox/Frietjes

This is an old revision of this page, as edited by Frietjes (talk | contribs) at 18:28, 31 January 2018. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
-- CFB schedule table conversion
-- {{subst:#invoke:CFB schedule/convert|table|...}}
local p = {}

local function splitargs(s)
	s = '♣MARK♣' .. s
	i = 0
	while mw.ustring.find(s, '♣MARK♣') and (i < 100) do
		i = i + 1
		s=mw.ustring.gsub(s,'(♣MARK♣)%s*%|%s*([^%|{}%[%]=%s]*)%s*=%s*', '♦♦%2 = %1')
		s=mw.ustring.gsub(s,'(♣MARK♣)([^%|{}%[%]=]*)', '%2%1')
		s=mw.ustring.gsub(s,'(♣MARK♣)(%{%{[^%{%}]*%}%})', '%2%1')
		s=mw.ustring.gsub(s,'(♣MARK♣)(%[%[[^%[%]]*%]%])', '%2%1')
		s=mw.ustring.gsub(s,'(♣MARK♣)(%[[^%[%]]*%])', '%2%1')
		s=mw.ustring.gsub(s,'♣MARK♣$', '')
	end
	local res = {}
	for t in mw.text.gsplit(s, '%s*♦♦%s*') do
		local k = mw.ustring.gsub(t, '^([^=]-)%s*=%s*(.-)%s*$', '%1')
		local v = mw.ustring.gsub(t, '^([^=]-)%s*=%s*(.-)%s*$', '%2')
		if k ~= t then
			res[k] = v
		end
	end
	return res
end

local headers = {
	time = true,
	rank = true,
	ranklink = true,
	tv = true,
	attend = false
}

local footers = {
	otherevent = nil,
	poll = nil,
	timezone = nil
}
		
local function parseHeader(s)
	local args = splitargs(s)
	if args['time'] and args['time'] == 'no' then
		headers['time'] = false
	end
	if args['rank'] and args['rank'] == 'no' then
		headers['rank'] = false
	end
	if args['ranklink'] and args['ranklink'] == 'no' then
		headers['ranklink'] = false
	end
	if args['tv'] and args['tv'] == 'no' then
		headers['tv'] = false
	end
	if args['attend'] and args['attend'] == 'yes' then
		headers['attend'] = true
	end
end

local function parseFooter(s)
	local args = splitargs(s)
	if args['other-event'] and args['other-event'] ~= '' then
		footers['otherevent'] = args['other-event']
	end
	if args['poll'] and args['poll'] ~= '' then
		footers['poll'] = args['poll']
	end
	if args['timezone'] and args['timezone'] ~= '' then
		footers['timezone'] = args['timezone']
		if mw.ustring.find(footers['timezone'], 'Eastern') then 
			footers['timezone'] = 'Eastern'
		end
		if mw.ustring.find(footers['timezone'], 'Central') then 
			footers['timezone'] = 'Central'
		end
		if mw.ustring.find(footers['timezone'], 'Mountain') then 
			footers['timezone'] = 'Mountain'
		end
		if mw.ustring.find(footers['timezone'], 'Pacific') then 
			footers['timezone'] = 'Pacific'
		end
	end
end

local function parseEntry(s)
	local args = splitargs(s)
	local res = ''
	res = res .. '| ' .. (args['date'] or '')
	if args['time'] and args['time'] == 'no' then
	else
		res = res .. '| ' .. (args['time'] or '')
	end
	res = res .. '| '
	if args['away'] and args['away'] ~= '' then
		res = res .. 'at '
	end
	if args['neutral'] and args['neutral'] ~= '' then
		res = res .. 'vs '
	end
	res = res .. '|'
	if args['opprank'] and args['opprank'] ~= '' then
		res = res .. '#' .. args['opprank']
	end
	res = res .. ' ' .. (args['opponent'] or '')
	if args['nonconf'] and args['nonconf'] == 'yes' then
		res = res .. '<ncg>'
	end
	if args['homecoming'] and args['homecoming'] == 'yes' then
		res = res .. '<hc>'
	end
	res = res .. (args['ref'] or '')
	if args['rank'] and args['rank'] == 'no' then
	else
		res = res .. '| ' .. (args['rank'] or '')
	end
	res = res .. '| ' .. (args['site_stadium'] or '')
	if args['site_cityst'] and args['site_cityst'] ~= '' then
	else
		if args['gamename'] and args['gamename'] ~= '' then
			res = res .. ' (' .. args['gamename'] .. ')'
		end
	end
	res = res .. '| ' .. (args['site_cityst'] or '')
	if args['site_cityst'] and args['site_cityst'] ~= '' then
		if args['gamename'] and args['gamename'] ~= '' then
			res = res .. ' (' .. args['gamename'] .. ')'
		end
	end
	if args['tv'] and args['tv'] == 'no' then
	else
		res = res .. '| ' .. (args['tv'] or '')
	end
	res = res .. '|' .. (args['w/l'] or '') .. ' ' .. (args['score'] or '')
	if args['overtime'] and args['overtime'] ~= '' then
		res = res .. '<sup>' .. args['overtime'] .. '</sup>'
	end
	if args['attend'] then
		res = res .. '| ' .. args['attend']
	end
	return res .. '\n'
end

function p.table(frame)
	local res = ''
	for t in mw.text.gsplit(frame.args[1] or '', '{{[_%s]*CFB[_%s]*Schedule[_%s]*') do
		local s = t:match( '^%s*(.-)%s*$' )
		if mw.ustring.sub(s,-2) == '}}' then
			s = mw.ustring.sub(s,1,-3)
			if mw.ustring.sub(s,1,5) == 'Entry' then
				res = res .. parseEntry(mw.ustring.sub(s,6))
			elseif mw.ustring.sub(s,1,5) == 'Start' then
				parseHeader(mw.ustring.sub(s,6))
			elseif mw.ustring.sub(s,1,3) == 'End' then
				parseFooter(mw.ustring.sub(s,4))
			else
				res = res .. t
			end
		else
			res = res .. t
		end
	end
	local head = '{{CFB schedule\n'
	if headers['time'] == true then
		head = head .. '| time = y\n'
	end
	head = head .. '| atvs = y\n'
	if headers['rank'] == true then
		head = head .. '| rank = y\n'
	end
	if headers['ranklink'] == true then
		head = head .. '| ranklink = y\n'
	end
	if headers['tv'] == true then
		head = head .. '| tv = y\n'
	end
	if headers['attend'] == true then
		head = head .. '| attend = y\n'
	end
	if footers['poll'] then
		head = head .. '| poll = ' .. footers['poll'] .. '\n'
	end
	if footers['otherevent'] then
		head = head .. '| other-event = ' .. footers['otherevent'] .. '\n'
	end
	if footers['timezone'] then
		head = head .. '| timezone = ' .. footers['timezone'] .. '\n'
	end
	
	return head .. res .. '}}'
end
return p