all files / lib/ token.js

69.35% Statements 43/62
42.42% Branches 14/33
80% Functions 8/10
70.73% Lines 29/41
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                88×   88×   88× 88× 88×         22× 12×     10× 10×                                                                                    
"use strict";
 
var _createClass = (function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Iif (protoProps) defineProperties(Constructor.prototype, protoProps); Eif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; })();
 
Object.defineProperty(exports, "__esModule", {
    value: true
});
 
var _hooks = require("./hooks");
 
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
var Token = (function () {
    function Token(type, content, alias) {
        _classCallCheck(this, Token);
 
        this.type = type;
        this.content = content;
        this.alias = alias;
    }
 
    _createClass(Token, null, [{
        key: "stringify",
        value: function stringify(o, language, parent) {
            if (typeof o === "string") {
                return o;
            }
 
            if (Array.isArray(o)) {
                return o.map(function (element) {
                    return Token.stringify(element, language, o);
                }).join("");
            }
 
            var env = {
                type: o.type,
                content: Token.stringify(o.content, language, parent),
                tag: "span",
                classes: ["token", o.type],
                attributes: {},
                language: language,
                parent: parent
            };
 
            switch (env.type) {
                case "comment":
                    env.attributes.spellcheck = "true";
                    break;
                case "keyword":
                    env.classes.push("keyword-" + env.content.toLowerCase().trim());
                    break;
                case "punctuation":
                    Eif (env.content.match(/\(|\)/g)) {
                        env.classes.push("brackets-parentheses");
                    } else if (env.content.match(/<|>/g)) {
                        env.classes.push("brackets-angle");
                    } else if (env.content.match(/\[|\]>/g)) {
                        env.classes.push("brackets-square");
                    } else if (env.content.match(/\{|\}/g)) {
                        env.classes.push("brackets-braces");
                    }
            }
 
            Iif (o.alias) {
                var _env$classes;
 
                var aliases = Array.isArray(o.alias) ? o.alias : [o.alias];
 
                (_env$classes = env.classes).push.apply(_env$classes, _toConsumableArray(aliases));
            }
 
            (0, _hooks.run)("wrap", env);
 
            var attributes = Object.keys(env.attributes).reduce(function (prev, key) {
                return "" + prev + key + "=\"" + (env.attributes[key] || "") + "\" ";
            }, "");
 
            return "<" + env.tag + " class=\"" + env.classes.join(" ") + "\" " + attributes + ">" + env.content + "</" + env.tag + ">";
        }
    }]);
 
    return Token;
})();
 
exports.default = Token;