jsMath2jax.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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-2017 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.7.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 previewClass = MathJax.Hub.config.preRemoveClass;
  71. var preview = this.config.preview;
  72. if (preview === "none") return;
  73. if ((node.previousSibling||{}).className === previewClass) return;
  74. if (preview === "TeX") {preview = [this.filterPreview(node.innerHTML)]}
  75. if (preview) {
  76. preview = MathJax.HTML.Element("span",{className:previewClass},preview);
  77. node.parentNode.insertBefore(preview,node);
  78. }
  79. },
  80. createMathTag: function (mode,tex) {
  81. tex = tex.replace(/&lt;/g,"<").replace(/&gt;/g,">").replace(/&amp;/g,"&");
  82. var script = document.createElement("script");
  83. script.type = "math/tex" + mode;
  84. MathJax.HTML.setScript(script,tex);
  85. return script;
  86. },
  87. filterPreview: function (tex) {return tex}
  88. };
  89. // We register the preprocessors with the following priorities:
  90. // - mml2jax.js: 5
  91. // - jsMath2jax.js: 8
  92. // - asciimath2jax.js, tex2jax.js: 10 (default)
  93. // See issues 18 and 484 and the other *2jax.js files.
  94. MathJax.Hub.Register.PreProcessor(["PreProcess",MathJax.Extension.jsMath2jax],8);
  95. MathJax.Ajax.loadComplete("[MathJax]/extensions/jsMath2jax.js");