Permalink
Browse files

Basic color/bold/underline styling converted

  • Loading branch information...
0 parents commit 3a24064306ee3fce8c438299289f031c76127a15 @agnoster committed Aug 14, 2012
Showing with 215 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +19 −0 LICENSE
  3. +52 −0 README.md
  4. +93 −0 lib/ansi2html.js
  5. +20 −0 package.json
  6. +30 −0 test/ansi2html_spec.js
@@ -0,0 +1 @@
+node_modules
19 LICENSE
@@ -0,0 +1,19 @@
+Copyright (C) 2011 by Isaac Wolkerstorfer
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
@@ -0,0 +1,52 @@
+# ansi2html.js
+
+Convert text with ANSI escape sequences to styled HTML.
+
+## Example
+
+ test[1mBold[32mGreen[31mRed[0mNone
+
+becomes
+
+ 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
+
+## Usage
+
+In Node.js:
+
+ var ansi2html = require('ansi2html')
+
+ console.log(ansi2html("test[1mBold"))
+
+In the browser:
+
+ <pre id="test"></pre>
+ <script type="text/javascript" src="ansi2html.js"></script>
+ <script type="text/javascript">
+ document.getElementById('test').innerHTML = ansi2html("test[1mBold")
+ </script>
+
+That's right, ansi2html.js works in Node *and* in the browser, in more or less the same way.
+
+## License
+
+Copyright (C) 2011 by Isaac Wolkerstorfer
+
+Permission is hereby granted, free of charge, to any person obtaining a copy
+of this software and associated documentation files (the "Software"), to deal
+in the Software without restriction, including without limitation the rights
+to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+copies of the Software, and to permit persons to whom the Software is
+furnished to do so, subject to the following conditions:
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+THE SOFTWARE.
+
@@ -0,0 +1,93 @@
+(function(){
+ function declare(module_name, exports) {
+ if (module) module.exports = exports
+ else window[module_name] = exports
+ }
+
+ function ansi2html(str) {
+ var props = {}
+ , open = false
+
+ var stylemap =
+ { bold: "font-weight"
+ , underline: "text-decoration"
+ , color: "color"
+ , background: "background"
+ }
+
+ function style() {
+ var key, val, style = []
+ for (var key in props) {
+ val = props[key]
+ if (!val) continue
+ if (val == true) {
+ style.push(stylemap[key] + ':' + key)
+ } else {
+ style.push(stylemap[key] + ':' + val)
+ }
+ }
+ return style.join(';')
+ }
+
+
+ function tag(code) {
+ var i
+ , tag = ''
+ , n = ansi2html.table[code]
+
+ if (open) tag += '</span>'
+ open = false
+
+ if (n) {
+ 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) {
+ var i, code, res = ''
+ for (i = 1; i < arguments.length - 2; i++) {
+ if (!arguments[i]) continue
+ code = parseInt(arguments[i])
+
+ 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' }
+ , 33: { color: 'yellow' }
+ , 34: { color: 'blue' }
+ , 35: { color: 'magenta' }
+ , 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 }
+ }
+
+ declare('ansi2html', ansi2html)
+})()
+
@@ -0,0 +1,20 @@
+{ "author": "Isaac Wolkerstorfer <agnoster@gmail.com> (http://agnoster.net/)"
+, "name": "ansi2html"
+, "description": "Convert text with ANSI escape sequences to HTML markup"
+, "version": "0.0.0"
+, "homepage": "https://github.com/agnoster/ansi2html-js"
+, "repository":
+ { "url": ""
+ }
+, "main": "./lib/ansi2html"
+, "engines":
+ { "node": "~0.4.7"
+ }
+, "dependencies": {}
+, "devDependencies":
+ { "vows": "~0.5.8"
+ }
+, "scripts":
+ { "test": "./node_modules/vows/bin/vows test/*_spec.js --spec"
+ }
+}
@@ -0,0 +1,30 @@
+var ansi2html = require('../lib/ansi2html')
+ , vows = require('vows')
+ , assert = require('assert')
+
+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
+ }
+
+ var batch = {}
+ for (var key in testCases) {
+ if (!testCases.hasOwnProperty(key)) continue
+ batch['When encoding "' + key + '"'] = makeTest(key, testCases[key])
+ }
+ 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 3a24064

Please sign in to comment.