Code coverage report for lib/index.js

Statements: 100% (20 / 20)      Branches: 100% (0 / 0)      Functions: 100% (3 / 3)      Lines: 100% (20 / 20)      Ignored: none     

All files » lib/ » index.js
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 371 2   2   6       6 6     6   6     6     2 4 4 4 3 3 3 3   1 1   3      
module.exports = function NunjucksCodeHighlight(nunjucks, hljs) {
    this.tags = ['code'];
 
    this.parse = function(parser, nodes) {
        // get the tag token
        var tok = parser.nextToken();
 
        // parse the args and move after the block end. passing true
        // as the second arg is required if there are no parentheses
        var args = parser.parseSignature(null, true);
        parser.advanceAfterBlockEnd(tok.value);
 
        // parse the body and possibly the error block, which is optional
        var body = parser.parseUntilBlocks('endcode');
 
        parser.advanceAfterBlockEnd();
 
        // See above for notes about CallExtension
        return new nodes.CallExtension(this, 'run', args, [body]);
    };
 
    this.run = function(context, body) {
        var htmlResult = '';
        try {
            var result = hljs.highlightAuto(body().toString());
            htmlResult += '<pre><code>';
            htmlResult += result.value;
            htmlResult += '</code></pre>';
            htmlResult = new nunjucks.runtime.SafeString(htmlResult);
        } catch (error) {
            htmlResult = undefined;
            throw new Error('Error rendering highlighted code');
        }
        return htmlResult;
    };
};