|
| 1 | +(function () { |
| 2 | +// |
| 3 | +// hiless - LESS Syntax Highlighter |
| 4 | +// |
| 5 | +// Copyright (c) 2010 Alexis Sellier |
| 6 | +// |
| 7 | + |
| 8 | +// All elements which match this will be syntax highlighted. |
| 9 | +var selector = 'code'; |
| 10 | + |
| 11 | +// Syntax definition |
| 12 | +// The key becomes the class name of the <span> |
| 13 | +// around the matched block of code. |
| 14 | +var syntax = { |
| 15 | + 'string' : /("(?:(?!")[^\\]|\\.)*"|'(?:(?!')[^\\]|\\.)*')/g, |
| 16 | + 'comment' : /(\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/[^\n]*)/mg, |
| 17 | + 'color' : /(#[a-fA-F0-9]{6}|#[a-fA-F0-9]{3})\b(?=[^\{\}]*[\};])/mg, |
| 18 | + 'nth' : /(\([n0-9+-]+\))(?=[^\{\}]*\{)/g, |
| 19 | + 'number' : /\b((?:-?\d*\.?\d+)(?:px|%|em|pc|ex|in|deg|s|ms|pt|cm|mm)?)/g, |
| 20 | + 'class' : /([\.:][\w-]+)(?=[^\{\};]*\{)/mg, |
| 21 | + 'variable' : /(@-?[-a-z_0-9]+\s*)/g, |
| 22 | + 'attribute': /(\*?-?[-a-z_0-9]+\s*)(?=:[^\{\};]*[\};])/mg, |
| 23 | + 'selector' : /(\[[a-z]+)/g, |
| 24 | + 'id' : /(#[\w-]+)(?=[^\{\}]*\{)/mg, |
| 25 | + 'mixin' : /([#\.][\w-]+)(?=[^;\{\}]*[;\}])/g, |
| 26 | + 'element' : /\b([a-z]+[0-9]?)\b(?=[^\{\}\);]*\{)/mg, |
| 27 | + 'special' : /(! *important)\b/g |
| 28 | +}; |
| 29 | +var nodes = document.querySelectorAll(selector); |
| 30 | +var table = {}; |
| 31 | + |
| 32 | +for (var i = 0, children; i < nodes.length; i++) { |
| 33 | + children = nodes[i].childNodes; |
| 34 | + |
| 35 | + for (var j = 0, str; j < children.length; j++) { |
| 36 | + code = children[j]; |
| 37 | + |
| 38 | + if (code.length >= 0) { // It's a text node |
| 39 | + // Don't highlight command-line snippets |
| 40 | + if (! /^\$/.test(code.nodeValue.trim())) { |
| 41 | + Object.keys(syntax).forEach(function (s) { |
| 42 | + code.nodeValue = code.nodeValue.replace(syntax[s], function (_, m) { |
| 43 | + return '\u00ab' + encode(s) + '\u00b7' |
| 44 | + + encode(m) + |
| 45 | + '\u00b7' + encode(s) + '\u00bb'; |
| 46 | + }); |
| 47 | + }); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | +for (var i = 0; i < nodes.length; i++) { |
| 53 | + nodes[i].innerHTML = |
| 54 | + nodes[i].innerHTML.replace(/\u00ab(.+?)\u00b7(.+?)\u00b7\1\u00bb/g, function (_, name, value) { |
| 55 | + value = value.replace(/\u00ab.+?\u00b7/g, '').replace(/\u00b7.+?\u00bb/g, ''); |
| 56 | + return '<span class="' + decode(name) + '">' + decode(value) + '</span>'; |
| 57 | + }); |
| 58 | +} |
| 59 | + |
| 60 | +// Encode ASCII characters to, and from Braille |
| 61 | +function encode (str, encoded) { |
| 62 | + table[encoded = str.split('').map(function (s) { |
| 63 | + if (s.charCodeAt(0) > 127) { return s } |
| 64 | + return String.fromCharCode(s.charCodeAt(0) + 0x2800); |
| 65 | + }).join('')] = str; |
| 66 | + return encoded; |
| 67 | +} |
| 68 | +function decode (str) { |
| 69 | + if (str in table) { |
| 70 | + return table[str]; |
| 71 | + } else { |
| 72 | + return str.trim().split('').map(function (s) { |
| 73 | + if (s.charCodeAt(0) - 0x2800 > 127) { return s } |
| 74 | + return String.fromCharCode(s.charCodeAt(0) - 0x2800); |
| 75 | + }).join(''); |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +})(); |
0 commit comments