RenderKid.js
5.34 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
// Generated by CoffeeScript 1.9.3
var AnsiPainter, Layout, RenderKid, Styles, blockStyleApplier, inlineStyleApplier, object, stripAnsi, terminalWidth, tools;
inlineStyleApplier = require('./renderKid/styleApplier/inline');
blockStyleApplier = require('./renderKid/styleApplier/block');
AnsiPainter = require('./AnsiPainter');
Styles = require('./renderKid/Styles');
Layout = require('./Layout');
tools = require('./tools');
object = require('utila').object;
stripAnsi = require('strip-ansi');
terminalWidth = require('./tools').getCols();
module.exports = RenderKid = (function() {
var self;
self = RenderKid;
RenderKid.AnsiPainter = AnsiPainter;
RenderKid.Layout = Layout;
RenderKid.quote = tools.quote;
RenderKid.tools = tools;
RenderKid._defaultConfig = {
layout: {
terminalWidth: terminalWidth
}
};
function RenderKid(config) {
if (config == null) {
config = {};
}
this.tools = self.tools;
this._config = object.append(self._defaultConfig, config);
this._initStyles();
}
RenderKid.prototype._initStyles = function() {
return this._styles = new Styles;
};
RenderKid.prototype.style = function() {
return this._styles.setRule.apply(this._styles, arguments);
};
RenderKid.prototype._getStyleFor = function(el) {
return this._styles.getStyleFor(el);
};
RenderKid.prototype.render = function(input, withColors) {
if (withColors == null) {
withColors = true;
}
return this._paint(this._renderDom(this._toDom(input)), withColors);
};
RenderKid.prototype._toDom = function(input) {
if (typeof input === 'string') {
return this._parse(input);
} else if (object.isBareObject(input) || Array.isArray(input)) {
return this._objToDom(input);
} else {
throw Error("Invalid input type. Only strings, arrays and objects are accepted");
}
};
RenderKid.prototype._objToDom = function(o, injectFakeRoot) {
if (injectFakeRoot == null) {
injectFakeRoot = true;
}
if (injectFakeRoot) {
o = {
body: o
};
}
return tools.objectToDom(o);
};
RenderKid.prototype._paint = function(text, withColors) {
var painted;
painted = AnsiPainter.paint(text);
if (withColors) {
return painted;
} else {
return stripAnsi(painted);
}
};
RenderKid.prototype._parse = function(string, injectFakeRoot) {
if (injectFakeRoot == null) {
injectFakeRoot = true;
}
if (injectFakeRoot) {
string = '<body>' + string + '</body>';
}
return tools.stringToDom(string);
};
RenderKid.prototype._renderDom = function(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();
};
RenderKid.prototype._renderChildrenOf = function(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);
}
};
RenderKid.prototype._renderNode = function(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);
}
};
RenderKid.prototype._renderText = function(node, parentNode, parentBlock) {
var ref, text;
text = node.data;
text = text.replace(/\s+/g, ' ');
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);
};
RenderKid.prototype._renderBlockNode = function(node, parentNode, parentBlock) {
var after, before, block, blockConfig, ref;
ref = blockStyleApplier.applyTo(node, this._getStyleFor(node)), before = ref.before, after = ref.after, blockConfig = ref.blockConfig;
block = parentBlock.openBlock(blockConfig);
if (before !== '') {
block.write(before);
}
this._renderChildrenOf(node, block);
if (after !== '') {
block.write(after);
}
return block.close();
};
RenderKid.prototype._renderInlineNode = function(node, parentNode, parentBlock) {
var after, before, ref;
ref = inlineStyleApplier.applyTo(node, this._getStyleFor(node)), before = ref.before, after = ref.after;
if (before !== '') {
parentBlock.write(before);
}
this._renderChildrenOf(node, parentBlock);
if (after !== '') {
return parentBlock.write(after);
}
};
RenderKid.prototype._renderBr = function(node, parentNode, parentBlock) {
return parentBlock.write("\n");
};
RenderKid.prototype._isBlock = function(node) {
return !(node.type === 'text' || node.name === 'br' || this._getStyleFor(node).display !== 'block');
};
RenderKid.prototype._isNone = function(node) {
return !(node.type === 'text' || node.name === 'br' || this._getStyleFor(node).display !== 'none');
};
return RenderKid;
})();