RenderKid.js 7.4 KB

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