jsMath2jax.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. /* -*- Mode: Javascript; indent-tabs-mode:nil; js-indent-level: 2 -*- */
  2. /* vim: set ts=2 et sw=2 tw=80: */
  3. /*************************************************************
  4. *
  5. * MathJax/extensions/jsMath2jax.js
  6. *
  7. * Implements a jsMath to Jax preprocessor that locates jsMath-style
  8. * <SPAN CLASS="math">...</SPAN> and <DIV CLASS="math">...</DIV> tags
  9. * and replaces them with SCRIPT tags for processing by MathJax.
  10. * (Note: use the tex2jax preprocessor to convert TeX delimiters or
  11. * custom delimiters to MathJax SCRIPT tags. This preprocessor is
  12. * only for the SPAN and DIV form of jsMath delimiters).
  13. *
  14. * To use this preprocessor, include "jsMath2jax.js" in the extensions
  15. * array in your config/MathJax.js file, or the MathJax.Hub.Config() call
  16. * in your HTML document.
  17. *
  18. * ---------------------------------------------------------------------
  19. *
  20. * Copyright (c) 2010-2013 The MathJax Consortium
  21. *
  22. * Licensed under the Apache License, Version 2.0 (the "License");
  23. * you may not use this file except in compliance with the License.
  24. * You may obtain a copy of the License at
  25. *
  26. * http://www.apache.org/licenses/LICENSE-2.0
  27. *
  28. * Unless required by applicable law or agreed to in writing, software
  29. * distributed under the License is distributed on an "AS IS" BASIS,
  30. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  31. * See the License for the specific language governing permissions and
  32. * limitations under the License.
  33. */
  34. MathJax.Extension.jsMath2jax = {
  35. version: "2.2",
  36. config: {
  37. preview: "TeX" // Set to "none" to prevent preview strings from being inserted
  38. // or to an array that specifies an HTML snippet to use for
  39. // the preview.
  40. },
  41. PreProcess: function (element) {
  42. if (!this.configured) {
  43. this.config = MathJax.Hub.CombineConfig("jsMath2jax",this.config);
  44. if (this.config.Augment) {MathJax.Hub.Insert(this,this.config.Augment)}
  45. if (typeof(this.config.previewTeX) !== "undefined" && !this.config.previewTeX)
  46. {this.config.preview = "none"} // backward compatibility for previewTeX parameter
  47. this.previewClass = MathJax.Hub.config.preRemoveClass;
  48. this.configured = true;
  49. }
  50. if (typeof(element) === "string") {element = document.getElementById(element)}
  51. if (!element) {element = document.body}
  52. var span = element.getElementsByTagName("span"), i;
  53. for (i = span.length-1; i >= 0; i--)
  54. {if (String(span[i].className).match(/(^| )math( |$)/)) {this.ConvertMath(span[i],"")}}
  55. var div = element.getElementsByTagName("div");
  56. for (i = div.length-1; i >= 0; i--)
  57. {if (String(div[i].className).match(/(^| )math( |$)/)) {this.ConvertMath(div[i],"; mode=display")}}
  58. },
  59. ConvertMath: function (node,mode) {
  60. if (node.getElementsByTagName("script").length === 0) {
  61. var parent = node.parentNode,
  62. script = this.createMathTag(mode,node.innerHTML);
  63. if (node.nextSibling) {parent.insertBefore(script,node.nextSibling)}
  64. else {parent.appendChild(script)}
  65. if (this.config.preview !== "none") {this.createPreview(node)}
  66. parent.removeChild(node);
  67. }
  68. },
  69. createPreview: function (node) {
  70. var preview = this.config.preview;
  71. if (preview === "TeX") {preview = [this.filterPreview(node.innerHTML)]}
  72. if (preview) {
  73. preview = MathJax.HTML.Element("span",{className: MathJax.Hub.config.preRemoveClass},preview);
  74. node.parentNode.insertBefore(preview,node);
  75. }
  76. },
  77. createMathTag: function (mode,tex) {
  78. tex = tex.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&");
  79. var script = document.createElement("script");
  80. script.type = "math/tex" + mode;
  81. MathJax.HTML.setScript(script,tex);
  82. return script;
  83. },
  84. filterPreview: function (tex) {return tex}
  85. };
  86. MathJax.Hub.Register.PreProcessor(["PreProcess",MathJax.Extension.jsMath2jax],8);
  87. MathJax.Ajax.loadComplete("[MathJax]/extensions/jsMath2jax.js");