Exemplo n.º 1
0
    highlight: function (text, ast) {
        function makeCallback(value) {
            if (_.isFunction(value)) {
                return value;
            }

            return function (node) {
                return node[value];
            };
        }

        var highlighter = this,
            nodeHighlighters = highlighter.options.nodes,
            getOffset = makeCallback(highlighter.options.offset),
            getLength = makeCallback(highlighter.options.length),
            getType = makeCallback(highlighter.options.type),
            parts = text.split('');

        _.forOwn(ast, function highlight(node) {
            if (typeof node === 'object' && hasOwn.call(nodeHighlighters, getType(node))) {
                nodeHighlighters[getType(node)](node, function (node, replacement) {
                    var i,
                        l;

                    parts[getOffset(node)] = replacement;

                    for (i = getOffset(node) + 1, l = getOffset(node) + getLength(node); i < l; i++) {
                        parts[i] = '';
                    }
                });
            }

            if (_.isArray(node)) {
                _.each(node, function (element) {
                    highlight(element);
                });
            } else if (_.isPlainObject(node)) {
                _.forOwn(node, function (property) {
                    highlight(property);
                });
            }
        });

        return parts.join('');
    }
Exemplo n.º 2
0
        _.forOwn(ast, function highlight(node) {
            if (typeof node === 'object' && hasOwn.call(nodeHighlighters, getType(node))) {
                nodeHighlighters[getType(node)](node, function (node, replacement) {
                    var i,
                        l;

                    parts[getOffset(node)] = replacement;

                    for (i = getOffset(node) + 1, l = getOffset(node) + getLength(node); i < l; i++) {
                        parts[i] = '';
                    }
                });
            }

            if (_.isArray(node)) {
                _.each(node, function (element) {
                    highlight(element);
                });
            } else if (_.isPlainObject(node)) {
                _.forOwn(node, function (property) {
                    highlight(property);
                });
            }
        });