TexError.ts 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*************************************************************
  2. *
  3. * Copyright (c) 2009-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 Error class for the TeX parser.
  19. *
  20. * @author v.sorge@mathjax.org (Volker Sorge)
  21. */
  22. export default class TexError {
  23. private static pattern =
  24. /%(\d+|\{\d+\}|\{[a-z]+:\%\d+(?:\|(?:%\{\d+\}|%.|[^\}])*)+\}|.)/g;
  25. /**
  26. * Default error message.
  27. * @type {string}
  28. */
  29. public message: string;
  30. /**
  31. * The old MathJax processing function.
  32. * @param {string} str The basic error message.
  33. * @param {string[]} args The arguments to be replaced in the error message.
  34. * @return {string} The processed error string.
  35. */
  36. private static processString(str: string, args: string[]): string {
  37. let parts = str.split(TexError.pattern);
  38. for (let i = 1, m = parts.length; i < m; i += 2) {
  39. let c = parts[i].charAt(0); // first char will be { or \d or a char to be
  40. // kept literally
  41. if (c >= '0' && c <= '9') { // %n
  42. parts[i] = args[parseInt(parts[i], 10) - 1];
  43. if (typeof parts[i] === 'number') {
  44. parts[i] = parts[i].toString();
  45. }
  46. } else if (c === '{') { // %{n} or %{plural:%n|...}
  47. c = parts[i].substr(1);
  48. if (c >= '0' && c <= '9') { // %{n}
  49. parts[i] = args[parseInt(parts[i].substr(1, parts[i].length - 2), 10) - 1];
  50. if (typeof parts[i] === 'number') {
  51. parts[i] = parts[i].toString();
  52. }
  53. } else { // %{plural:%n|...}
  54. let match = parts[i].match(/^\{([a-z]+):%(\d+)\|(.*)\}$/);
  55. if (match) {
  56. // Removed plural here.
  57. parts[i] = '%' + parts[i];
  58. }
  59. }
  60. }
  61. if (parts[i] == null) {
  62. parts[i] = '???';
  63. }
  64. }
  65. return parts.join('');
  66. }
  67. /**
  68. * @constructor
  69. * @param{string} id message id (for localization)
  70. * @param{string} message text of English message
  71. * @param{string[]=} rest any substitution arguments
  72. */
  73. constructor(public id: string, message: string, ...rest: string[]) {
  74. this.message = TexError.processString(message, rest);
  75. }
  76. }