braille_store.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.EuroStore = exports.BrailleStore = void 0;
  4. const math_store_js_1 = require("./math_store.js");
  5. const semantic_annotations_js_1 = require("../semantic_tree/semantic_annotations.js");
  6. const semantic_attr_js_1 = require("../semantic_tree/semantic_attr.js");
  7. const semantic_meaning_js_1 = require("../semantic_tree/semantic_meaning.js");
  8. class BrailleStore extends math_store_js_1.MathStore {
  9. constructor() {
  10. super(...arguments);
  11. this.modality = 'braille';
  12. this.customTranscriptions = {
  13. '\u22ca': '⠈⠡⠳'
  14. };
  15. }
  16. evaluateString(str) {
  17. const descs = [];
  18. const text = Array.from(str);
  19. for (let i = 0; i < text.length; i++) {
  20. descs.push(this.evaluateCharacter(text[i]));
  21. }
  22. return descs;
  23. }
  24. annotations() {
  25. for (let i = 0, annotator; (annotator = this.annotators[i]); i++) {
  26. (0, semantic_annotations_js_1.activate)(this.locale, annotator);
  27. }
  28. }
  29. }
  30. exports.BrailleStore = BrailleStore;
  31. class EuroStore extends BrailleStore {
  32. constructor() {
  33. super(...arguments);
  34. this.locale = 'euro';
  35. this.customTranscriptions = {};
  36. this.customCommands = {
  37. '\\cdot': '*',
  38. '\\lt': '<',
  39. '\\gt': '>'
  40. };
  41. this.lastSpecial = false;
  42. this.specialChars = ['^', '_', '{', '}'];
  43. }
  44. evaluateString(str) {
  45. const regexp = /(\\[a-z]+|\\{|\\}|\\\\)/i;
  46. const split = str.split(regexp);
  47. const cleaned = this.cleanup(split);
  48. return super.evaluateString(cleaned);
  49. }
  50. cleanup(commands) {
  51. const cleaned = [];
  52. let intext = false;
  53. let lastcom = null;
  54. for (let command of commands) {
  55. if (command.match(/^\\/)) {
  56. if (command === '\\text') {
  57. intext = true;
  58. }
  59. if (this.addSpace(semantic_attr_js_1.SemanticMap.LatexCommands.get(command))) {
  60. cleaned.push(' ');
  61. }
  62. command = this.customCommands[command] || command;
  63. const newcom = command.match(/^\\/);
  64. if (newcom && command.match(/^\\[a-zA-Z]+$/) && lastcom) {
  65. cleaned.push(' ');
  66. }
  67. lastcom = newcom ? command : null;
  68. cleaned.push(command);
  69. continue;
  70. }
  71. const rest = command.split('');
  72. for (const char of rest) {
  73. if (intext) {
  74. cleaned.push(char);
  75. intext = char !== '}';
  76. lastcom = null;
  77. continue;
  78. }
  79. if (char.match(/[a-z]/i) && lastcom) {
  80. lastcom = null;
  81. cleaned.push(' ');
  82. cleaned.push(char);
  83. continue;
  84. }
  85. if (char.match(/\s/))
  86. continue;
  87. if (this.addSpace(char)) {
  88. cleaned.push(' ');
  89. }
  90. cleaned.push(char);
  91. lastcom = null;
  92. }
  93. }
  94. return cleaned.join('');
  95. }
  96. addSpace(char) {
  97. if (!char)
  98. return false;
  99. if (this.specialChars.indexOf(char) !== -1) {
  100. this.lastSpecial = true;
  101. return false;
  102. }
  103. if (this.lastSpecial) {
  104. this.lastSpecial = false;
  105. return false;
  106. }
  107. const meaning = semantic_attr_js_1.SemanticMap.Meaning.get(char);
  108. return (meaning.type === semantic_meaning_js_1.SemanticType.OPERATOR ||
  109. meaning.type === semantic_meaning_js_1.SemanticType.RELATION ||
  110. (meaning.type === semantic_meaning_js_1.SemanticType.PUNCTUATION &&
  111. meaning.role === semantic_meaning_js_1.SemanticRole.COLON));
  112. }
  113. }
  114. exports.EuroStore = EuroStore;