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