You are here

mathjax.module in MathJax: LaTeX for Drupal 7

MathJax module.

File

mathjax.module
View source
<?php

/**
 * @file
 * MathJax module.
 */

/**
 * Default setting for CDN url for MathJax. Can be overridden in the settings.
 */
function mathjax_default_cdn_url() {
  return 'https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML';
}

/**
 * Default config string for MathJax parsing. Can be overridden in the settings.
 *
 * @return string 
 */
function mathjax_default_config_string() {
  return "\nMathJax.Hub.Config({\n  extensions: ['tex2jax.js'],\n  jax: ['input/TeX','output/HTML-CSS'],\n  tex2jax: {\n    inlineMath: [ ['\$','\$'], ['\\\\(','\\\\)'] ],\n    processEscapes: true\n  }\n});\n";
}

/**
 * Implements hook_help().
 */
function mathjax_help($path, $arg) {
  switch ($path) {

    // Main module help for the mathjax module.
    case 'admin/settings/mathjax':
      return t('MathJax allows you to include mathematics in your web pages, either using TeX and LaTeX notation, or as MathML, and you can even use both in the same document. Go to the <a href="@url">MathJax website</a> for more information.', array(
        '@url' => url('http://www.mathjax.org/'),
      ));
    case 'admin/help#mathjax':
      return '<p>' . t('MathJax allows you to include mathematics in your web pages, either using TeX and LaTeX notation, or as MathML, and you can even use both in the same document. Go to the <a href="@url">MathJax website</a> for more information.', array(
        '@url' => url('http://www.mathjax.org/'),
      )) . '</p>';
  }
}

/**
 * Implements hook_init().
 */
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');
      }
    }
  }
}

/**
 * Verify that MathJax should be active for the current URL.
 *
 * (taken from Block module)
 *
 * @return bool
 *   TRUE if MathJax should be active for the current page.
 */
function mathjax_active() {
  $pages = variable_get('mathjax_pages', "admin*\nnode/add/*\nnode/*/edit");
  $path = drupal_strtolower(drupal_get_path_alias($_GET['q']));

  // Compare the lowercase internal and lowercase path alias (if any).
  $page_match = drupal_match_path($path, $pages);
  if ($path != $_GET['q']) {
    $page_match = $page_match || drupal_match_path($_GET['q'], $pages);
  }
  if (variable_get('mathjax_active_type', 'disable') == 'disable') {
    return !$page_match;
  }
  else {
    return $page_match;
  }
}

/**
 * Implements hook_permission().
 */
function mathjax_permission() {
  return array(
    'administer mathjax' => array(
      'title' => t('Administer MathJax'),
    ),
  );
}

/**
 * Implements hook_menu().
 */
function mathjax_menu() {
  $items['admin/config/content/mathjax'] = array(
    'title' => 'MathJax',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'mathjax_global_settings',
    ),
    'access arguments' => array(
      'administer mathjax',
    ),
    'description' => 'Configure global settings for MathJax.',
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

/**
 * Configure global settings for MathJax.
 */
function mathjax_global_settings() {
  $form['mathjax']['mathjax_enabled'] = array(
    '#type' => 'checkbox',
    '#title' => t('Global switch'),
    '#default_value' => variable_get('mathjax_enabled', TRUE),
    '#description' => t('Check this box to enable MathJax for the entire site.'),
  );
  $form['mathjax']['mathjax_use_cdn'] = array(
    '#type' => 'checkbox',
    '#title' => t('Use MathJax Content Delivery Network (CDN)'),
    '#default_value' => variable_get('mathjax_use_cdn', TRUE),
    '#description' => t('Check this box to load MathJax source from MathJax servers (recommended) or from the link you can provide below.'),
  );
  $form['mathjax']['mathjax_cdn_url'] = array(
    '#type' => 'textfield',
    '#title' => t('MathJax CDN url'),
    '#default_value' => variable_get('mathjax_cdn_url', mathjax_default_cdn_url()),
    '#description' => t("Enter the Mathjax CDN url here or leave it unchanged to use the one provided by <a target='_blank' href='@mathjax-homepage'>www.mathjax.org</a>.", array(
      '@mathjax-homepage' => 'http://www.mathjax.org',
    )),
  );
  $form['mathjax']['mathjax_config_string'] = array(
    '#type' => 'textarea',
    '#title' => t('MathJax configuration'),
    '#default_value' => variable_get('mathjax_config_string', mathjax_default_config_string()),
    '#description' => t("Enter a javascript configuration string as documented on  <a target='_blank' href='@mathjax-help'>MathJax help</a> Use with caution as you may introduce javascript errors.", array(
      '@mathjax-help' => 'http://docs.mathjax.org/en/latest/',
    )),
  );
  $form['mathjax']['active'] = array(
    '#type' => 'fieldset',
    '#title' => t('Page specific activation settings'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
  );
  $form['mathjax']['active']['mathjax_active_type'] = array(
    '#type' => 'radios',
    '#title' => t('Enable MathJax on specific pages'),
    '#options' => array(
      'disable' => 'Enable on every page except the listed pages.',
      'enable' => 'Enable on the listed pages only.',
    ),
    '#default_value' => variable_get('mathjax_active_type', 'disable'),
  );
  $form['mathjax']['active']['mathjax_pages'] = array(
    '#type' => 'textarea',
    '#title' => t('Pages'),
    '#default_value' => variable_get('mathjax_pages', "admin*\nnode/add/*\nnode/*/edit"),
    '#description' => t("Enter one page per line as Drupal paths. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array(
      '%blog' => 'blog',
      '%blog-wildcard' => 'blog/*',
      '%front' => '<front>',
    )),
  );
  return system_settings_form($form);
}

Functions

Namesort descending Description
mathjax_active Verify that MathJax should be active for the current URL.
mathjax_default_cdn_url Default setting for CDN url for MathJax. Can be overridden in the settings.
mathjax_default_config_string Default config string for MathJax parsing. Can be overridden in the settings.
mathjax_global_settings Configure global settings for MathJax.
mathjax_help Implements hook_help().
mathjax_init Implements hook_init().
mathjax_menu Implements hook_menu().
mathjax_permission Implements hook_permission().