Module:Sandbox/trappist the monk/math

require('strict'); -- $3987^{12} + 4365^{12} = 4472^{12}$ 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 ' math-title has $$...$$ markup '; 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 ' |math-title= missing TeX delimiters '; elseif 0 ~= count % 2 then return ' |math-title= has unbalanced TeX delimiters '; 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, '$%2$ ');	-- 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: '		}); 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 ' math-title has $$...$$ markup '; 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 ' |math-title= missing TeX delimiters '; 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, '$%2$ ');	-- 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: '		}); 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 {'', ''},														-- remove any html tags (undefined) {'(]*)class *= *"[^"]*"([^>]*>)', '%1%2'},						-- remove class attributes from tags		{'(]*)style *= *"[^"]*"([^>]*>)', '%1%2'},						-- remove style attributes from tags {'(]*)title *= *"[^"]*"([^>]*>)', '%1%2'},						-- remove title attributes from 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 (' (.-) ', '%1'); end

return span_str; end

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