enclose.js 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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/TeX/enclose.js
  6. *
  7. * Implements the \enclose macros, which give access from TeX to the
  8. * <menclose> tag in the MathML that underlies MathJax's internal format.
  9. *
  10. * Usage:
  11. *
  12. * \enclose{notation}{math} % enclose math using given notation
  13. * \enclose{notation,notation,...}{math} % enclose with several notations
  14. * \enclose{notation}[attributes]{math} % enclose with attributes
  15. *
  16. * ---------------------------------------------------------------------
  17. *
  18. * Copyright (c) 2011-2013 The MathJax Consortium
  19. *
  20. * Licensed under the Apache License, Version 2.0 (the "License");
  21. * you may not use this file except in compliance with the License.
  22. * You may obtain a copy of the License at
  23. *
  24. * http://www.apache.org/licenses/LICENSE-2.0
  25. *
  26. * Unless required by applicable law or agreed to in writing, software
  27. * distributed under the License is distributed on an "AS IS" BASIS,
  28. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  29. * See the License for the specific language governing permissions and
  30. * limitations under the License.
  31. */
  32. MathJax.Extension["TeX/enclose"] = {
  33. version: "2.2",
  34. //
  35. // The attributes allowed in \enclose{notation}[attributes]{math}
  36. //
  37. ALLOWED: {
  38. arrow: 1,
  39. color: 1, mathcolor: 1,
  40. background: 1, mathbackground: 1,
  41. padding: 1,
  42. thickness: 1
  43. }
  44. };
  45. MathJax.Hub.Register.StartupHook("TeX Jax Ready",function () {
  46. var TEX = MathJax.InputJax.TeX,
  47. MML = MathJax.ElementJax.mml,
  48. ALLOW = MathJax.Extension["TeX/enclose"].ALLOWED;
  49. //
  50. // Set up macro
  51. //
  52. TEX.Definitions.Add({macros: {enclose: 'Enclose'}},null,true);
  53. TEX.Parse.Augment({
  54. //
  55. // Implement \enclose{notation}[attr]{math}
  56. // (create <menclose notation="notation">math</menclose>)
  57. //
  58. Enclose: function(name) {
  59. var notation = this.GetArgument(name),
  60. attr = this.GetBrackets(name),
  61. math = this.ParseArg(name);
  62. var def = {notation: notation.replace(/,/g," ")};
  63. if (attr) {
  64. attr = attr.replace(/ /g,"").split(/,/);
  65. for (var i = 0, m = attr.length; i < m; i++) {
  66. var keyvalue = attr[i].split(/[:=]/);
  67. if (ALLOW[keyvalue[0]]) {
  68. keyvalue[1] = keyvalue[1].replace(/^"(.*)"$/,"$1");
  69. if (keyvalue[1] === "true") {keyvalue[1] = true}
  70. if (keyvalue[1] === "false") {keyvalue[1] = false}
  71. def[keyvalue[0]] = keyvalue[1];
  72. }
  73. }
  74. }
  75. this.Push(MML.menclose(math).With(def));
  76. }
  77. });
  78. MathJax.Hub.Startup.signal.Post("TeX enclose Ready");
  79. });
  80. MathJax.Ajax.loadComplete("[MathJax]/extensions/TeX/enclose.js");