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