Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

magicpen

Package Overview
Dependencies
Maintainers
2
Versions
93
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

magicpen - npm Package Compare versions

Comparing version 4.1.0 to 4.2.0

images/fib-theme-ansi.png

17

lib/AnsiSerializer.js
var utils = require('./utils');
var TextSerializer = require('./TextSerializer');
var colorDiff = require('color-diff');
var rgbRegexp = require('./rgbRegexp');
var themeMapper = require('./themeMapper');

@@ -10,3 +12,5 @@ var ansiStyles = utils.extend({}, require('ansi-styles'));

function AnsiSerializer() {}
function AnsiSerializer(theme) {
this.theme = theme;
}

@@ -88,9 +92,10 @@ AnsiSerializer.prototype = new TextSerializer();

var rgbRegexp = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
AnsiSerializer.prototype.text = function () {
var content = arguments[0];
if (arguments.length > 1) {
for (var i = arguments.length -1; i > 0; i -= 1) {
var styleName = arguments[i];
var args = themeMapper(this.theme, arguments);
var content = args[0];
if (args.length > 1) {
for (var i = args.length -1; i > 0; i -= 1) {
var styleName = args[i];
if (ansiStyles[styleName]) {

@@ -97,0 +102,0 @@ content = ansiStyles[styleName].open + content + ansiStyles[styleName].close;

var cssStyles = require('./cssStyles');
var flattenBlocksInLines = require('./flattenBlocksInLines');
var rgbRegexp = require('./rgbRegexp');
var themeMapper = require('./themeMapper');
function ColoredConsoleSerializer() {}
function ColoredConsoleSerializer(theme) {
this.theme = theme;
}

@@ -45,14 +49,15 @@ ColoredConsoleSerializer.prototype.serialize = function (lines) {

var rgbRegexp = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
ColoredConsoleSerializer.prototype.text = function () {
var content = String(arguments[0]);
var args = themeMapper(this.theme, arguments);
var content = String(args[0]);
if (content === '') {
return null;
}
var result = ['%c' + content.replace(/%/g, '%%')];
var styleProperties = [];
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i += 1) {
var styleName = arguments[i];
if (args.length > 1) {
for (var i = 1; i < args.length; i += 1) {
var styleName = args[i];
if (rgbRegexp.test(styleName)) {

@@ -59,0 +64,0 @@ if (styleName.substring(0, 2) === 'bg') {

var cssStyles = require('./cssStyles');
var rgbRegexp = require('./rgbRegexp');
var themeMapper = require('./themeMapper');
function HtmlSerializer() {}
function HtmlSerializer(theme) {
this.theme = theme;
}

@@ -29,5 +33,6 @@ HtmlSerializer.prototype.serialize = function (lines) {

var rgbRegexp = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
HtmlSerializer.prototype.text = function () {
var content = String(arguments[0])
var args = themeMapper(this.theme, arguments);
var content = String(args[0])
.replace(/&/g, '&amp;')

@@ -39,6 +44,6 @@ .replace(/ /g, '&nbsp;')

if (arguments.length > 1) {
if (args.length > 1) {
var styleProperties = [];
for (var i = 1; i < arguments.length; i += 1) {
var styleName = arguments[i];
for (var j = 1; j < args.length; j += 1) {
var styleName = args[j];
if (rgbRegexp.test(styleName)) {

@@ -55,3 +60,6 @@ if (styleName.substring(0, 2) === 'bg') {

}
content = '<span style="' + styleProperties.join('; ') + '">' + content + '</span>';
if (styleProperties.length > 0) {
content = '<span style="' + styleProperties.join('; ') + '">' + content + '</span>';
}
}

@@ -58,0 +66,0 @@ return content;

@@ -21,2 +21,3 @@ /*global window*/

this.installedPlugins = [];
this._themes = {};
}

@@ -144,3 +145,4 @@

}
var serializer = new MagicPen.serializers[format]();
var theme = this._themes[format] || {};
var serializer = new MagicPen.serializers[format](theme);
return serializer.serialize(this.output);

@@ -293,2 +295,3 @@ };

clonedPen.installedPlugins = [].concat(this.installedPlugins);
clonedPen._themes = this._themes;
return clonedPen;

@@ -433,2 +436,41 @@ };

MagicPen.prototype.installTheme = function (formats, theme) {
var that = this;
if (arguments.length === 1) {
theme = formats;
formats = Object.keys(MagicPen.serializers);
}
if (typeof formats === 'string') {
formats = [formats];
}
if (
typeof theme !== 'object' ||
!Array.isArray(formats) ||
formats.some(function (format) {
return typeof format !== 'string';
})
) {
throw new Error("Themes must be installed the following way:\n" +
"Install theme for all formats: pen.installTheme({ comment: 'gray' })\n" +
"Install theme for a specific format: pen.installTheme('ansi', { comment: 'gray' }) or\n" +
"Install theme for a list of formats: pen.installTheme(['ansi', 'html'], { comment: 'gray' })");
}
formats.forEach(function (format) {
that._themes[format] = extend({}, that._themes[format] || {}, theme);
});
Object.keys(theme).forEach(function (themeEntry) {
if (!that[themeEntry]) {
that.addStyle(themeEntry, function (content) {
that.text(content, themeEntry);
});
}
});
return this;
};
module.exports = MagicPen;

@@ -25,7 +25,9 @@ /*!

!function(e){if("object"==typeof exports&&"undefined"!=typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o;"undefined"!=typeof window?o=window:"undefined"!=typeof global?o=global:"undefined"!=typeof self&&(o=self),(o.weknowhow||(o.weknowhow={})).MagicPen=e()}}(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
var utils = require(9);
var utils = require(11);
var TextSerializer = require(5);
var colorDiff = require(14);
var colorDiff = require(16);
var rgbRegexp = require(9);
var themeMapper = require(10);
var ansiStyles = utils.extend({}, require(10));
var ansiStyles = utils.extend({}, require(12));
Object.keys(ansiStyles).forEach(function (styleName) {

@@ -35,3 +37,5 @@ ansiStyles[styleName.toLowerCase()] = ansiStyles[styleName];

function AnsiSerializer() {}
function AnsiSerializer(theme) {
this.theme = theme;
}

@@ -113,9 +117,10 @@ AnsiSerializer.prototype = new TextSerializer();

var rgbRegexp = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
AnsiSerializer.prototype.text = function () {
var content = arguments[0];
if (arguments.length > 1) {
for (var i = arguments.length -1; i > 0; i -= 1) {
var styleName = arguments[i];
var args = themeMapper(this.theme, arguments);
var content = args[0];
if (args.length > 1) {
for (var i = args.length -1; i > 0; i -= 1) {
var styleName = args[i];
if (ansiStyles[styleName]) {

@@ -160,4 +165,8 @@ content = ansiStyles[styleName].open + content + ansiStyles[styleName].close;

var flattenBlocksInLines = require(8);
var rgbRegexp = require(9);
var themeMapper = require(10);
function ColoredConsoleSerializer() {}
function ColoredConsoleSerializer(theme) {
this.theme = theme;
}

@@ -203,14 +212,15 @@ ColoredConsoleSerializer.prototype.serialize = function (lines) {

var rgbRegexp = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
ColoredConsoleSerializer.prototype.text = function () {
var content = String(arguments[0]);
var args = themeMapper(this.theme, arguments);
var content = String(args[0]);
if (content === '') {
return null;
}
var result = ['%c' + content.replace(/%/g, '%%')];
var styleProperties = [];
if (arguments.length > 1) {
for (var i = 1; i < arguments.length; i += 1) {
var styleName = arguments[i];
if (args.length > 1) {
for (var i = 1; i < args.length; i += 1) {
var styleName = args[i];
if (rgbRegexp.test(styleName)) {

@@ -235,4 +245,8 @@ if (styleName.substring(0, 2) === 'bg') {

var cssStyles = require(6);
var rgbRegexp = require(9);
var themeMapper = require(10);
function HtmlSerializer() {}
function HtmlSerializer(theme) {
this.theme = theme;
}

@@ -263,5 +277,6 @@ HtmlSerializer.prototype.serialize = function (lines) {

var rgbRegexp = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
HtmlSerializer.prototype.text = function () {
var content = String(arguments[0])
var args = themeMapper(this.theme, arguments);
var content = String(args[0])
.replace(/&/g, '&amp;')

@@ -273,6 +288,6 @@ .replace(/ /g, '&nbsp;')

if (arguments.length > 1) {
if (args.length > 1) {
var styleProperties = [];
for (var i = 1; i < arguments.length; i += 1) {
var styleName = arguments[i];
for (var j = 1; j < args.length; j += 1) {
var styleName = args[j];
if (rgbRegexp.test(styleName)) {

@@ -289,3 +304,6 @@ if (styleName.substring(0, 2) === 'bg') {

}
content = '<span style="' + styleProperties.join('; ') + '">' + content + '</span>';
if (styleProperties.length > 0) {
content = '<span style="' + styleProperties.join('; ') + '">' + content + '</span>';
}
}

@@ -299,3 +317,3 @@ return content;

/*global window*/
var utils = require(9);
var utils = require(11);
var extend = utils.extend;

@@ -319,2 +337,3 @@ var duplicateText = require(7);

this.installedPlugins = [];
this._themes = {};
}

@@ -328,3 +347,3 @@

}
} else if (require(16)) {
} else if (require(18)) {
MagicPen.defaultFormat = 'ansi'; // colored console

@@ -443,3 +462,4 @@ } else {

}
var serializer = new MagicPen.serializers[format]();
var theme = this._themes[format] || {};
var serializer = new MagicPen.serializers[format](theme);
return serializer.serialize(this.output);

@@ -592,2 +612,3 @@ };

clonedPen.installedPlugins = [].concat(this.installedPlugins);
clonedPen._themes = this._themes;
return clonedPen;

@@ -732,2 +753,41 @@ };

MagicPen.prototype.installTheme = function (formats, theme) {
var that = this;
if (arguments.length === 1) {
theme = formats;
formats = Object.keys(MagicPen.serializers);
}
if (typeof formats === 'string') {
formats = [formats];
}
if (
typeof theme !== 'object' ||
!Array.isArray(formats) ||
formats.some(function (format) {
return typeof format !== 'string';
})
) {
throw new Error("Themes must be installed the following way:\n" +
"Install theme for all formats: pen.installTheme({ comment: 'gray' })\n" +
"Install theme for a specific format: pen.installTheme('ansi', { comment: 'gray' }) or\n" +
"Install theme for a list of formats: pen.installTheme(['ansi', 'html'], { comment: 'gray' })");
}
formats.forEach(function (format) {
that._themes[format] = extend({}, that._themes[format] || {}, theme);
});
Object.keys(theme).forEach(function (themeEntry) {
if (!that[themeEntry]) {
that.addStyle(themeEntry, function (content) {
that.text(content, themeEntry);
});
}
});
return this;
};
module.exports = MagicPen;

@@ -836,3 +896,3 @@

},{}],8:[function(require,module,exports){
var utils = require(9);
var utils = require(11);
var duplicateText = require(7);

@@ -921,2 +981,21 @@

},{}],9:[function(require,module,exports){
module.exports = /^(?:bg)?#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
},{}],10:[function(require,module,exports){
var rgbRegexp = require(9);
var cssStyles = require(6);
module.exports = function (theme, args) {
var themeKey = args[1];
if (args.length === 2 && typeof themeKey === 'string') {
var themeMapping = theme[themeKey];
if (themeMapping && !rgbRegexp.test(themeKey) && !cssStyles[themeKey]) {
return [args[0]].concat(themeMapping);
}
}
return args;
};
},{}],11:[function(require,module,exports){
var utils = {

@@ -989,3 +1068,3 @@ extend: function (target) {

},{}],10:[function(require,module,exports){
},{}],12:[function(require,module,exports){
'use strict';

@@ -1048,42 +1127,34 @@

},{}],11:[function(require,module,exports){
},{}],13:[function(require,module,exports){
// shim for using process in browser
var process = module.exports = {};
var queue = [];
var draining = false;
process.nextTick = (function () {
var canSetImmediate = typeof window !== 'undefined'
&& window.setImmediate;
var canPost = typeof window !== 'undefined'
&& window.postMessage && window.addEventListener
;
if (canSetImmediate) {
return function (f) { return window.setImmediate(f) };
function drainQueue() {
if (draining) {
return;
}
if (canPost) {
var queue = [];
window.addEventListener('message', function (ev) {
var source = ev.source;
if ((source === window || source === null) && ev.data === 'process-tick') {
ev.stopPropagation();
if (queue.length > 0) {
var fn = queue.shift();
fn();
}
}
}, true);
return function nextTick(fn) {
queue.push(fn);
window.postMessage('process-tick', '*');
};
draining = true;
var currentQueue;
var len = queue.length;
while(len) {
currentQueue = queue;
queue = [];
var i = -1;
while (++i < len) {
currentQueue[i]();
}
len = queue.length;
}
draining = false;
}
process.nextTick = function (fun) {
queue.push(fun);
if (!draining) {
setTimeout(drainQueue, 0);
}
};
return function nextTick(fn) {
setTimeout(fn, 0);
};
})();
process.title = 'browser';

@@ -1093,2 +1164,3 @@ process.browser = true;

process.argv = [];
process.version = ''; // empty string to avoid regexp issues

@@ -1107,3 +1179,3 @@ function noop() {}

throw new Error('process.binding is not supported');
}
};

@@ -1115,4 +1187,5 @@ // TODO(shtylman)

};
process.umask = function() { return 0; };
},{}],12:[function(require,module,exports){
},{}],14:[function(require,module,exports){
/**

@@ -1232,3 +1305,3 @@ * @author Markus Näsman

},{}],13:[function(require,module,exports){
},{}],15:[function(require,module,exports){
/**

@@ -1399,8 +1472,8 @@ * @author Markus Näsman

},{}],14:[function(require,module,exports){
},{}],16:[function(require,module,exports){
'use strict';
var diff = require(13);
var convert = require(12);
var palette = require(15);
var diff = require(15);
var convert = require(14);
var palette = require(17);

@@ -1417,16 +1490,7 @@ var color = module.exports = {};

var result = color.map_palette([target], relative, 'closest');
var result = color.map_palette([target], relative);
return result[key];
};
color.furthest = function(target, relative) {
var key = color.palette_map_key(target);
var result = color.map_palette([target], relative, 'furthest');
return result[key];
};
},{}],15:[function(require,module,exports){
},{}],17:[function(require,module,exports){
/**

@@ -1469,4 +1533,4 @@ * @author Markus Näsman

*/
var color_diff = require(13);
var color_convert = require(12);
var color_diff = require(15);
var color_convert = require(14);

@@ -1491,9 +1555,7 @@ /**

* @param [{rgbcolor}] b each element should have fields R,G,B
* @param 'type' should have field closest or furthest
* @return {palettemap}
*/
function map_palette(a, b, type)
function map_palette(a, b)
{
var c = {};
type = type || 'closest';
for (var idx1 in a){

@@ -1507,15 +1569,7 @@ var color1 = a[idx1];

var current_color_diff = diff(color1,color2);
if((best_color == undefined) || ((type === 'closest') && (current_color_diff < best_color_diff)))
if((best_color == undefined) || (current_color_diff < best_color_diff))
{
best_color = color2;
best_color_diff = current_color_diff;
continue;
}
if((type === 'furthest') && (current_color_diff > best_color_diff))
{
best_color = color2;
best_color_diff = current_color_diff;
continue;
}
}

@@ -1543,3 +1597,3 @@ c[palette_map_key(color1)] = best_color;

},{}],16:[function(require,module,exports){
},{}],18:[function(require,module,exports){
(function (process){

@@ -1586,4 +1640,4 @@ 'use strict';

}).call(this,require(11))
}).call(this,require(13))
},{}]},{},[4])(4)
});
{
"name": "magicpen",
"version": "4.1.0",
"version": "4.2.0",
"description": "Styled output in both consoles and browsers",

@@ -22,19 +22,19 @@ "main": "./lib/MagicPen.js",

"devDependencies": {
"browserify": "^5.9.1",
"bundle-collapser": "^1.1.0",
"coveralls": "^2.11.1",
"del": "^0.1.1",
"istanbul": "^0.3.0",
"browserify": "8.1.0",
"bundle-collapser": "1.1.1",
"coveralls": "2.11.1",
"del": "0.1.1",
"istanbul": "0.3.0",
"jshint": "*",
"mocha": "^1.20.1",
"sinon": "=1.9.1",
"unexpected": "5.0.0-beta36",
"unexpected-sinon": "^3.0.2"
"mocha": "1.20.1",
"sinon": "1.9.1",
"unexpected": "5.2.0",
"unexpected-sinon": "5.0.0"
},
"dependencies": {
"ansi-styles": "^2.0.0",
"color-diff": "^0.1.4",
"supports-color": "^1.2.0"
"ansi-styles": "2.0.0",
"color-diff": "0.1.4",
"supports-color": "1.2.0"
},
"license": "MIT"
}

@@ -426,2 +426,75 @@ # MagicPen

### installTheme(theme), installTheme(format, theme), installTheme(formats, theme)
MagicPen have support for theming text styles differently for each
format. A theme is just a hash of aliases to build in text styles. You
install the theme for one or more formats.
```js
pen.installTheme({
keyword: 'yellow',
functionName: ['green', 'italic'],
number: '#FF8DFE'
});
```
After installing the theme you get the following methods on the pen
`keyword`, `functionName` and `number`. If you wish to install the
theme after writing to the pen, you need to use the `text` method
instead: `pen.text('this uses the keyword style', 'keyword')`
Let's print some code with the pen:
```js
pen.keyword('function').sp().functionName('fib').text(' {').nl()
.indentLines()
.i().keyword('var').text(' i=0, fibs = [').number(0).text(', ').number(1)
.text('];').nl()
.i().keyword('for').text(' (; i < n; i += ').number(1).text(') {').nl()
.indentLines()
.i().text('fibs.push(fibs[').number(0).text('] + fibs[').number(1)
.text(']);').nl()
.i().text('fibs.shift();').nl()
.outdentLines()
.i().text('}').nl()
.i().keyword('return').text(' fibs[').number(0).text('];').nl()
.outdentLines()
.text('}');
```
This will produce the following ansi output:
![Fibonacci ansi output](images/fib-theme-ansi.png "Fibonacci ansi output")
But these colors looks pretty lame on white background, which we
usually use when outputting to the html format:
![Fibonacci lame html output](images/fib-theme-lame-html.png "Fibonacci lame html output")
Let's tweak the html colors without touching the ansi colors:
```js
pen.installTheme('html', {
keyword: 'bold',
functionName: ['#403298', 'italic', 'bold'],
number: '#80417F'
});
```
![Fibonacci html output](images/fib-theme-html.png "Fibonacci html output")
You can even extend the current theme:
```js
pen.installTheme('html', {
functionName: ['#5B9832', 'bold']
});
```
![Fibonacci extended html output](images/fib-theme-extended-html.png "Fibonacci extended html output")
The theme is applied at serialization time, so you can change the theme
and serialize again with the theme applied without touching the
content of the pen.
## Aliases

@@ -428,0 +501,0 @@

@@ -1083,2 +1083,89 @@ /*global describe, it, beforeEach*/

});
describe('themes can be specified using the installTheme method', function () {
var pen;
beforeEach(function () {
pen = magicpen();
pen.installTheme('html', {
comment: ['#969896', 'italic'],
keyword: '#bf41ea'
});
pen.text('// This is a comment', 'comment').nl();
pen.keyword('function').sp().text('wat', 'methodDefinition').text('() {').nl();
pen.indentLines();
pen.i().text('console.').text('log', 'method').text('("wat");').nl();
pen.outdentLines();
pen.text('}');
pen.installTheme('ansi', {
comment: 'grey',
keyword: 'cyan'
});
});
it('when serializing to text the theme has no effect', function () {
expect(pen.toString('text'), 'to equal',
'// This is a comment\n' +
'function wat() {\n' +
' console.log("wat");\n' +
'}');
});
it('when serializing to html the output uses the html theme', function () {
expect(pen.toString('html'), 'to equal',
'<div style="font-family: monospace; white-space: nowrap">\n' +
' <div><span style="color: #969896; font-style: italic">//&nbsp;This&nbsp;is&nbsp;a&nbsp;comment</span></div>\n' +
' <div><span style="color: #bf41ea">function</span>&nbsp;wat()&nbsp;{</div>\n' +
' <div>&nbsp;&nbsp;console.log(&quot;wat&quot;);</div>\n' +
' <div>}</div>\n' +
'</div>');
});
it('when serializing to ansi the output uses the ansi theme', function () {
expect(pen.toString('ansi'), 'to equal',
'\x1B[90m// This is a comment\x1B[39m\n'+
'\x1B[36mfunction\x1B[39m wat() {\n'+
' console.log("wat");\n'+
'}');
});
describe('when the theme is extended', function () {
beforeEach(function () {
pen.installTheme(['ansi', 'html'], {
'methodDefinition': '#55ab40'
});
pen.installTheme('html', {
comment: ['#969896', 'italic']
});
pen.installTheme('ansi', {
comment: '#969896'
});
pen.installTheme({
method: ['#55ab40', 'bold']
});
});
it('when serializing to html the output uses the extended html theme', function () {
expect(pen.toString('html'), 'to equal',
'<div style="font-family: monospace; white-space: nowrap">\n' +
' <div><span style="color: #969896; font-style: italic">//&nbsp;This&nbsp;is&nbsp;a&nbsp;comment</span></div>\n' +
' <div><span style="color: #bf41ea">function</span>&nbsp;<span style="color: #55ab40">wat</span>()&nbsp;{</div>\n' +
' <div>&nbsp;&nbsp;console.<span style="color: #55ab40; font-weight: bold">log</span>(&quot;wat&quot;);</div>\n' +
' <div>}</div>\n' +
'</div>');
});
it('when serializing to ansi the output uses the extended ansi theme', function () {
expect(pen.toString('ansi'), 'to equal',
'\x1B[90m\u001b[38;5;246m// This is a comment\x1B[39m\n'+
'\x1B[36mfunction\x1B[39m \x1B[32m\x1B[38;5;113mwat\x1B[39m() {\n'+
' console.\x1B[32m\x1B[38;5;113m\x1B[1mlog\x1B[22m\x1B[39m("wat");\n'+
'}');
});
});
});
});
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc