Permalink
Browse files

More robust highlighting

- Fixed strings inside comments and comments inside strings
- Better regexp handling
  • Loading branch information...
1 parent f73fa0c commit 5f6baf150327eaebb8cce5976b04f1fe0ce42b27 cloudhead committed Jul 11, 2010
Showing with 36 additions and 21 deletions.
  1. +36 −21 hijs.js
View
57 hijs.js
@@ -16,16 +16,18 @@ var keywords = ('var function if else for while break switch case do new null in
// Syntax definition
// The key becomes the class name of the <span>
// around the matched block of code.
-var syntax = {
- 'comment': /(\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/[^\n]*)/mg,
- 'string' : /("(?:(?!")[^\\]|\\.)*"|'(?:(?!')[^\\]|\\.)*')/g,
- 'regexp' : /(\/.*\/[mgi]*)(?!\w)/g,
- 'class' : /\b([A-Z][a-zA-Z]+)\b/g,
- 'number' : /\b([0-9]+(?:\.[0-9]+)?)\b/g,
- 'keyword': new(RegExp)('\\b(' + keywords.join('|') + ')\\b', 'g'),
- 'special': new(RegExp)('\\b(' + special.join('|') + ')\\b', 'g')
-};
+var syntax = [
+ ['comment', /(\/\*(?:[^*\n]|\*+[^\/*])*\*+\/)/g],
+ ['comment', /(\/\/[^\n]*)/g],
+ ['string' , /("(?:(?!")[^\\\n]|\\.)*"|'(?:(?!')[^\\\n]|\\.)*')/g],
+ ['regexp' , /(\/.+\/[mgi]*)(?!\s*\w)/g],
+ ['class' , /\b([A-Z][a-zA-Z]+)\b/g],
+ ['number' , /\b([0-9]+(?:\.[0-9]+)?)\b/g],
+ ['keyword', new(RegExp)('\\b(' + keywords.join('|') + ')\\b', 'g')],
+ ['special', new(RegExp)('\\b(' + special.join('|') + ')\\b', 'g')]
+];
var nodes = document.querySelectorAll(selector);
+var table = {};
for (var i = 0, children; i < nodes.length; i++) {
children = nodes[i].childNodes;
@@ -36,9 +38,12 @@ for (var i = 0, children; i < nodes.length; i++) {
if (code.length >= 0) { // It's a text node
// Don't highlight command-line snippets
if (! /^\$/.test(code.nodeValue.trim())) {
- Object.keys(syntax).forEach(function (s) {
- code.nodeValue = code.nodeValue.replace(syntax[s], function (_, m) {
- return '{#' + s + '#' + encode(m) + '#}';
+ syntax.forEach(function (s) {
+ var k = s[0], v = s[1];
+ code.nodeValue = code.nodeValue.replace(v, function (_, m) {
+ return '\u00ab' + encode(k) + '\u00b7'
+ + encode(m) +
+ '\u00b7' + encode(k) + '\u00bb';
});
});
}
@@ -47,23 +52,33 @@ for (var i = 0, children; i < nodes.length; i++) {
}
for (var i = 0; i < nodes.length; i++) {
nodes[i].innerHTML =
- nodes[i].innerHTML.replace(/\{#([a-z]+)#(.*?)#\}/g, function (_, name, value) {
- return '<span class="' + name + '">' + decode(value) + '</span>';
+ nodes[i].innerHTML.replace(/\u00ab(.+?)\u00b7(.+?)\u00b7\1\u00bb/g, function (_, name, value) {
+ value = value.replace(/\u00ab[^\u00b7]+\u00b7/g, '').replace(/\u00b7[^\u00bb]+\u00bb/g, '');
+ return '<span class="' + decode(name) + '">' + escape(decode(value)) + '</span>';
});
}
+function escape(str) {
+ return str.replace(/</g, '&lt;').replace(/>/g, '&gt;');
+}
+
// Encode ASCII characters to, and from Braille
-function encode (str) {
- return str.split('').map(function (s) {
+function encode (str, encoded) {
+ table[encoded = str.split('').map(function (s) {
if (s.charCodeAt(0) > 127) { return s }
return String.fromCharCode(s.charCodeAt(0) + 0x2800);
- }).join(' ');
+ }).join('')] = str;
+ return encoded;
}
function decode (str) {
- return str.trim().split(' ').map(function (s) {
- if (s.charCodeAt(0) - 0x2800 > 127) { return s }
- return String.fromCharCode(s.charCodeAt(0) - 0x2800);
- }).join('');
+ if (str in table) {
+ return table[str];
+ } else {
+ return str.trim().split('').map(function (s) {
+ if (s.charCodeAt(0) - 0x2800 > 127) { return s }
+ return String.fromCharCode(s.charCodeAt(0) - 0x2800);
+ }).join('');
+ }
}
})();

0 comments on commit 5f6baf1

Please sign in to comment.