User:Proteins/showmathderivations.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
//<pre>
// Script to allow readers to see the derivations of mathematical results
//
// To use this script, add "importScript('User:Proteins/showmathderivations.js');" to your monobook.js subpage 
// under your user page, as you can see at User:Proteins/monobook.js


function showMathDerivations() { 
	var alert_string = "";
	var error_string = "";
	var derivation_string = "";
	var del_derivation_string = "";

	var body_content;

	var span_nodes;
	var temp_span_node;
	var num_span_nodes = 0;
	var span_node_index = 0;

	var temp_derivation;
	var temp_derivation_step;
	var mathematical_derivation_name = ""; 
	var mathematical_derivation_list = new Array();
	var mathematical_derivation_step_list = new Array();

	var derivation_index = 0;
	var num_mathematical_derivations = 0;

	var step_index = 0;
	var num_derivation_steps = 0;


//****************
// Initializations
//****************

	num_mathematical_derivations = 0;
 
// Get the bodyContent node
	body_content = document.getElementById('bodyContent');
	if (!body_content) { 
		error_string = "ERROR: There is no bodyContent node in this article.";
		window.alert(error_string);
		return;
	}
 
	span_nodes = body_content.getElementsByTagName("SPAN");
	if (!span_nodes) { 
		error_string = "ERROR: This page has no SPAN nodes.\n";
		window.alert(error_string);
		return; 
	}
	num_span_nodes = span_nodes.length;
	if (num_span_nodes < 1) { 
		error_string = "ERROR: This page has no SPAN nodes.\n";
		window.alert(error_string);
		return; 
	}
 
	for (span_node_index=0; span_node_index<num_span_nodes; span_node_index++) { 
		temp_span_node = span_nodes[span_node_index];
		if (!temp_span_node) { continue; }
 
		if (temp_span_node.className == "mathematical_derivation") { 
			num_mathematical_derivations++;
			mathematical_derivation_list.push(temp_span_node);
		}
	} // closes loop over the SPAN nodes in the main article
	if (num_mathematical_derivations < 1) { 
		error_string = "No mathematical derivations were found on this page.";
		window.alert(error_string);
		return;
	} else if (num_mathematical_derivations == 1) {
		alert_string = "This page has one mathematical derivation.\n\n"; 
//		window.alert(alert_string);
	} else {
		alert_string = "This page has " + num_mathematical_derivations + " mathematical derivations.\n\n"; 
		alert_string += "These will be analyzed in individual pop-up windows.\n"; 
//		window.alert(alert_string);
	}
 
 
//***************************************************
// Loop over the mathematical derivations on the page
//***************************************************
 
	for (derivation_index=1; derivation_index<=num_mathematical_derivations; derivation_index++) {
		derivation_string = "Derivation " + derivation_index + " (of " + num_mathematical_derivations + " on this page):\n\n"; 

		temp_derivation = mathematical_derivation_list[derivation_index-1];
		if (!temp_derivation) { 
			error_string = "ERROR: Mathematical derivation " + derivation_index + " is undefined.\n";
			window.alert(error_string);
			continue;
		}


// Get the name of the derivation
		mathematical_derivation_name = temp_derivation.title;
		if (!mathematical_derivation_name) { 
			error_string = "ERROR: No name is defined for mathematical derivation " + derivation_index + ".\n";
			window.alert(error_string);
			mathematical_derivation_name = "UNNAMED REACTION";
		}
		derivation_string += mathematical_derivation_name + "\n\n";


// Find the number of steps in the derivation
		span_nodes = temp_derivation.getElementsByTagName("SPAN");
		if (!span_nodes) { 
			error_string = "ERROR: Mathematical derivation " + derivation_index + " has no SPAN nodes.\n";
			window.alert(error_string);
			return; 
		}
		num_span_nodes = span_nodes.length;
		if (num_span_nodes < 1) {
			error_string = "ERROR: Mathematical derivation " + derivation_index + " has zero SPAN nodes.\n"; 
			window.alert(error_string);
			return; 
		}
 
		for (span_node_index=0; span_node_index<num_span_nodes; span_node_index++) { 
			temp_span_node = span_nodes[span_node_index];
			if (!temp_span_node) { continue; }
 
			if (temp_span_node.className == "mathematical_derivation_step") { 
				num_derivation_steps++;
				mathematical_derivation_step_list.push(temp_span_node);
			}
		} // closes loop over the SPAN nodes in this mathematical derivation


// Loop over the steps of the derivation
		for (step_index=0; step_index<num_derivation_steps; step_index++) { 
			temp_derivation_step = mathematical_derivation_step_list[step_index];
			if (!temp_derivation_step) { 
				error_string = "ERROR: Step " + step_index + " in mathematical derivation " + derivation_index + " is undefined.\n";
				window.alert(error_string);
				continue;
			}

			derivation_string += temp_derivation_step.title + ": ";
			del_derivation_string = temp_derivation_step.innerHTML;
			del_derivation_string = del_derivation_string.replace(/<[^>]+>/, "");
			derivation_string += del_derivation_string + "\n\n";
		} // closes loop over derivation steps
		derivation_string += "Q. E. D.";

// Print out the derivation
		window.alert(derivation_string);

 	} // closes loop over mathematical derivations

}  // closes function showMathDerivations()
 
$(function () {
            mw.util.addPortletLink('p-cactions', 'javascript:showMathDerivations()', 'D', 'ca-showmathderiv', 'Show derivations of mathematical results', '', '');
});
 
//</pre>