Permalink
Browse files

Split out ansi2html

  • Loading branch information...
1 parent 719cdfa commit 667e61f756b62179e9f046be424bcbe4eea37ab1 @agnoster committed Jun 21, 2011
Showing with 40 additions and 13 deletions.
  1. +17 −5 lib/ansi2html.js
  2. +23 −8 test/ansi2html_spec.js
View
@@ -12,6 +12,7 @@
{ bold: "font-weight"
, underline: "text-decoration"
, color: "color"
+ , background: "background"
}
function style() {
@@ -38,15 +39,14 @@
open = false
if (n) {
- for (i in n) {
- props[i] = n[i]
- }
+ for (i in n) props[i] = n[i]
tag += '<span style="' + style() + '">'
open = true
+ } else {
+ props = {}
}
return tag
-
}
return str.replace(/\[(\d+;)?(\d+)+m/g, function(match, b1, b2) {
@@ -58,13 +58,16 @@
res += tag(code)
}
return res
- })
+ }) + tag()
}
+
ansi2html.table =
{ 0: null
, 1: { bold: true }
, 3: { italic: true }
, 4: { underline: true }
+ , 23: { italic: false }
+ , 24: { underline: false }
, 30: { color: 'black' }
, 31: { color: 'red' }
, 32: { color: 'green' }
@@ -74,6 +77,15 @@
, 36: { color: 'cyan' }
, 37: { color: 'white' }
, 39: { color: null }
+ , 40: { background: 'black' }
+ , 41: { background: 'red' }
+ , 42: { background: 'green' }
+ , 43: { background: 'yellow' }
+ , 44: { background: 'blue' }
+ , 45: { background: 'magenta' }
+ , 46: { background: 'cyan' }
+ , 47: { background: 'white' }
+ , 49: { background: null }
}
export('ansi2html', ansi2html)
View
@@ -2,14 +2,29 @@ var ansi2html = require('../lib/ansi2html')
, vows = require('vows')
, assert = require('assert')
-vows.describe('ansi2html').addBatch(
-{ 'when loading ansi2html':
- { topic: function() { return ansi2html }
- , 'it should exist': function(it) { assert.notEqual(it, null) }
+function makeBatchFromTestCases(testCases) {
+ function makeTest(input, expected) {
+ var test = { topic: function() { return ansi2html(input) } }
+ test['we get "' + expected + '"'] = function(topic) { assert.equal(topic, expected) }
+ return test
}
-, 'when encoding a string':
- { topic: function() { return ansi2html('test string') }
- , 'it should be correct': function(it) { assert.equal(it, 'test string') }
+
+ var batch = {}
+ for (var key in testCases) {
+ if (!testCases.hasOwnProperty(key)) continue
+ batch['When encoding "' + key + '"'] = makeTest(key, testCases[key])
}
-}).export(module)
+ return batch
+}
+
+var suite = vows.describe('ansi2html')
+suite.addBatch(makeBatchFromTestCases(
+{ 'testString': 'testString'
+, 'test[0mString': 'testString'
+, 'test[32mString': 'test<span style="color:green">String</span>'
+, 'test[1mBold[32mGreen[31mRed[0mNone': 'test<span style="font-weight:bold">Bold</span><span style="font-weight:bold;color:green">Green</span><span style="font-weight:bold;color:red">Red</span>None'
+, 'test[31mRed[1mBold[39mString': 'test<span style="color:red">Red</span><span style="color:red;font-weight:bold">Bold</span><span style="font-weight:bold">String</span>'
+, 'test[42mGreenBG': 'test<span style="background:green">GreenBG</span>'
+}))
+suite.export(module)

0 comments on commit 667e61f

Please sign in to comment.