Skip to content

Commit

Permalink
Added quick n dirty syntax highlighting. Closes mochajs#248
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Feb 3, 2012
1 parent 60a9e0f commit ddf89ce
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 4 deletions.
8 changes: 7 additions & 1 deletion mocha.css
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,10 @@ body {

#stats li {
list-style: none;
}
}

code .comment { color: #ddd }
code .init { color: #2F6FAD }
code .string { color: #5890AD }
code .keyword { color: #8A6343 }
code .number { color: #2F6FAD }
23 changes: 21 additions & 2 deletions mocha.js
Original file line number Diff line number Diff line change
Expand Up @@ -1838,7 +1838,7 @@ function Spec(runner) {

runner.on('fail', function(test, err){
cursor.CR();
console.log(indent() + color('fail', ' %d) %s'), n++, test.title);
console.log(indent() + color('fail', ' %d) %s'), ++n, test.title);
});

runner.on('end', self.epilogue.bind(self));
Expand Down Expand Up @@ -2018,6 +2018,7 @@ function XUnit(runner) {
name: 'Mocha Tests'
, tests: stats.tests
, failures: stats.failures
, errors: stats.failures
, skip: stats.tests - stats.failures - stats.passes
, timestamp: (new Date).toUTCString()
, time: stats.duration / 1000
Expand All @@ -2042,7 +2043,7 @@ XUnit.prototype.constructor = XUnit;

function test(test) {
var attrs = {
classname: test.fullTitle()
classname: test.parent.fullTitle()
, name: test.title
, time: test.duration / 1000
};
Expand Down Expand Up @@ -3210,6 +3211,18 @@ window.mocha = require('mocha');
, utils = mocha.utils
, Reporter = mocha.reporters.HTML

function highlight(js) {
return js
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\/\/(.*)/gm, '<span class="comment">//$1</span>')
.replace(/('.*')/gm, '<span class="string">$1</span>')
.replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
.replace(/(\d+)/gm, '<span class="number">$1</span>')
.replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
.replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')
}

function parse(qs) {
return utils.reduce(qs.replace('?', '').split('&'), function(obj, pair){
var i = pair.indexOf('=')
Expand All @@ -3234,6 +3247,12 @@ window.mocha = require('mocha');
var reporter = new Reporter(runner);
var query = parse(window.location.search || "");
if (query.grep) runner.grep(new RegExp(query.grep));
runner.on('end', function(){
$('code').each(function(){
console.log('code');
$(this).html(highlight($(this).text()));
});
});
return runner.run();
};
})();
Expand Down
17 changes: 17 additions & 0 deletions support/tail.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ window.mocha = require('mocha');
, utils = mocha.utils
, Reporter = mocha.reporters.HTML

function highlight(js) {
return js
.replace(/</g, '&lt;')
.replace(/>/g, '&gt;')
.replace(/\/\/(.*)/gm, '<span class="comment">//$1</span>')
.replace(/('.*')/gm, '<span class="string">$1</span>')
.replace(/(\d+\.\d+)/gm, '<span class="number">$1</span>')
.replace(/(\d+)/gm, '<span class="number">$1</span>')
.replace(/\bnew *(\w+)/gm, '<span class="keyword">new</span> <span class="init">$1</span>')
.replace(/\b(function|new|throw|return|var|if|else)\b/gm, '<span class="keyword">$1</span>')
}

function parse(qs) {
return utils.reduce(qs.replace('?', '').split('&'), function(obj, pair){
var i = pair.indexOf('=')
Expand All @@ -80,6 +92,11 @@ window.mocha = require('mocha');
var reporter = new Reporter(runner);
var query = parse(window.location.search || "");
if (query.grep) runner.grep(new RegExp(query.grep));
runner.on('end', function(){
$('code').each(function(){
$(this).html(highlight($(this).text()));
});
});
return runner.run();
};
})();
8 changes: 7 additions & 1 deletion test/browser/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,10 @@ body {

#stats li {
list-style: none;
}
}

code .comment { color: #ddd }
code .init { color: #2F6FAD }
code .string { color: #5890AD }
code .keyword { color: #8A6343 }
code .number { color: #2F6FAD }

0 comments on commit ddf89ce

Please sign in to comment.