ConfigMacrosConfiguration.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2019-2022 The MathJax Consortium
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /**
  18. * @fileoverview Configuration file for the configmacros package.
  19. *
  20. * @author dpvc@mathjax.org (Davide P. Cervone)
  21. */
  22. import {Configuration, ParserConfiguration} from '../Configuration.js';
  23. import {expandable} from '../../../util/Options.js';
  24. import {CommandMap, EnvironmentMap} from '../SymbolMap.js';
  25. import ParseMethods from '../ParseMethods.js';
  26. import {Macro} from '../Symbol.js';
  27. import NewcommandMethods from '../newcommand/NewcommandMethods.js';
  28. import {BeginEnvItem} from '../newcommand/NewcommandItems.js';
  29. import {TeX} from '../../tex.js';
  30. type TEX = TeX<any, any, any>;
  31. /**
  32. * The name to use for the macros map
  33. */
  34. const MACROSMAP = 'configmacros-map';
  35. /**
  36. * The name to use for the environment map
  37. */
  38. const ENVIRONMENTMAP = 'configmacros-env-map';
  39. /**
  40. * Create the command map for the macros
  41. *
  42. * @param {Configuration} config The configuration object for the input jax
  43. */
  44. function configmacrosInit(config: ParserConfiguration) {
  45. new CommandMap(MACROSMAP, {}, {});
  46. new EnvironmentMap(ENVIRONMENTMAP, ParseMethods.environment, {}, {});
  47. config.append(Configuration.local({
  48. handler: {
  49. macro: [MACROSMAP],
  50. environment: [ENVIRONMENTMAP]
  51. },
  52. priority: 3
  53. }));
  54. }
  55. /**
  56. * Create the user-defined macros and environments from their options
  57. *
  58. * @param {Configuration} config The configuration object for the input jax
  59. * @param {TeX} jax The TeX input jax
  60. */
  61. function configmacrosConfig(_config: ParserConfiguration, jax: TEX) {
  62. configMacros(jax);
  63. configEnvironments(jax);
  64. }
  65. /**
  66. * Create user-defined macros from the macros option
  67. *
  68. * @param {TeX} jax The TeX input jax
  69. */
  70. function configMacros(jax: TEX) {
  71. const handler = jax.parseOptions.handlers.retrieve(MACROSMAP) as CommandMap;
  72. const macros = jax.parseOptions.options.macros;
  73. for (const cs of Object.keys(macros)) {
  74. const def = (typeof macros[cs] === 'string' ? [macros[cs]] : macros[cs]);
  75. const macro = Array.isArray(def[2]) ?
  76. new Macro(cs, NewcommandMethods.MacroWithTemplate, def.slice(0, 2).concat(def[2])) :
  77. new Macro(cs, NewcommandMethods.Macro, def);
  78. handler.add(cs, macro);
  79. }
  80. }
  81. /**
  82. * Create user-defined environments from the environments option
  83. *
  84. * @param {TeX} jax The TeX input jax
  85. */
  86. function configEnvironments(jax: TEX) {
  87. const handler = jax.parseOptions.handlers.retrieve(ENVIRONMENTMAP) as EnvironmentMap;
  88. const environments = jax.parseOptions.options.environments;
  89. for (const env of Object.keys(environments)) {
  90. handler.add(env, new Macro(env, NewcommandMethods.BeginEnv, [true].concat(environments[env])));
  91. }
  92. }
  93. /**
  94. * The configuration object for configmacros
  95. */
  96. export const ConfigMacrosConfiguration = Configuration.create(
  97. 'configmacros', {
  98. init: configmacrosInit,
  99. config: configmacrosConfig,
  100. items: {
  101. [BeginEnvItem.prototype.kind]: BeginEnvItem,
  102. },
  103. options: {
  104. macros: expandable({}),
  105. environments: expandable({})
  106. }
  107. }
  108. );