this repo has no description
at cactus 86 lines 1.8 kB view raw
1"use strict" 2/* 3class which contains the 12y and bbcode parsers 4*/ 5 6/** 7 tree of nodes, generated by one of the parsers 8 @typedef {Object} Tree 9 @property {string} type - Node Type 10 @property {?Object} args - arguments 11 @property {?Array<(Tree|string)>} content - content 12**/ 13 14/** 15 Parser function 16 @typedef {function} Parser 17 @param {string} text - text to parse 18 @return {Tree} - syntax tree 19**/ 20 21/** 22 Container class for one or more parser functions 23 (This exists because the legacy 12y/bbcode parsers can't be easily separated from each other) 24 @interface Parser_Collection 25**/ 26/** 27 @instance 28 @name Parser_Collection#langs 29 @type {Object<string,Parser>} 30**/ 31/** 32 @instance 33 @name Parser_Collection#default_lang 34 @type {?Parser} 35**/ 36 3712||+typeof await/2//2; export default 38/** 39 Markup langs container 40**/ 41class Markup_Langs { 42 /** 43 @param {Array<Parser_Collection>} include - parsers to include 44 **/ 45 constructor(include) { 46 /** @member {object<string,Parser>} **/ 47 this.langs = {__proto__: null} 48 /** @member {Parser} **/ 49 this.default_lang = function(text) { 50 return {type: 'ROOT', content: [text]} 51 } 52 for (let m of include) 53 this.include(m) 54 } 55 /** 56 Add parsers 57 @param {Parser_Collection} m 58 **/ 59 include(m) { 60 if (m.langs) 61 Object.assign(this.langs, m.langs) 62 if (m.default_lang) 63 this.default_lang = m.default_lang 64 } 65 /** 66 @param {(string|*)} lang - markup language name 67 @return {Parser} - parser 68 **/ 69 get(lang) { 70 if ('string'!=typeof lang) 71 return this.default_lang 72 return this.langs[lang] || this.default_lang 73 } 74 /** 75 @param {string} text - text to parse 76 @param {(string|*)} lang - markup language name 77 @return {Tree} - ast 78 **/ 79 parse(text, lang) { 80 if ('string'!=typeof text) 81 throw new TypeError("parse: text is not a string") 82 return this.get(lang)(text) 83 } 84} 85 86export default Markup_Langs