VerbConfiguration.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2018-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 verb package.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. import {Configuration} from '../Configuration.js';
  23. import {TexConstant} from '../TexConstants.js';
  24. import TexParser from '../TexParser.js';
  25. import {CommandMap} from '../SymbolMap.js';
  26. import {ParseMethod} from '../Types.js';
  27. import TexError from '../TexError.js';
  28. // Namespace
  29. export let VerbMethods: Record<string, ParseMethod> = {};
  30. /**
  31. * Implements the verbatim notation \verb|...|.
  32. * @param {TexParser} parser The current tex parser.
  33. * @param {string} name The name of the calling macro.
  34. */
  35. VerbMethods.Verb = function(parser: TexParser, name: string) {
  36. const c = parser.GetNext();
  37. const start = ++parser.i;
  38. if (c === '' ) {
  39. throw new TexError('MissingArgFor', 'Missing argument for %1', name);
  40. }
  41. while (parser.i < parser.string.length &&
  42. parser.string.charAt(parser.i) !== c) {
  43. parser.i++;
  44. }
  45. if (parser.i === parser.string.length) {
  46. throw new TexError('NoClosingDelim',
  47. 'Can\'t find closing delimiter for %1',
  48. parser.currentCS);
  49. }
  50. const text = parser.string.slice(start, parser.i).replace(/ /g, '\u00A0');
  51. parser.i++;
  52. parser.Push(parser.create('token', 'mtext',
  53. {mathvariant: TexConstant.Variant.MONOSPACE},
  54. text));
  55. };
  56. new CommandMap('verb', {verb: 'Verb'}, VerbMethods);
  57. export const VerbConfiguration = Configuration.create(
  58. 'verb', {handler: {macro: ['verb']}}
  59. );