l0bsterssg

node.js static responsive blog post generator
Log | Files | Refs | README

ini.js (2922B)


      1 /**
      2  * @param {string} value
      3  * @returns {RegExp}
      4  * */
      5 
      6 /**
      7  * @param {RegExp | string } re
      8  * @returns {string}
      9  */
     10 function source(re) {
     11   if (!re) return null;
     12   if (typeof re === "string") return re;
     13 
     14   return re.source;
     15 }
     16 
     17 /**
     18  * @param {RegExp | string } re
     19  * @returns {string}
     20  */
     21 function lookahead(re) {
     22   return concat('(?=', re, ')');
     23 }
     24 
     25 /**
     26  * @param {...(RegExp | string) } args
     27  * @returns {string}
     28  */
     29 function concat(...args) {
     30   const joined = args.map((x) => source(x)).join("");
     31   return joined;
     32 }
     33 
     34 /**
     35  * Any of the passed expresssions may match
     36  *
     37  * Creates a huge this | this | that | that match
     38  * @param {(RegExp | string)[] } args
     39  * @returns {string}
     40  */
     41 function either(...args) {
     42   const joined = '(' + args.map((x) => source(x)).join("|") + ")";
     43   return joined;
     44 }
     45 
     46 /*
     47 Language: TOML, also INI
     48 Description: TOML aims to be a minimal configuration file format that's easy to read due to obvious semantics.
     49 Contributors: Guillaume Gomez <guillaume1.gomez@gmail.com>
     50 Category: common, config
     51 Website: https://github.com/toml-lang/toml
     52 */
     53 
     54 function ini(hljs) {
     55   var NUMBERS = {
     56     className: 'number',
     57     relevance: 0,
     58     variants: [
     59       { begin: /([\+\-]+)?[\d]+_[\d_]+/ },
     60       { begin: hljs.NUMBER_RE }
     61     ]
     62   };
     63   var COMMENTS = hljs.COMMENT();
     64   COMMENTS.variants = [
     65     {begin: /;/, end: /$/},
     66     {begin: /#/, end: /$/},
     67   ];
     68   var VARIABLES = {
     69     className: 'variable',
     70     variants: [
     71       { begin: /\$[\w\d"][\w\d_]*/ },
     72       { begin: /\$\{(.*?)}/ }
     73     ]
     74   };
     75   var LITERALS = {
     76     className: 'literal',
     77     begin: /\bon|off|true|false|yes|no\b/
     78   };
     79   var STRINGS = {
     80     className: "string",
     81     contains: [hljs.BACKSLASH_ESCAPE],
     82     variants: [
     83       { begin: "'''", end: "'''", relevance: 10 },
     84       { begin: '"""', end: '"""', relevance: 10 },
     85       { begin: '"', end: '"' },
     86       { begin: "'", end: "'" }
     87     ]
     88   };
     89   var ARRAY = {
     90     begin: /\[/, end: /\]/,
     91     contains: [
     92       COMMENTS,
     93       LITERALS,
     94       VARIABLES,
     95       STRINGS,
     96       NUMBERS,
     97       'self'
     98     ],
     99     relevance:0
    100   };
    101 
    102   var BARE_KEY = /[A-Za-z0-9_-]+/;
    103   var QUOTED_KEY_DOUBLE_QUOTE = /"(\\"|[^"])*"/;
    104   var QUOTED_KEY_SINGLE_QUOTE = /'[^']*'/;
    105   var ANY_KEY = either(
    106     BARE_KEY, QUOTED_KEY_DOUBLE_QUOTE, QUOTED_KEY_SINGLE_QUOTE
    107   );
    108   var DOTTED_KEY = concat(
    109     ANY_KEY, '(\\s*\\.\\s*', ANY_KEY, ')*',
    110     lookahead(/\s*=\s*[^#\s]/)
    111   );
    112 
    113   return {
    114     name: 'TOML, also INI',
    115     aliases: ['toml'],
    116     case_insensitive: true,
    117     illegal: /\S/,
    118     contains: [
    119       COMMENTS,
    120       {
    121         className: 'section',
    122         begin: /\[+/, end: /\]+/
    123       },
    124       {
    125         begin: DOTTED_KEY,
    126         className: 'attr',
    127         starts: {
    128           end: /$/,
    129           contains: [
    130             COMMENTS,
    131             ARRAY,
    132             LITERALS,
    133             VARIABLES,
    134             STRINGS,
    135             NUMBERS
    136           ]
    137         }
    138       }
    139     ]
    140   };
    141 }
    142 
    143 module.exports = ini;