You are here

function mathjax_init in MathJax: LaTeX for Drupal 7

Same name and namespace in other branches
  1. 6 mathjax.module \mathjax_init()

Implements hook_init().

File

./mathjax.module, line 49
MathJax module.

Code

function mathjax_init() {
  global $base_url;
  global $base_path;

  // Load Mathjax only on specified nodes.
  if (mathjax_active() && variable_get('mathjax_enabled', TRUE)) {
    $config = array(
      '#type' => 'markup',
      '#markup' => '<script type="text/x-mathjax-config">' . variable_get('mathjax_config_string', mathjax_default_config_string()) . '</script>',
    );
    drupal_add_html_head($config, 'mathjax-config');
    drupal_add_js(drupal_get_path('module', 'mathjax') . '/mathjax.js');
    if (variable_get('mathjax_use_cdn', TRUE)) {
      $cdn_url = variable_get('mathjax_cdn_url', mathjax_default_cdn_url());
      drupal_add_js($cdn_url, 'external');
    }
    else {

      // Use the local library.
      $library = DRUPAL_ROOT . $base_path . libraries_get_path('mathjax') . '/MathJax.js';
      if (file_exists($library)) {
        drupal_add_js($base_path . libraries_get_path('mathjax') . '/MathJax.js?config=TeX-AMS-MML_HTMLorMML', 'external');
      }
      else {

        // Throw an error.
        drupal_set_message('MathJax is configured to load from a local library, but could not find the files. Check the module settings.', 'error');
      }
    }
  }
}