--- jsmath.module	2008-07-11 05:20:26.000000000 -0700
+++ /usr/clients/www/alexis.m2osw.com/public_html/animals/sites/all/modules/jsmath/jsmath.module	2008-11-17 23:01:02.000000000 -0800
@@ -1,6 +1,24 @@
 <?php
 /* $Id: jsmath.module,v 1.7 2008/07/11 12:20:26 delius Exp $ */
 
+
+define('JSMATH_DEFAULT_DELIMITERS', "'[math]','[/math]','[display]','[/display]'");
+
+/**
+ * Get the current delimiters
+ *
+ * @param[in] $no_quotes   set to true to get rid of the ' characters
+ *
+ * @return the delimiters to use with jsMath
+ */
+function _jsmath_delims($no_quotes = false) {
+  $delims = variable_get('jsmath_customDelimiters', JSMATH_DEFAULT_DELIMITERS);
+  if ($no_quotes) {
+    $delims = str_replace("'", "", $delims);
+  }
+  return $delims;
+}
+
 /**
  * Produce the form with the jsMath settings
  */
@@ -163,7 +181,7 @@
   $form['jsmath_customDelimiters'] = array(
     '#type' => 'textfield',
     '#title' => t('customDelimiters'),
-    '#default_value' => variable_get('jsmath_customDelimiters', "'[math]','[/math]','[display]','[/display]'"),
+    '#default_value' => _jsmath_delims(),
     '#size' => 80,
     '#maxlength' => 80,
     '#description' => t("This array holds four strings that are used to start and stop in-line and 
@@ -172,7 +190,9 @@
       This is because those characters are used to indicate tags and entities within HTML, and since 
       tex2math doesn't run until after the page has been interpreted by the browser, these character 
       are no longer part of the text that is available to tex2math when it processes the page. 
-      In particular, that means you can't make delimiters that look like normal HTML tags."
+      In particular, that means you can't make delimiters that look like normal HTML tags.<br/><br/>
+      The default delimiters are !delims",
+      array("!delims", JSMATH_DEFAULT_DELIMITERS)
     )
   );
 
@@ -240,11 +260,17 @@
 
 
 /**
- * Implementation of hook_init()
- *
  * Writes javascript into the page header for loading jsMath
  */
-function jsmath_init() {
+function _jsmath_insert() {
+  static $inserted = false;
+
+  // we want to insert it once only
+  if($inserted) {
+    return;
+  }
+  $inserted = true;
+
   // Create javascript for setting jsMath options and loading jsMath
   $jsmath_path = base_path() . drupal_get_path('module', 'jsmath') . '/jsMath';
   $load = "<script type=\"text/javascript\">
@@ -262,7 +288,7 @@
       fixEscapedDollars: ". variable_get('jsmath_processSingleDollars', 1) .",
       doubleDollarsAreInLine: 0,
       allowDisableTag: 0,
-      customDelimiters: [". variable_get('jsmath_customDelimiters', "'[math]','[/math]','[display]','[/display]'") ."],
+      customDelimiters: [". _jsmath_delims() ."],
       allowDoubleClicks: ". variable_get('jsmath_allowDoubleClicks', 1) .",
       showFontWarnings: ". variable_get('jsmath_showFontWarnings', 1) .",
       method:  \"". variable_get('jsmath_method', 'Process') ."\",
@@ -298,3 +324,103 @@
 
   return $items;
 }
+
+/**
+ * Implementation of hook_help()
+ *
+ * Describe the filter
+ */
+function jsmath_help($section) {
+  switch ($section) {
+  case 'admin/modules#description':
+    // This description is shown in the listing at admin/modules.
+    return t('Remove unless paragraph often added by WYSIWIG editors.');
+
+  }
+}
+
+/**
+ * Implementation of hook_filter_tips()
+ *
+ * Describe the fitler
+ */
+function jsmath_filter_tips($delta, $format, $long = FALSE) {
+  switch ($delta) {
+  case 0:
+    if ($long) {
+      return t('This filter parses the [math]...[/math] tags in your nodes.');
+    }
+    else {
+      return t('Parse [math]...[/math] tags.');
+    }
+    break;
+
+  }
+}
+
+/**
+ * Implementation of hook_filter().
+ *
+ * The filter itself.
+ */
+function jsmath_filter($op, $delta = 0, $format = -1, $text = '') {
+  // The "list" operation provides the module an opportunity to declare both how
+  // many filters it defines and a human-readable name for each filter. Note that
+  // the returned name should be passed through t() for translation.
+  if ($op == 'list') {
+    return array(
+      0 => t('Replace jsMath tags with nicely formatted formulae'),
+    );
+  }
+
+  $delims = _jsmath_delims();
+  $delims = str_replace("'", "", $delims);
+
+  // All operations besides "list" provide a $delta argument so we know which
+  // filter they refer to.
+  switch ($op) {
+  // This description is shown in the administrative interface, unlike the
+  // filter tips which are shown in the content editing interface.
+  case 'description':
+    // should we have a separate description for each filter here?!
+    return t('This replaces the '. $delims .' tags with nicely formatted formulae.');
+
+  // We don't need the "prepare" operation for this filter, but it's required
+  // to at least return the input text as-is.
+  case 'prepare':
+    return $text;
+
+  // Unfortunate, but if the page gets cached then we miss the _jsmath_insert()
+  // call of the process below; so we could help some by having our own cache
+  // feature implemented... I'm not too sure how else we could resolve this one.
+  case 'no cache':
+    return TRUE;
+
+  // The actual filtering is performed here. The supplied text should be
+  // returned, once any necessary substitutions have taken place.
+  case 'process':
+    // TODO: to the minimum we should put the arrays in statics
+    // and better yet, put them in the cache
+    $search = explode(",", $delims);
+    $replace = array();
+    $open = TRUE;
+    foreach($search as $d) {
+      if ($open) {
+        $replace[] = '<span class="jsmath">' . $d;
+      }
+      else {
+        $replace[] = $d . '</span>';
+      }
+      $open = !$open;
+    }
+    $result = str_replace($search, $replace, $text);
+    if ($result != $text) {
+      // we need jsmath in this case
+      _jsmath_insert();
+    }
+    return $result;
+
+  }
+}
+
+
