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 22:07, 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 str = math_title:gsub ('\\%$', '__35c4p3d__');						-- replace '\$' (escaped $) with special secret keyword (35c4p3d -> EScApEd)
	for math_str in str:gmatch ('%$[^%$]-%$') do								-- loop across the text and for each $ delimited math text
		math_str = math_str:gsub ('%$([^%$]-)%$', '<math display=inline>%1</math>');	-- replace the delimiters
		math_str = math_str:gsub ('__35c4p3d__', '\\%$');						-- replace special secret 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 ('%$([^%$]-)%$', stripmarker, 1);						-- replace $ delimited math text with matching strip marker
	end
	str = str:gsub ('__35c4p3d__', '%$');										-- replace special secret keyword with unescaped form '$'
	
	return table.concat ({
		'math title: ',
		str,
		'; metadata: <code>',
		math_title,
		'</code>'
		});
end

return 
	{
	math_test = math_test,
	}