Configuration.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. var __read = (this && this.__read) || function (o, n) {
  14. var m = typeof Symbol === "function" && o[Symbol.iterator];
  15. if (!m) return o;
  16. var i = m.call(o), r, ar = [], e;
  17. try {
  18. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  19. }
  20. catch (error) { e = { error: error }; }
  21. finally {
  22. try {
  23. if (r && !r.done && (m = i["return"])) m.call(i);
  24. }
  25. finally { if (e) throw e.error; }
  26. }
  27. return ar;
  28. };
  29. Object.defineProperty(exports, "__esModule", { value: true });
  30. exports.ParserConfiguration = exports.ConfigurationHandler = exports.Configuration = void 0;
  31. var Options_js_1 = require("../../util/Options.js");
  32. var MapHandler_js_1 = require("./MapHandler.js");
  33. var FunctionList_js_1 = require("../../util/FunctionList.js");
  34. var PrioritizedList_js_1 = require("../../util/PrioritizedList.js");
  35. var Tags_js_1 = require("./Tags.js");
  36. var Configuration = (function () {
  37. function Configuration(name, handler, fallback, items, tags, options, nodes, preprocessors, postprocessors, initMethod, configMethod, priority, parser) {
  38. if (handler === void 0) { handler = {}; }
  39. if (fallback === void 0) { fallback = {}; }
  40. if (items === void 0) { items = {}; }
  41. if (tags === void 0) { tags = {}; }
  42. if (options === void 0) { options = {}; }
  43. if (nodes === void 0) { nodes = {}; }
  44. if (preprocessors === void 0) { preprocessors = []; }
  45. if (postprocessors === void 0) { postprocessors = []; }
  46. if (initMethod === void 0) { initMethod = null; }
  47. if (configMethod === void 0) { configMethod = null; }
  48. this.name = name;
  49. this.handler = handler;
  50. this.fallback = fallback;
  51. this.items = items;
  52. this.tags = tags;
  53. this.options = options;
  54. this.nodes = nodes;
  55. this.preprocessors = preprocessors;
  56. this.postprocessors = postprocessors;
  57. this.initMethod = initMethod;
  58. this.configMethod = configMethod;
  59. this.priority = priority;
  60. this.parser = parser;
  61. this.handler = Object.assign({ character: [], delimiter: [], macro: [], environment: [] }, handler);
  62. }
  63. Configuration.makeProcessor = function (func, priority) {
  64. return Array.isArray(func) ? func : [func, priority];
  65. };
  66. Configuration._create = function (name, config) {
  67. var _this = this;
  68. if (config === void 0) { config = {}; }
  69. var priority = config.priority || PrioritizedList_js_1.PrioritizedList.DEFAULTPRIORITY;
  70. var init = config.init ? this.makeProcessor(config.init, priority) : null;
  71. var conf = config.config ? this.makeProcessor(config.config, priority) : null;
  72. var preprocessors = (config.preprocessors || []).map(function (pre) { return _this.makeProcessor(pre, priority); });
  73. var postprocessors = (config.postprocessors || []).map(function (post) { return _this.makeProcessor(post, priority); });
  74. var parser = config.parser || 'tex';
  75. return new Configuration(name, config.handler || {}, config.fallback || {}, config.items || {}, config.tags || {}, config.options || {}, config.nodes || {}, preprocessors, postprocessors, init, conf, priority, parser);
  76. };
  77. Configuration.create = function (name, config) {
  78. if (config === void 0) { config = {}; }
  79. var configuration = Configuration._create(name, config);
  80. ConfigurationHandler.set(name, configuration);
  81. return configuration;
  82. };
  83. Configuration.local = function (config) {
  84. if (config === void 0) { config = {}; }
  85. return Configuration._create('', config);
  86. };
  87. Object.defineProperty(Configuration.prototype, "init", {
  88. get: function () {
  89. return this.initMethod ? this.initMethod[0] : null;
  90. },
  91. enumerable: false,
  92. configurable: true
  93. });
  94. Object.defineProperty(Configuration.prototype, "config", {
  95. get: function () {
  96. return this.configMethod ? this.configMethod[0] : null;
  97. },
  98. enumerable: false,
  99. configurable: true
  100. });
  101. return Configuration;
  102. }());
  103. exports.Configuration = Configuration;
  104. var ConfigurationHandler;
  105. (function (ConfigurationHandler) {
  106. var maps = new Map();
  107. ConfigurationHandler.set = function (name, map) {
  108. maps.set(name, map);
  109. };
  110. ConfigurationHandler.get = function (name) {
  111. return maps.get(name);
  112. };
  113. ConfigurationHandler.keys = function () {
  114. return maps.keys();
  115. };
  116. })(ConfigurationHandler = exports.ConfigurationHandler || (exports.ConfigurationHandler = {}));
  117. var ParserConfiguration = (function () {
  118. function ParserConfiguration(packages, parsers) {
  119. var e_1, _a, e_2, _b;
  120. if (parsers === void 0) { parsers = ['tex']; }
  121. this.initMethod = new FunctionList_js_1.FunctionList();
  122. this.configMethod = new FunctionList_js_1.FunctionList();
  123. this.configurations = new PrioritizedList_js_1.PrioritizedList();
  124. this.parsers = [];
  125. this.handlers = new MapHandler_js_1.SubHandlers();
  126. this.items = {};
  127. this.tags = {};
  128. this.options = {};
  129. this.nodes = {};
  130. this.parsers = parsers;
  131. try {
  132. for (var _c = __values(packages.slice().reverse()), _d = _c.next(); !_d.done; _d = _c.next()) {
  133. var pkg = _d.value;
  134. this.addPackage(pkg);
  135. }
  136. }
  137. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  138. finally {
  139. try {
  140. if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
  141. }
  142. finally { if (e_1) throw e_1.error; }
  143. }
  144. try {
  145. for (var _e = __values(this.configurations), _f = _e.next(); !_f.done; _f = _e.next()) {
  146. var _g = _f.value, config = _g.item, priority = _g.priority;
  147. this.append(config, priority);
  148. }
  149. }
  150. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  151. finally {
  152. try {
  153. if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
  154. }
  155. finally { if (e_2) throw e_2.error; }
  156. }
  157. }
  158. ParserConfiguration.prototype.init = function () {
  159. this.initMethod.execute(this);
  160. };
  161. ParserConfiguration.prototype.config = function (jax) {
  162. var e_3, _a;
  163. this.configMethod.execute(this, jax);
  164. try {
  165. for (var _b = __values(this.configurations), _c = _b.next(); !_c.done; _c = _b.next()) {
  166. var config = _c.value;
  167. this.addFilters(jax, config.item);
  168. }
  169. }
  170. catch (e_3_1) { e_3 = { error: e_3_1 }; }
  171. finally {
  172. try {
  173. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  174. }
  175. finally { if (e_3) throw e_3.error; }
  176. }
  177. };
  178. ParserConfiguration.prototype.addPackage = function (pkg) {
  179. var name = typeof pkg === 'string' ? pkg : pkg[0];
  180. var conf = this.getPackage(name);
  181. conf && this.configurations.add(conf, typeof pkg === 'string' ? conf.priority : pkg[1]);
  182. };
  183. ParserConfiguration.prototype.add = function (name, jax, options) {
  184. var e_4, _a;
  185. if (options === void 0) { options = {}; }
  186. var config = this.getPackage(name);
  187. this.append(config);
  188. this.configurations.add(config, config.priority);
  189. this.init();
  190. var parser = jax.parseOptions;
  191. parser.nodeFactory.setCreators(config.nodes);
  192. try {
  193. for (var _b = __values(Object.keys(config.items)), _c = _b.next(); !_c.done; _c = _b.next()) {
  194. var kind = _c.value;
  195. parser.itemFactory.setNodeClass(kind, config.items[kind]);
  196. }
  197. }
  198. catch (e_4_1) { e_4 = { error: e_4_1 }; }
  199. finally {
  200. try {
  201. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  202. }
  203. finally { if (e_4) throw e_4.error; }
  204. }
  205. Tags_js_1.TagsFactory.addTags(config.tags);
  206. (0, Options_js_1.defaultOptions)(parser.options, config.options);
  207. (0, Options_js_1.userOptions)(parser.options, options);
  208. this.addFilters(jax, config);
  209. if (config.config) {
  210. config.config(this, jax);
  211. }
  212. };
  213. ParserConfiguration.prototype.getPackage = function (name) {
  214. var config = ConfigurationHandler.get(name);
  215. if (config && this.parsers.indexOf(config.parser) < 0) {
  216. throw Error("Package ".concat(name, " doesn't target the proper parser"));
  217. }
  218. return config;
  219. };
  220. ParserConfiguration.prototype.append = function (config, priority) {
  221. priority = priority || config.priority;
  222. if (config.initMethod) {
  223. this.initMethod.add(config.initMethod[0], config.initMethod[1]);
  224. }
  225. if (config.configMethod) {
  226. this.configMethod.add(config.configMethod[0], config.configMethod[1]);
  227. }
  228. this.handlers.add(config.handler, config.fallback, priority);
  229. Object.assign(this.items, config.items);
  230. Object.assign(this.tags, config.tags);
  231. (0, Options_js_1.defaultOptions)(this.options, config.options);
  232. Object.assign(this.nodes, config.nodes);
  233. };
  234. ParserConfiguration.prototype.addFilters = function (jax, config) {
  235. var e_5, _a, e_6, _b;
  236. try {
  237. for (var _c = __values(config.preprocessors), _d = _c.next(); !_d.done; _d = _c.next()) {
  238. var _e = __read(_d.value, 2), pre = _e[0], priority = _e[1];
  239. jax.preFilters.add(pre, priority);
  240. }
  241. }
  242. catch (e_5_1) { e_5 = { error: e_5_1 }; }
  243. finally {
  244. try {
  245. if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
  246. }
  247. finally { if (e_5) throw e_5.error; }
  248. }
  249. try {
  250. for (var _f = __values(config.postprocessors), _g = _f.next(); !_g.done; _g = _f.next()) {
  251. var _h = __read(_g.value, 2), post = _h[0], priority = _h[1];
  252. jax.postFilters.add(post, priority);
  253. }
  254. }
  255. catch (e_6_1) { e_6 = { error: e_6_1 }; }
  256. finally {
  257. try {
  258. if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
  259. }
  260. finally { if (e_6) throw e_6.error; }
  261. }
  262. };
  263. return ParserConfiguration;
  264. }());
  265. exports.ParserConfiguration = ParserConfiguration;
  266. //# sourceMappingURL=Configuration.js.map