Comparing version 4.7.0 to 4.8.0
@@ -140,2 +140,10 @@ var utils = require('./utils'); | ||
AnsiSerializer.prototype.raw = function (content) { | ||
if ('ansi' in content) { | ||
return content.ansi(); | ||
} else { | ||
return this.serialize(content.fallback); | ||
} | ||
}; | ||
module.exports = AnsiSerializer; |
@@ -77,2 +77,10 @@ var cssStyles = require('./cssStyles'); | ||
ColoredConsoleSerializer.prototype.raw = function (content) { | ||
if ('coloredConsole' in content) { | ||
return content.coloredConsole(); | ||
} else { | ||
return this.serialize(content.fallback); | ||
} | ||
}; | ||
module.exports = ColoredConsoleSerializer; |
@@ -66,2 +66,10 @@ var cssStyles = require('./cssStyles'); | ||
HtmlSerializer.prototype.raw = function (content) { | ||
if ('html' in content) { | ||
return content.html(); | ||
} else { | ||
return this.serialize(content.fallback); | ||
} | ||
}; | ||
module.exports = HtmlSerializer; |
@@ -96,2 +96,22 @@ /*global window*/ | ||
MagicPen.prototype.getContentFromValue = function (value) { | ||
var clone; | ||
if (value.isMagicPen) { | ||
return value; | ||
} else if (typeof value === 'function') { | ||
clone = this.clone(); | ||
value.call(clone, clone); | ||
return clone; | ||
} else if (typeof value === 'string') { | ||
clone = this.clone(); | ||
clone.text(value); | ||
return clone; | ||
} else { | ||
throw new Error('Requires the arguments to be:\n' + | ||
'a pen or\n' + | ||
'a callback appending content to a pen or\n' + | ||
'a text'); | ||
} | ||
}; | ||
MagicPen.prototype.write = function (options) { | ||
@@ -209,3 +229,3 @@ if (this.styles[options.style]) { | ||
'a pen or\n' + | ||
'a callback append content to a penor\n' + | ||
'a callback appending content to a pen or\n' + | ||
'a style and arguments for that style'); | ||
@@ -234,2 +254,43 @@ } | ||
MagicPen.prototype.getContentFromValue = function (value) { | ||
var clone; | ||
if (value.isMagicPen) { | ||
return value; | ||
} else if (typeof value === 'function') { | ||
clone = this.clone(); | ||
value.call(clone, clone); | ||
return clone; | ||
} else if (typeof value === 'string') { | ||
clone = this.clone(); | ||
clone.text(value); | ||
return clone; | ||
} else { | ||
throw new Error('Requires the arguments to be:\n' + | ||
'a pen or\n' + | ||
'a callback appending content to a pen or\n' + | ||
'a text'); | ||
} | ||
}; | ||
MagicPen.prototype.raw = function (options) { | ||
if (!options || typeof options.fallback === 'undefined') { | ||
throw new Error('Requires the argument to be an object with atleast an fallback key'); | ||
} | ||
options = extend({}, options); | ||
Object.keys(options).forEach(function (mode) { | ||
if (mode !== 'fallback' && typeof options[mode] === 'string') { | ||
var output = options[mode]; | ||
options[mode] = function () { | ||
return output; | ||
}; | ||
} | ||
}); | ||
options.fallback = this.getContentFromValue(options.fallback).output.map(function (line) { | ||
return [].concat(line); | ||
}); | ||
this.write({ style: 'raw', args: [options]}); | ||
}; | ||
function amend(output, pen) { | ||
@@ -236,0 +297,0 @@ var lastLine = output[output.length - 1].slice(); |
@@ -26,2 +26,11 @@ var flattenBlocksInLines = require('./flattenBlocksInLines'); | ||
TextSerializer.prototype.raw = function (content) { | ||
if ('text' in content) { | ||
return content.text(); | ||
} else { | ||
return this.serialize(content.fallback); | ||
} | ||
}; | ||
module.exports = TextSerializer; |
@@ -164,2 +164,10 @@ /*! | ||
AnsiSerializer.prototype.raw = function (content) { | ||
if ('ansi' in content) { | ||
return content.ansi(); | ||
} else { | ||
return this.serialize(content.fallback); | ||
} | ||
}; | ||
module.exports = AnsiSerializer; | ||
@@ -244,2 +252,10 @@ | ||
ColoredConsoleSerializer.prototype.raw = function (content) { | ||
if ('coloredConsole' in content) { | ||
return content.coloredConsole(); | ||
} else { | ||
return this.serialize(content.fallback); | ||
} | ||
}; | ||
module.exports = ColoredConsoleSerializer; | ||
@@ -313,2 +329,10 @@ | ||
HtmlSerializer.prototype.raw = function (content) { | ||
if ('html' in content) { | ||
return content.html(); | ||
} else { | ||
return this.serialize(content.fallback); | ||
} | ||
}; | ||
module.exports = HtmlSerializer; | ||
@@ -413,2 +437,22 @@ | ||
MagicPen.prototype.getContentFromValue = function (value) { | ||
var clone; | ||
if (value.isMagicPen) { | ||
return value; | ||
} else if (typeof value === 'function') { | ||
clone = this.clone(); | ||
value.call(clone, clone); | ||
return clone; | ||
} else if (typeof value === 'string') { | ||
clone = this.clone(); | ||
clone.text(value); | ||
return clone; | ||
} else { | ||
throw new Error('Requires the arguments to be:\n' + | ||
'a pen or\n' + | ||
'a callback appending content to a pen or\n' + | ||
'a text'); | ||
} | ||
}; | ||
MagicPen.prototype.write = function (options) { | ||
@@ -526,3 +570,3 @@ if (this.styles[options.style]) { | ||
'a pen or\n' + | ||
'a callback append content to a penor\n' + | ||
'a callback appending content to a pen or\n' + | ||
'a style and arguments for that style'); | ||
@@ -551,2 +595,43 @@ } | ||
MagicPen.prototype.getContentFromValue = function (value) { | ||
var clone; | ||
if (value.isMagicPen) { | ||
return value; | ||
} else if (typeof value === 'function') { | ||
clone = this.clone(); | ||
value.call(clone, clone); | ||
return clone; | ||
} else if (typeof value === 'string') { | ||
clone = this.clone(); | ||
clone.text(value); | ||
return clone; | ||
} else { | ||
throw new Error('Requires the arguments to be:\n' + | ||
'a pen or\n' + | ||
'a callback appending content to a pen or\n' + | ||
'a text'); | ||
} | ||
}; | ||
MagicPen.prototype.raw = function (options) { | ||
if (!options || typeof options.fallback === 'undefined') { | ||
throw new Error('Requires the argument to be an object with atleast an fallback key'); | ||
} | ||
options = extend({}, options); | ||
Object.keys(options).forEach(function (mode) { | ||
if (mode !== 'fallback' && typeof options[mode] === 'string') { | ||
var output = options[mode]; | ||
options[mode] = function () { | ||
return output; | ||
}; | ||
} | ||
}); | ||
options.fallback = this.getContentFromValue(options.fallback).output.map(function (line) { | ||
return [].concat(line); | ||
}); | ||
this.write({ style: 'raw', args: [options]}); | ||
}; | ||
function amend(output, pen) { | ||
@@ -872,2 +957,11 @@ var lastLine = output[output.length - 1].slice(); | ||
TextSerializer.prototype.raw = function (content) { | ||
if ('text' in content) { | ||
return content.text(); | ||
} else { | ||
return this.serialize(content.fallback); | ||
} | ||
}; | ||
module.exports = TextSerializer; | ||
@@ -874,0 +968,0 @@ |
{ | ||
"name": "magicpen", | ||
"version": "4.7.0", | ||
"version": "4.8.0", | ||
"description": "Styled output in both consoles and browsers", | ||
@@ -30,4 +30,4 @@ "main": "./lib/MagicPen.js", | ||
"sinon": "1.9.1", | ||
"unexpected": "5.2.0", | ||
"unexpected-sinon": "5.0.0" | ||
"unexpected": "^7.0.0", | ||
"unexpected-sinon": "^5.1.2" | ||
}, | ||
@@ -34,0 +34,0 @@ "dependencies": { |
@@ -450,2 +450,43 @@ # MagicPen | ||
### raw(...) | ||
If you need something completely custom, you can specify the actual | ||
string that will be serialized for each of the different modes. You | ||
need to specify a fallback, for the modes that are not specified. | ||
The custom output is generated at serialization time and can be a | ||
string or a function returning a string for each mode you want to | ||
override. | ||
The fallback can be a pen, a function where this is a pen or a string. | ||
```js | ||
var pen = magicpen(); | ||
pen.addStyle('link', function (label, url) { | ||
this.raw({ | ||
fallback: function () { | ||
this.text(label).sp().text('(').blue(url).text(')'); | ||
}, | ||
html: function () { | ||
return '<a href="' + url + '" alt="' + label + '">' + label + '</a>'; | ||
} | ||
}); | ||
}); | ||
pen.link('magicpen', 'https://github.com/sunesimonsen/magicpen'); | ||
``` | ||
This will be the output in ansi mode: | ||
``` | ||
magicpen (\x1B[34mhttps://github.com/sunesimonsen/magicpen\x1B[39m) | ||
``` | ||
This will be the output in html mode: | ||
``` | ||
<div style="font-family: monospace; white-space: nowrap"> | ||
<div><a href="https://github.com/sunesimonsen/magicpen" alt="magicpen">magicpen</a></div> | ||
</div> | ||
``` | ||
### isBlock() | ||
@@ -452,0 +493,0 @@ |
@@ -499,2 +499,97 @@ /*global describe, it, beforeEach*/ | ||
describe('raw', function () { | ||
it('requires an argument with an fallback key', function () { | ||
expect(function () { | ||
pen.raw({}); | ||
}, 'to throw', 'Requires the argument to be an object with atleast an fallback key'); | ||
}); | ||
it('is capable of providing custom output for different serializers', function () { | ||
pen.raw({ | ||
fallback: function () { | ||
this.text('foo'); | ||
}, | ||
ansi: 'bar', | ||
html: function () { | ||
return '<img src="..." style="width: 100em">'; | ||
} | ||
}); | ||
expect(pen.toString('text'), 'to equal', 'foo'); | ||
expect(pen.toString('ansi'), 'to equal', 'bar'); | ||
expect(pen.toString('html'), 'to equal', | ||
'<div style="font-family: monospace; white-space: nowrap">\n' + | ||
' <div><img src="..." style="width: 100em"></div>\n' + | ||
'</div>'); | ||
}); | ||
it('custom output for modes is computed at serialization time', function () { | ||
var dynamicContent = null; | ||
pen.raw({ | ||
fallback: function () { | ||
this.text('foo'); | ||
}, | ||
ansi: function () { | ||
return 'This is dynamic content: ' + dynamicContent; | ||
} | ||
}); | ||
dynamicContent = 'foo'; | ||
expect(pen.toString('ansi'), 'to equal', 'This is dynamic content: foo'); | ||
dynamicContent = 'bar'; | ||
expect(pen.toString('ansi'), 'to equal', 'This is dynamic content: bar'); | ||
}); | ||
it('custom content for modes can be specified as a string', function () { | ||
pen.raw({ | ||
fallback: function () { | ||
this.text('foo'); | ||
}, | ||
ansi: 'bar' | ||
}); | ||
expect(pen.toString('ansi'), 'to equal', 'bar'); | ||
}); | ||
it('custom content for modes can be specified as a function', function () { | ||
pen.raw({ | ||
fallback: function () { | ||
this.text('foo'); | ||
}, | ||
ansi: function () { return 'bar'; } | ||
}); | ||
expect(pen.toString('ansi'), 'to equal', 'bar'); | ||
}); | ||
it('the fallback content can be specified as a string', function () { | ||
pen.raw({ | ||
fallback: 'foo' | ||
}); | ||
expect(pen.toString('ansi'), 'to equal', 'foo'); | ||
}); | ||
it('the fallback content can be specified as a function that appends the output', function () { | ||
pen.raw({ | ||
fallback: function () { | ||
this.red('foo'); | ||
} | ||
}); | ||
expect(pen.toString('ansi'), 'to equal', '\x1B[31mfoo\x1B[39m'); | ||
}); | ||
it('the fallback content can be specified as a pen', function () { | ||
pen.raw({ | ||
fallback: pen.clone().red('foo') | ||
}); | ||
expect(pen.toString('ansi'), 'to equal', '\x1B[31mfoo\x1B[39m'); | ||
}); | ||
it('falls back to the fallback content if there is no override for the mode that is being serialized', function () { | ||
pen.raw({ | ||
fallback: function (output) { | ||
output.text('fallback'); | ||
} | ||
}); | ||
expect(pen.toString('text'), 'to equal', 'fallback'); | ||
expect(pen.toString('ansi'), 'to equal', 'fallback'); | ||
}); | ||
}); | ||
describe('in text mode', function () { | ||
@@ -1107,2 +1202,35 @@ it('ignores unknown styles', function () { | ||
describe('link example', function () { | ||
beforeEach(function () { | ||
pen.addStyle('link', function (label, url) { | ||
this.raw({ | ||
fallback: function () { | ||
this.text(label).sp().text('(').blue(url).text(')'); | ||
}, | ||
html: function () { | ||
return '<a href="' + url + '" alt="' + label + '">' + label + '</a>'; | ||
} | ||
}); | ||
}); | ||
pen.link('magicpen', 'https://github.com/sunesimonsen/magicpen'); | ||
}); | ||
it('in text mode', function () { | ||
expect(pen.toString(), 'to equal', | ||
'magicpen (https://github.com/sunesimonsen/magicpen)'); | ||
}); | ||
it('in ansi mode', function () { | ||
expect(pen.toString('ansi'), 'to equal', | ||
'magicpen (\x1B[34mhttps://github.com/sunesimonsen/magicpen\x1B[39m)'); | ||
}); | ||
it('in html mode', function () { | ||
expect(pen.toString('html'), 'to equal', | ||
'<div style="font-family: monospace; white-space: nowrap">\n' + | ||
' <div><a href="https://github.com/sunesimonsen/magicpen" alt="magicpen">magicpen</a></div>\n' + | ||
'</div>'); | ||
}); | ||
}); | ||
describe('ColoredConsoleSerializer', function () { | ||
@@ -1109,0 +1237,0 @@ it('should output an array', function () { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
550286
3773
703