818
Du
dustymarkdown / files
dustymarkdown / node_modules / dust / vendor / hijs.js
3.2 KB
1(function (hijs) { 2// 3// hijs - JavaScript Syntax Highlighter 4// 5// Copyright (c) 2010 Alexis Sellier 6// 7 8// All elements which match this will be syntax highlighted. 9var selector = hijs || 'code'; 10 11var keywords = ('var function if else for while break switch case do new null in with void ' 12 +'continue delete return this true false throw catch typeof with instanceof').split(' '), 13 special = ('eval window document undefined NaN Infinity parseInt parseFloat ' 14 +'encodeURI decodeURI encodeURIComponent decodeURIComponent').split(' '); 15 16// Syntax definition 17// The key becomes the class name of the <span> 18// around the matched block of code. 19var syntax = [ 20 ['comment', /(\/\*(?:[^*\n]|\*+[^\/*])*\*+\/)/g], 21 ['comment', /(\/\/[^\n]*)/g], 22 ['string' , /("(?:(?!")[^\\\n]|\\.)*"|'(?:(?!')[^\\\n]|\\.)*')/g], 23 ['regexp' , /(\/.+\/[mgi]*)(?!\s*\w)/g], 24 ['class' , /\b([A-Z][a-zA-Z]+)\b/g], 25 ['number' , /\b([0-9]+(?:\.[0-9]+)?)\b/g], 26 ['keyword', new(RegExp)('\\b(' + keywords.join('|') + ')\\b', 'g')], 27 ['special', new(RegExp)('\\b(' + special.join('|') + ')\\b', 'g')] 28]; 29var nodes, table = {}; 30 31if (/^[a-z]+$/.test(selector)) { 32 nodes = document.getElementsByTagName(selector); 33} else if (/^\.[\w-]+$/.test(selector)) { 34 nodes = document.getElementsByClassName(selector.slice(1)); 35} else if (document.querySelectorAll) { 36 nodes = document.querySelectorAll(selector); 37} else { 38 nodes = []; 39} 40 41for (var i = 0, children; i < nodes.length; i++) { 42 children = nodes[i].childNodes; 43 44 for (var j = 0, str; j < children.length; j++) { 45 code = children[j]; 46 47 if (code.length >= 0) { // It's a text node 48 // Don't highlight command-line snippets 49 if (! /^\$/.test(code.nodeValue.trim())) { 50 syntax.forEach(function (s) { 51 var k = s[0], v = s[1]; 52 code.nodeValue = code.nodeValue.replace(v, function (_, m) { 53 return '\u00ab' + encode(k) + '\u00b7' 54 + encode(m) + 55 '\u00b7' + encode(k) + '\u00bb'; 56 }); 57 }); 58 } 59 } 60 } 61} 62for (var i = 0; i < nodes.length; i++) { 63 nodes[i].innerHTML = 64 nodes[i].innerHTML.replace(/\u00ab(.+?)\u00b7(.+?)\u00b7\1\u00bb/g, function (_, name, value) { 65 value = value.replace(/\u00ab[^\u00b7]+\u00b7/g, '').replace(/\u00b7[^\u00bb]+\u00bb/g, ''); 66 return '<span class="' + decode(name) + '">' + escape(decode(value)) + '</span>'; 67 }); 68} 69 70function escape(str) { 71 return str.replace(/</g, '<').replace(/>/g, '>'); 72} 73 74// Encode ASCII characters to, and from Braille 75function encode (str, encoded) { 76 table[encoded = str.split('').map(function (s) { 77 if (s.charCodeAt(0) > 127) { return s } 78 return String.fromCharCode(s.charCodeAt(0) + 0x2800); 79 }).join('')] = str; 80 return encoded; 81} 82function decode (str) { 83 if (str in table) { 84 return table[str]; 85 } else { 86 return str.trim().split('').map(function (s) { 87 if (s.charCodeAt(0) - 0x2800 > 127) { return s } 88 return String.fromCharCode(s.charCodeAt(0) - 0x2800); 89 }).join(''); 90 } 91} 92 93})(window.hijs);