UNPKG

nunjucks-highlight.js

Version:

A custom nunjucks tag to highlight code blocks using highlight.js

315 lines (314 loc) 11.3 kB
<!doctype html> <html lang="en"> <head> <title>Code coverage report for lib/index.js</title> <meta charset="utf-8" /> <link rel="stylesheet" href="../prettify.css" /> <link rel="stylesheet" href="../base.css" /> <meta name="viewport" content="width=device-width, initial-scale=1"> <style type='text/css'> .coverage-summary .sorter { background-image: url(../sort-arrow-sprite.png); } </style> </head> <body> <div class='wrapper'> <div class='pad1'> <h1> <a href="../index.html">all files</a> / <a href="index.html">lib/</a> index.js </h1> <div class='clearfix'> <div class='fl pad1y space-right2'> <span class="strong">59.46% </span> <span class="quiet">Statements</span> <span class='fraction'>22/37</span> </div> <div class='fl pad1y space-right2'> <span class="strong">25% </span> <span class="quiet">Branches</span> <span class='fraction'>4/16</span> </div> <div class='fl pad1y space-right2'> <span class="strong">75% </span> <span class="quiet">Functions</span> <span class='fraction'>3/4</span> </div> <div class='fl pad1y space-right2'> <span class="strong">59.46% </span> <span class="quiet">Lines</span> <span class='fraction'>22/37</span> </div> </div> </div> <div class='status-line medium'></div> <pre><table class="coverage"> <tr><td class="line-count quiet">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</td><td class="line-coverage quiet"><span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-no">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-yes"></span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span> <span class="cline-any cline-neutral">&nbsp;</span></td><td class="text"><pre class="prettyprint lang-js">module.exports = function NunjucksCodeHighlight(nunjucks, hljs) { this.tags = ['code']; &nbsp; this.parse = function(parser, nodes, lexer) { // get the tag token var tok = parser.nextToken(); &nbsp; // 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); &nbsp; // If arguments, return the fileTag constructed node <span class="missing-if-branch" title="if path not taken" >I</span>if (args.children.length &gt; 0) <span class="cstat-no" title="statement not covered" > return new nodes.CallExtension(this, 'fileTag', args);</span> &nbsp; // parse the body and possibly the error block, which is optional var body = parser.parseUntilBlocks('endcode'); &nbsp; // I found Nunjucks to be incredibly convoluted on how to just get some data into the BlockTag function, // this finally worked by faking another template node. var tabStart = new nodes.NodeList(0, 0, [new nodes.Output(0, 0, [new nodes.TemplateData(0, 0, (tok.colno - 1))])]); &nbsp; parser.advanceAfterBlockEnd(); &nbsp; // See above for notes about CallExtension return new nodes.CallExtension(this, 'blockTag', args, [ body, tabStart]); }; &nbsp; // code rendering for the file tag. Use the nunjucks.render function to render // the actual contents of the file. Pass the results through the code renderer. // this.fileTag = function(environment, file) { // return new nunjucks.runtime.SafeString(...) // } &nbsp; // code rendering for the block. Pretty simple, just get the body text and pass // it through the code renderer. this.blockTag = function(environment, body, tabStart) { &nbsp; var body = body(); var spacesRegex = /^[\s]+/; var tabStart = tabStart(); // The column postion of the {% code %} tag. &nbsp; <span class="missing-if-branch" title="if path not taken" >I</span>if (tabStart &gt; 0) { // If the {% code %} tag is tabbed in, normalize the content to the same depth. <span class="cstat-no" title="statement not covered" > body = body.split(/\r?\n/); </span>// Split into lines. <span class="cstat-no" title="statement not covered" > body = body.map(<span class="fstat-no" title="function not covered" >function(line, i) {</span></span> <span class="cstat-no" title="statement not covered" > var startSpaces = line.match(spacesRegex);</span> <span class="cstat-no" title="statement not covered" > if (startSpaces &amp;&amp; startSpaces[0].length &gt;= tabStart) { // If the content is not at the same or greater tab depth, do nothing..</span> <span class="cstat-no" title="statement not covered" > return line.slice(tabStart); </span>// Subtract the column position from the start of the string. } else { <span class="cstat-no" title="statement not covered" > return line;</span> } }); &nbsp; // remove blank start and end lines to be safe <span class="cstat-no" title="statement not covered" > if (body[0] === '')</span> <span class="cstat-no" title="statement not covered" > body = body.slice(1);</span> <span class="cstat-no" title="statement not covered" > if (body[body.length - 1] === '')</span> <span class="cstat-no" title="statement not covered" > body = body.slice(0, body.length - 1);</span> &nbsp; <span class="cstat-no" title="statement not covered" > body = body.join("\n"); </span>// Rejoin into one string. &nbsp; } &nbsp; // remove line break from start and end of string // &lt;http://stackoverflow.com/a/14572494&gt; body = body.replace(/^\s+|\s+$/, ''); &nbsp; // get the first line of the body to see if we have a file type specified var value = hljs.highlightAuto(body).value; <span class="missing-if-branch" title="else path not taken" >E</span>if (body.split('\n').length &gt; 1) { var lang = hljs.getLanguage(body.split('\n')[0]); <span class="missing-if-branch" title="if path not taken" >I</span>if (lang) { <span class="cstat-no" title="statement not covered" > lang = body.split('\n')[0];</span> <span class="cstat-no" title="statement not covered" > body = body.split('\n').slice(1).join('\n');</span> <span class="cstat-no" title="statement not covered" > value = hljs.highlight(lang, body).value;</span> } } &nbsp; return new nunjucks.runtime.SafeString('&lt;pre&gt;&lt;code class="hljs"&gt;' + value + ' &lt;/code&gt;&lt;/pre&gt;'); &nbsp; }; }; &nbsp;</pre></td></tr> </table></pre> <div class='push'></div><!-- for sticky footer --> </div><!-- /wrapper --> <div class='footer quiet pad2 space-top1 center small'> Code coverage generated by <a href="http://istanbul-js.org/" target="_blank">istanbul</a> at Sun Nov 20 2016 10:21:34 GMT+0200 (SAST) </div> </div> <script src="../prettify.js"></script> <script> window.onload = function () { if (typeof prettyPrint === 'function') { prettyPrint(); } }; </script> <script src="../sorter.js"></script> </body> </html>