Module:Sandbox/Frietjes

This is an old revision of this page, as edited by Frietjes (talk | contribs) at 17:32, 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 function parseHeader(frame, s)
	return frame:expandTemplate{title='CFB Schedule Start', args=splitargs(s)}
end

local function parseFooter(frame, s)
	return frame:expandTemplate{title='CFB Schedule End', args=splitargs(s)}
end

local function parseEntry(frame, s)
	return frame:expandTemplate{title='CFB Schedule Entry', args=splitargs(s)}
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(frame,mw.ustring.sub(s,6))
			elseif mw.ustring.sub(s,1,5) == 'Start' then
				res = res .. parseHeader(frame,mw.ustring.sub(s,6))
			elseif mw.ustring.sub(s,1,3) == 'End' then
				res = res .. parseFooter(frame,mw.ustring.sub(s,4))
			else
				res = res .. t
			end
		else
			res = res .. t
		end
	end
	return res
end
return p