From 964c250fd919772ef0b0feb6f7d9f5afd444046b Mon Sep 17 00:00:00 2001
From: "Kolier.Li" <kolier.li@gmail.com>
Date: Sun, 24 Jun 2012 08:03:14 +0800
Subject: [PATCH] Compatible with mathjax_trigger.

---
 mathjax.js     |   42 ++++++++++++++++++++++++++++++++++++++++++
 mathjax.module |   53 ++++++++++++-----------------------------------------
 2 files changed, 54 insertions(+), 41 deletions(-)
 create mode 100644 mathjax.js

diff --git a/mathjax.js b/mathjax.js
new file mode 100644
index 0000000..535e05a
--- /dev/null
+++ b/mathjax.js
@@ -0,0 +1,42 @@
+// $Id$
+
+/**
+ * Add MathJax support to HTML using settings variables defined in the module
+ */
+(function($) {
+  Drupal.behaviors.mathjax = {
+    attach : function(context, settings) { // include mathjax only after setting variables are defined
+      if (Drupal.settings.hasOwnProperty('mathjax')) {
+        var mathjax = Drupal.settings.mathjax;
+  
+        // from http://www.mathjax.org/resources/docs/?dynamic.html
+        var script = document.createElement("script");
+        script.type = "text/javascript";
+  
+        if (mathjax.path == 'cdn') {
+          script.src = "https://d3eoax9i5htok0.cloudfront.net/mathjax/1.1-latest/MathJax.js";
+          // script.src  = "http://cdn.mathjax.org/mathjax/latest/MathJax.js"; // unsafe clear connection
+        }
+        else {
+          script.src = mathjax.path;
+        }
+  
+        var config = 'MathJax.Hub.Config({' + 'extensions: ["tex2jax.js"],'
+            + 'jax: ["input/TeX","output/HTML-CSS"],' + 'tex2jax: {'
+            + 'inlineMath: [ [\'$\',\'$\'], [\'\\\\(\',\'\\\\)\'] ],' + // look for $...$ and \(...\) as delimiters for inline math
+            'displayMath: [ [\'$$\',\'$$\'], [\'\\\\[\',\'\\\\]\'] ],' + // look for $$...$$ and \[...\] as delimiters for display math
+            'processEscapes: true' + '}' + '});'
+            + 'MathJax.Hub.Startup.onload();';
+  
+        if (window.opera) {
+          script.innerHTML = config
+        }
+        else {
+          script.text = config
+        }
+  
+        document.getElementsByTagName("head")[0].appendChild(script);
+      }
+    }
+  };
+})(jQuery);
diff --git a/mathjax.module b/mathjax.module
index fce317a..c145332 100644
--- a/mathjax.module
+++ b/mathjax.module
@@ -1,25 +1,6 @@
 <?php
 // $Id$
 
-// Config variables
-global $defaultMathjaxConfigString;
-$defaultMathjaxConfigString = <<<MJ_CFG
-showProcessingMessages: false, 
-messageStyle: "simple",
-jax: ["input/TeX", "output/HTML-CSS"], 
-extensions: ["tex2jax.js","MathMenu.js","MathZoom.js"], 
-TeX: {
-  extensions: ["AMSmath.js","AMSsymbols.js","noErrors.js","noUndefined.js"]
-}, 
-"HTML-CSS": { availableFonts: ["TeX"] },
-tex2jax: {
-  inlineMath: [ ["$","$"], ["\\\\(","\\\\)"] ],
-  displayMath: [ ["$$","$$"], ["\\\\[","\\\\]"] ],
-  processEscapes: true
-}
-MJ_CFG;
-
-
 /**
  * Implementation of hook_help().
  */
@@ -38,23 +19,20 @@ function mathjax_help($path, $arg) {
  * Implementation of hook_init().
  */
 function mathjax_init() {
-  global $base_path, $defaultMathjaxConfigString;
+  global $base_path;
+  
+  $active = mathjax_active() && variable_get('mathjax_enabled', TRUE);
+  drupal_alter('mathjax_active', $active);
 
   // Load Mathjax only on specified nodes
-  if (mathjax_active() && variable_get('mathjax_enabled', TRUE)) {
-    // first configure Mathjax
-    $inline_script = '<script type="text/x-mathjax-config">';
-    $inline_script .= 'MathJax.Hub.Config({' . "\n" . variable_get('mathjax_config_string', $defaultMathjaxConfigString) . "\n" . '});' . "\n";
-    $inline_script .= '</script>';
-    $element = array(
-      '#type' => 'markup',
-      '#markup' => $inline_script,
-    );
-    drupal_add_html_head($element, 'mathjax-config');
-
-    // then load Mathjax
-    $mathjax_path = variable_get('mathjax_use_cdn', TRUE) ? 'https://d3eoax9i5htok0.cloudfront.net/mathjax/1.1-latest/MathJax.js' : $base_path . libraries_get_path('mathjax') . '/MathJax.js';
-    drupal_add_js($mathjax_path, 'external');
+  if ($active) {
+    drupal_add_js(drupal_get_path('module', 'mathjax') . '/mathjax.js', 'file');
+  
+    // Pass variables to mathjax.js
+    $mathjax_path = variable_get('mathjax_use_cdn', TRUE) ? 'cdn' : $base_path . libraries_get_path('mathjax') . '/MathJax.js';
+    drupal_add_js(array(
+      'mathjax' => array(
+        'path' => $mathjax_path)), 'setting');
    }
 }
 
@@ -150,13 +128,6 @@ function mathjax_global_settings() {
     '#default_value' => variable_get('mathjax_use_cdn', TRUE),
     '#description' => t('Check this box to load MathJax source from MathJax servers (recommended).'),
   );
-  global $defaultMathjaxConfigString;
-  $form['mathjax']['mathjax_config_string'] = array(
-    '#type' => 'textarea',
-    '#title' => t('MathJax configuration'),
-    '#default_value' => variable_get('mathjax_config_string', $defaultMathjaxConfigString),
-    '#description' => t("Enter a javascript configuration string as documented on " . l(t("MathJax help"), "http://www.mathjax.org/docs/1.1/options/index.html#configuration") . ". Use with caution as you may introduce javascript errors."),
-  );
   $form['mathjax']['active'] = array(
     '#type' => 'fieldset',
     '#title' => t('Page specific activation settings'),
-- 
1.7.10.msysgit.1

