Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

More robust highlighting

- Fixed strings inside comments and comments inside strings
- Better regexp handling
  • Loading branch information...
commit 5f6baf150327eaebb8cce5976b04f1fe0ce42b27 1 parent f73fa0c
cloudhead authored

Showing 1 changed file with 36 additions and 21 deletions. Show diff stats Hide diff stats

  1. +36 21 hijs.js
57 hijs.js
@@ -16,16 +16,18 @@ var keywords = ('var function if else for while break switch case do new null in
16 16 // Syntax definition
17 17 // The key becomes the class name of the <span>
18 18 // around the matched block of code.
19   -var syntax = {
20   - 'comment': /(\/\*(?:[^*]|\*+[^\/*])*\*+\/|\/\/[^\n]*)/mg,
21   - 'string' : /("(?:(?!")[^\\]|\\.)*"|'(?:(?!')[^\\]|\\.)*')/g,
22   - 'regexp' : /(\/.*\/[mgi]*)(?!\w)/g,
23   - 'class' : /\b([A-Z][a-zA-Z]+)\b/g,
24   - 'number' : /\b([0-9]+(?:\.[0-9]+)?)\b/g,
25   - 'keyword': new(RegExp)('\\b(' + keywords.join('|') + ')\\b', 'g'),
26   - 'special': new(RegExp)('\\b(' + special.join('|') + ')\\b', 'g')
27   -};
  19 +var 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 +];
28 29 var nodes = document.querySelectorAll(selector);
  30 +var table = {};
29 31
30 32 for (var i = 0, children; i < nodes.length; i++) {
31 33 children = nodes[i].childNodes;
@@ -36,9 +38,12 @@ for (var i = 0, children; i < nodes.length; i++) {
36 38 if (code.length >= 0) { // It's a text node
37 39 // Don't highlight command-line snippets
38 40 if (! /^\$/.test(code.nodeValue.trim())) {
39   - Object.keys(syntax).forEach(function (s) {
40   - code.nodeValue = code.nodeValue.replace(syntax[s], function (_, m) {
41   - return '{#' + s + '#' + encode(m) + '#}';
  41 + syntax.forEach(function (s) {
  42 + var k = s[0], v = s[1];
  43 + code.nodeValue = code.nodeValue.replace(v, function (_, m) {
  44 + return '\u00ab' + encode(k) + '\u00b7'
  45 + + encode(m) +
  46 + '\u00b7' + encode(k) + '\u00bb';
42 47 });
43 48 });
44 49 }
@@ -47,23 +52,33 @@ for (var i = 0, children; i < nodes.length; i++) {
47 52 }
48 53 for (var i = 0; i < nodes.length; i++) {
49 54 nodes[i].innerHTML =
50   - nodes[i].innerHTML.replace(/\{#([a-z]+)#(.*?)#\}/g, function (_, name, value) {
51   - return '<span class="' + name + '">' + decode(value) + '</span>';
  55 + nodes[i].innerHTML.replace(/\u00ab(.+?)\u00b7(.+?)\u00b7\1\u00bb/g, function (_, name, value) {
  56 + value = value.replace(/\u00ab[^\u00b7]+\u00b7/g, '').replace(/\u00b7[^\u00bb]+\u00bb/g, '');
  57 + return '<span class="' + decode(name) + '">' + escape(decode(value)) + '</span>';
52 58 });
53 59 }
54 60
  61 +function escape(str) {
  62 + return str.replace(/</g, '&lt;').replace(/>/g, '&gt;');
  63 +}
  64 +
55 65 // Encode ASCII characters to, and from Braille
56   -function encode (str) {
57   - return str.split('').map(function (s) {
  66 +function encode (str, encoded) {
  67 + table[encoded = str.split('').map(function (s) {
58 68 if (s.charCodeAt(0) > 127) { return s }
59 69 return String.fromCharCode(s.charCodeAt(0) + 0x2800);
60   - }).join(' ');
  70 + }).join('')] = str;
  71 + return encoded;
61 72 }
62 73 function decode (str) {
63   - return str.trim().split(' ').map(function (s) {
64   - if (s.charCodeAt(0) - 0x2800 > 127) { return s }
65   - return String.fromCharCode(s.charCodeAt(0) - 0x2800);
66   - }).join('');
  74 + if (str in table) {
  75 + return table[str];
  76 + } else {
  77 + return str.trim().split('').map(function (s) {
  78 + if (s.charCodeAt(0) - 0x2800 > 127) { return s }
  79 + return String.fromCharCode(s.charCodeAt(0) - 0x2800);
  80 + }).join('');
  81 + }
67 82 }
68 83
69 84 })();

0 comments on commit 5f6baf1

Please sign in to comment.
Something went wrong with that request. Please try again.