Jump to content

Module:Sandbox/trappist the monk/math

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Trappist the monk (talk | contribs) at 23:34, 14 September 2021. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
require('Module:No globals');
-- <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 pattern = '(%$([^%$]+)%$)';											-- pattern used to find $ delimited math text
	
	for math_str in str:gmatch (pattern) do										-- loop across 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

return 
	{
	math_test = math_test,
	}