engine.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.EnginePromise = exports.Engine = exports.SREError = void 0;
  4. const Dcstr = require("../rule_engine/dynamic_cstr.js");
  5. const EngineConst = require("./engine_const.js");
  6. const debugger_js_1 = require("./debugger.js");
  7. const variables_js_1 = require("./variables.js");
  8. class SREError extends Error {
  9. constructor(message = '') {
  10. super();
  11. this.message = message;
  12. this.name = 'SRE Error';
  13. }
  14. }
  15. exports.SREError = SREError;
  16. class Engine {
  17. set defaultLocale(loc) {
  18. this._defaultLocale = variables_js_1.Variables.ensureLocale(loc, this._defaultLocale);
  19. }
  20. get defaultLocale() {
  21. return this._defaultLocale;
  22. }
  23. static getInstance() {
  24. Engine.instance = Engine.instance || new Engine();
  25. return Engine.instance;
  26. }
  27. static defaultEvaluator(str, _cstr) {
  28. return str;
  29. }
  30. static evaluateNode(node) {
  31. return Engine.nodeEvaluator(node);
  32. }
  33. getRate() {
  34. const numeric = parseInt(this.rate, 10);
  35. return isNaN(numeric) ? 100 : numeric;
  36. }
  37. setDynamicCstr(opt_dynamic) {
  38. if (this.defaultLocale) {
  39. Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE] = this.defaultLocale;
  40. }
  41. if (opt_dynamic) {
  42. const keys = Object.keys(opt_dynamic);
  43. for (let i = 0; i < keys.length; i++) {
  44. const feature = keys[i];
  45. if (Dcstr.DynamicCstr.DEFAULT_ORDER.indexOf(feature) !== -1) {
  46. const value = opt_dynamic[feature];
  47. this[feature] = value;
  48. }
  49. }
  50. }
  51. EngineConst.DOMAIN_TO_STYLES[this.domain] = this.style;
  52. const dynamic = [this.locale, this.modality, this.domain, this.style].join('.');
  53. const fallback = Dcstr.DynamicProperties.createProp([Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE]], [Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.MODALITY]], [Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.DOMAIN]], [Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.STYLE]]);
  54. const comparator = this.comparators[this.domain];
  55. const parser = this.parsers[this.domain];
  56. this.parser = parser ? parser : this.defaultParser;
  57. this.dynamicCstr = this.parser.parse(dynamic);
  58. this.dynamicCstr.updateProperties(fallback.getProperties());
  59. this.comparator = comparator
  60. ? comparator()
  61. : new Dcstr.DefaultComparator(this.dynamicCstr);
  62. }
  63. constructor() {
  64. this.customLoader = null;
  65. this.parsers = {};
  66. this.comparator = null;
  67. this.mode = EngineConst.Mode.SYNC;
  68. this.init = true;
  69. this.delay = false;
  70. this.comparators = {};
  71. this.domain = 'mathspeak';
  72. this.style = Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.STYLE];
  73. this._defaultLocale = Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE];
  74. this.locale = Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.LOCALE];
  75. this.subiso = '';
  76. this.modality = Dcstr.DynamicCstr.DEFAULT_VALUES[Dcstr.Axis.MODALITY];
  77. this.speech = EngineConst.Speech.NONE;
  78. this.markup = EngineConst.Markup.NONE;
  79. this.mark = true;
  80. this.automark = false;
  81. this.character = true;
  82. this.cleanpause = true;
  83. this.cayleyshort = true;
  84. this.linebreaks = false;
  85. this.rate = '100';
  86. this.walker = 'Table';
  87. this.structure = false;
  88. this.aria = false;
  89. this.ruleSets = [];
  90. this.strict = false;
  91. this.isIE = false;
  92. this.isEdge = false;
  93. this.pprint = false;
  94. this.config = false;
  95. this.rules = '';
  96. this.prune = '';
  97. this.locale = this.defaultLocale;
  98. this.evaluator = Engine.defaultEvaluator;
  99. this.defaultParser = new Dcstr.DynamicCstrParser(Dcstr.DynamicCstr.DEFAULT_ORDER);
  100. this.parser = this.defaultParser;
  101. this.dynamicCstr = Dcstr.DynamicCstr.defaultCstr();
  102. }
  103. configurate(feature) {
  104. if (this.mode === EngineConst.Mode.HTTP && !this.config) {
  105. configBlocks(feature);
  106. this.config = true;
  107. }
  108. configFeature(feature);
  109. }
  110. setCustomLoader(fn) {
  111. if (fn) {
  112. this.customLoader = fn;
  113. }
  114. }
  115. }
  116. exports.Engine = Engine;
  117. Engine.BINARY_FEATURES = [
  118. 'automark',
  119. 'mark',
  120. 'character',
  121. 'cleanpause',
  122. 'strict',
  123. 'structure',
  124. 'aria',
  125. 'pprint',
  126. 'cayleyshort',
  127. 'linebreaks'
  128. ];
  129. Engine.STRING_FEATURES = [
  130. 'markup',
  131. 'style',
  132. 'domain',
  133. 'speech',
  134. 'walker',
  135. 'defaultLocale',
  136. 'locale',
  137. 'delay',
  138. 'modality',
  139. 'rate',
  140. 'rules',
  141. 'subiso',
  142. 'prune'
  143. ];
  144. Engine.nodeEvaluator = function (_node) {
  145. return [];
  146. };
  147. exports.default = Engine;
  148. function configFeature(feature) {
  149. if (typeof SREfeature !== 'undefined') {
  150. for (const [name, feat] of Object.entries(SREfeature)) {
  151. feature[name] = feat;
  152. }
  153. }
  154. }
  155. function configBlocks(feature) {
  156. const scripts = document.documentElement.querySelectorAll('script[type="text/x-sre-config"]');
  157. for (let i = 0, m = scripts.length; i < m; i++) {
  158. let inner;
  159. try {
  160. inner = scripts[i].innerHTML;
  161. const config = JSON.parse(inner);
  162. for (const [key, val] of Object.entries(config)) {
  163. feature[key] = val;
  164. }
  165. }
  166. catch (_err) {
  167. debugger_js_1.Debugger.getInstance().output('Illegal configuration ', inner);
  168. }
  169. }
  170. }
  171. class EnginePromise {
  172. static get(locale = Engine.getInstance().locale) {
  173. return EnginePromise.promises[locale] || Promise.resolve('');
  174. }
  175. static getall() {
  176. return Promise.all(Object.values(EnginePromise.promises));
  177. }
  178. }
  179. exports.EnginePromise = EnginePromise;
  180. EnginePromise.loaded = {};
  181. EnginePromise.promises = {};