RenderKid.js
7.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"use strict";
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
// Generated by CoffeeScript 2.5.1
var AnsiPainter, Layout, RenderKid, Styles, blockStyleApplier, cloneAndMergeDeep, inlineStyleApplier, isPlainObject, stripAnsi, terminalWidth, tools;
inlineStyleApplier = require('./renderKid/styleApplier/inline');
blockStyleApplier = require('./renderKid/styleApplier/block');
isPlainObject = require('lodash/isPlainObject');
var _require = require('./tools');
cloneAndMergeDeep = _require.cloneAndMergeDeep;
AnsiPainter = require('./AnsiPainter');
Styles = require('./renderKid/Styles');
Layout = require('./Layout');
tools = require('./tools');
stripAnsi = require('strip-ansi');
terminalWidth = require('./tools').getCols();
module.exports = RenderKid = function () {
var self;
var RenderKid = /*#__PURE__*/function () {
function RenderKid() {
var config = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
_classCallCheck(this, RenderKid);
this.tools = self.tools;
this._config = cloneAndMergeDeep(self._defaultConfig, config);
this._initStyles();
}
_createClass(RenderKid, [{
key: "_initStyles",
value: function _initStyles() {
return this._styles = new Styles();
}
}, {
key: "style",
value: function style() {
return this._styles.setRule.apply(this._styles, arguments);
}
}, {
key: "_getStyleFor",
value: function _getStyleFor(el) {
return this._styles.getStyleFor(el);
}
}, {
key: "render",
value: function render(input) {
var withColors = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
return this._paint(this._renderDom(this._toDom(input)), withColors);
}
}, {
key: "_toDom",
value: function _toDom(input) {
if (typeof input === 'string') {
return this._parse(input);
} else if (isPlainObject(input) || Array.isArray(input)) {
return this._objToDom(input);
} else {
throw Error("Invalid input type. Only strings, arrays and objects are accepted");
}
}
}, {
key: "_objToDom",
value: function _objToDom(o) {
var injectFakeRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
if (injectFakeRoot) {
o = {
body: o
};
}
return tools.objectToDom(o);
}
}, {
key: "_paint",
value: function _paint(text, withColors) {
var painted;
painted = AnsiPainter.paint(text);
if (withColors) {
return painted;
} else {
return stripAnsi(painted);
}
}
}, {
key: "_parse",
value: function _parse(string) {
var injectFakeRoot = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : true;
if (injectFakeRoot) {
string = '<body>' + string + '</body>';
}
return tools.stringToDom(string);
}
}, {
key: "_renderDom",
value: function _renderDom(dom) {
var bodyTag, layout, rootBlock;
bodyTag = dom[0];
layout = new Layout(this._config.layout);
rootBlock = layout.getRootBlock();
this._renderBlockNode(bodyTag, null, rootBlock);
return layout.get();
}
}, {
key: "_renderChildrenOf",
value: function _renderChildrenOf(parentNode, parentBlock) {
var i, len, node, nodes;
nodes = parentNode.children;
for (i = 0, len = nodes.length; i < len; i++) {
node = nodes[i];
this._renderNode(node, parentNode, parentBlock);
}
}
}, {
key: "_renderNode",
value: function _renderNode(node, parentNode, parentBlock) {
if (node.type === 'text') {
this._renderText(node, parentNode, parentBlock);
} else if (node.name === 'br') {
this._renderBr(node, parentNode, parentBlock);
} else if (this._isBlock(node)) {
this._renderBlockNode(node, parentNode, parentBlock);
} else if (this._isNone(node)) {
return;
} else {
this._renderInlineNode(node, parentNode, parentBlock);
}
}
}, {
key: "_renderText",
value: function _renderText(node, parentNode, parentBlock) {
var ref, text;
text = node.data;
text = text.replace(/\s+/g, ' '); // let's only trim if the parent is an inline element
if ((parentNode != null ? (ref = parentNode.styles) != null ? ref.display : void 0 : void 0) !== 'inline') {
text = text.trim();
}
if (text.length === 0) {
return;
}
text = text.replace(/&nl;/g, "\n");
return parentBlock.write(text);
}
}, {
key: "_renderBlockNode",
value: function _renderBlockNode(node, parentNode, parentBlock) {
var after, before, block, blockConfig;
var _blockStyleApplier$ap = blockStyleApplier.applyTo(node, this._getStyleFor(node));
before = _blockStyleApplier$ap.before;
after = _blockStyleApplier$ap.after;
blockConfig = _blockStyleApplier$ap.blockConfig;
block = parentBlock.openBlock(blockConfig);
if (before !== '') {
block.write(before);
}
this._renderChildrenOf(node, block);
if (after !== '') {
block.write(after);
}
return block.close();
}
}, {
key: "_renderInlineNode",
value: function _renderInlineNode(node, parentNode, parentBlock) {
var after, before;
var _inlineStyleApplier$a = inlineStyleApplier.applyTo(node, this._getStyleFor(node));
before = _inlineStyleApplier$a.before;
after = _inlineStyleApplier$a.after;
if (before !== '') {
parentBlock.write(before);
}
this._renderChildrenOf(node, parentBlock);
if (after !== '') {
return parentBlock.write(after);
}
}
}, {
key: "_renderBr",
value: function _renderBr(node, parentNode, parentBlock) {
return parentBlock.write("\n");
}
}, {
key: "_isBlock",
value: function _isBlock(node) {
return !(node.type === 'text' || node.name === 'br' || this._getStyleFor(node).display !== 'block');
}
}, {
key: "_isNone",
value: function _isNone(node) {
return !(node.type === 'text' || node.name === 'br' || this._getStyleFor(node).display !== 'none');
}
}]);
return RenderKid;
}();
;
self = RenderKid;
RenderKid.AnsiPainter = AnsiPainter;
RenderKid.Layout = Layout;
RenderKid.quote = tools.quote;
RenderKid.tools = tools;
RenderKid._defaultConfig = {
layout: {
terminalWidth: terminalWidth
}
};
return RenderKid;
}.call(void 0);