Module:Mctxt

local getArgs = require('Module:Arguments').getArgs

local p = {}

function p.raw( frame ) local args = getArgs(frame) return p._raw(args[1], args.size or args[2], args.class, args.style) end

function p._raw( str, size, class, style ) str = str:gsub('\n', '/'):gsub(' ', '/') size = tonumber(size) and (size .. 'px') or size or '12px' local s = str:gsub('\\\\', '%%%%BACKSLASH%%%%'):gsub('\\/', '%%%%FORSLASH%%%%'):gsub('\\&', '%%%%AMPERSAND%%%%') local t = mw.text.split(s, '/') for i, l in ipairs(t) do		-- add trailing &r l = l .. '&r' -- handle colors and formatting while (l:match('&[0-9a-fk-o]')) do			l = l:gsub('&([0-9a-fk-o])(.-)(&[0-9a-fr])', ' %2&r %3', 1) end t[i] = l	end s = table.concat(t, ' ') s = s:gsub('&r', ''):gsub('%%%%BACKSLASH%%%%', '\\\\'):gsub('%%%%FORSLASH%%%%', '\\/'):gsub('%%%%AMPERSAND%%%%', '\\&'):gsub('\\(.)', '%1') return (' %s '):format(class or , size, style or , s) end

function p.dialogue( frame ) local args = getArgs(frame) return p._dialogue(args[1], args.size or args[2], args.class, args.style) end

function p._dialogue( str, size, class, style ) str = str:gsub('\n', '/'):gsub(' ', '/') size = size and (tonumber(size) and (size .. 'px') or size) or nil local s = str:gsub('\\\\', '%%%%BACKSLASH%%%%'):gsub('\\/', '%%%%FORSLASH%%%%'):gsub('\\&', '%%%%AMPERSAND%%%%') local t = mw.text.split(s, '/') local add_category = '' for i, l in ipairs(t) do		-- handle modifiers and add trailing &r local availableMods = 'cr' -- put all available modifiers here local pttn, lineclass = '^$%[[' .. availableMods .. ']+%]', 		local modifiers = l:match(pttn)		if modifiers then			if modifiers:match('c') then lineclass = 'centertxt' end			if modifiers:match('r') then lineclass = 'righttxt' end			l = l:gsub(pttn, )		end		l = l:gsub('^%s*$', '%%%%AMPERSAND%%%%nbsp;')		l = ' ' .. l .. '&r '		-- handle colors and formatting		while (l:match('&[0-9a-fk-o]')) do			l = l:gsub('&([0-9a-fk-o])(.-)(&[0-9a-fr])', ' %2&r %3', 1)		end		t[i] = l	end	s = table.concat(t)	s = s:gsub('&r', ):gsub('%%%%BACKSLASH%%%%', '\\\\'):gsub('%%%%FORSLASH%%%%', '\\/'):gsub('%%%%AMPERSAND%%%%', '\\&'):gsub('\\(.)', '%1')	return (' %s '):format(		class or , (size and ('font-size:' .. size) or ) .. (style or ), s	) .. add_category end

return p