Module:Sandbox/trappist the monk/math

Source: Wikipedia, the free encyclopedia.
require('strict');
-- <math display=inline>3987^{12} + 4365^{12} = 4472^{12}</math>
local function math_test (frame)
	local math_title = frame.args['math-title'];
	if math_title:find ('\127[^\127]*UNIQ%-%-math%-[%a%d]+%-QINU[^\127]*\127') then
		return '<span style="color:#d33">math-title has <math>...</math> markup</span>';
	end
	
	local math_t = {};															-- temporary holding spot for math strip markers
	local escaped = '__35c4p3d__';												-- the special secret EScApEd keyword
	local str = math_title:gsub ('\\%$', escaped);								-- replace '\$' (escaped $) with special secret EScApEd keyword

	local count;
	str, count = str:gsub ('%$', '%1');											-- there must be $-delimited text and the delimters must be balanced
	if 0 == count then
		return '<span style="color:#d33">|math-title= missing TeX delimiters</span>';
	elseif 0 ~= count % 2 then
		return '<span style="color:#d33">|math-title= has unbalanced TeX delimiters</span>';
	end
	
	local pattern = '(%$([^%$]+)%$)';											-- pattern used to find $-delimited math text
	for math_str in str:gmatch (pattern) do										-- walk through the text and for each $-delimited math text
		math_str = math_str:gsub (pattern, '<math display="inline">%2</math>');	-- replace the delimiters
		math_str = math_str:gsub (escaped, '\\%$');								-- replace special secret EScApEd keyword with '\$'
		math_str = frame:preprocess (math_str);									-- preprocess math text into a math strip marker
		table.insert (math_t, math_str);										-- and save the math strip marker
	end

	for _, stripmarker in ipairs (math_t) do
		str = str:gsub (pattern, stripmarker, 1);								-- replace $-delimited math text with matching strip marker
	end
	str = str:gsub (escaped, '%$');												-- replace special secret EScApEd keyword with unescaped form '$'
	
	return table.concat ({
		'math title: ',
		str,
		'; metadata: <code>',
		math_title,
		'</code>'
		});
end



local function math_test2 (frame)
	local math_title = frame.args['math-title'];
	if math_title:find ('\127[^\127]*UNIQ%-%-math%-[%a%d]+%-QINU[^\127]*\127') then
		return '<span style="color:#d33">math-title has <math>...</math> markup</span>';
	end
	
	local math_t = {};															-- temporary holding spot for math strip markers
	local str = math_title;
	local pattern = '(\\%((.-)\\%))';											-- pattern used to find \(...\) delimited math text

	if not str:find (pattern) then												-- there must be \(...\) delimited text and the delimters must be balanced
		return '<span style="color:#d33">|math-title= missing TeX delimiters</span>';
	end

	for math_str in str:gmatch (pattern) do										-- walk through the text and for each \(...\) delimited math text
		math_str = math_str:gsub (pattern, '<math display="inline">%2</math>');	-- replace the delimiters
		math_str = frame:preprocess (math_str);									-- preprocess math text into a math strip marker
		table.insert (math_t, math_str);										-- and save the math strip marker
	end

	for _, stripmarker in ipairs (math_t) do
		str = str:gsub (pattern, stripmarker, 1);								-- replace \(...\) delimited math text with matching strip marker
	end

	return table.concat ({
		'math title: ',
		str,
		'; metadata: <code>',
		math_title,
		'</code>'
		});
end


local function span_test (frame)
	local span_str = frame.args[1];
	
	local patterns_t = {
		{'\127[^\127]*UNIQ%-%-(%a+)%-[%a%d]+%-QINU[^\127]*\127', ''},			-- remove any stripmarkers
		{'<br */*>', ''},														-- remove any <br /> html tags ({{chem2}})
		{'(<span [^>]*)class *= *"[^"]*"([^>]*>)', '%1%2'},						-- remove class attributes from <span ...> tags
		{'(<span [^>]*)style *= *"[^"]*"([^>]*>)', '%1%2'},						-- remove style attributes from <span ...> tags
		{'(<span [^>]*)title *= *"[^"]*"([^>]*>)', '%1%2'},						-- remove title attributes from <span ...> tags
		{' +>', '>'},															-- remove trailing spaces in the tag
		{' +', ' '},															-- remove redundant spaces
		}
		
	for _, pattern_t in ipairs (patterns_t) do
		span_str = span_str:gsub (pattern_t[1], pattern_t[2]);
	end

	local count = 1;
	while 0 ~= count do
		span_str, count = span_str:gsub ('<span>(.-)</span>', '%1');
	end

	return span_str;
end

return 
	{
	math_test = math_test,														-- $...$
	math_test2 = math_test2,													-- \(...\)
	span_test = span_test,
	}