RenderKid.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // Generated by CoffeeScript 1.9.3
  2. var AnsiPainter, Layout, RenderKid, Styles, blockStyleApplier, cloneAndMergeDeep, inlineStyleApplier, isPlainObject, stripAnsi, terminalWidth, tools;
  3. inlineStyleApplier = require('./renderKid/styleApplier/inline');
  4. blockStyleApplier = require('./renderKid/styleApplier/block');
  5. isPlainObject = require('lodash/isPlainObject');
  6. cloneAndMergeDeep = require('./tools').cloneAndMergeDeep;
  7. AnsiPainter = require('./AnsiPainter');
  8. Styles = require('./renderKid/Styles');
  9. Layout = require('./Layout');
  10. tools = require('./tools');
  11. stripAnsi = require('strip-ansi');
  12. terminalWidth = require('./tools').getCols();
  13. module.exports = RenderKid = (function() {
  14. var self;
  15. self = RenderKid;
  16. RenderKid.AnsiPainter = AnsiPainter;
  17. RenderKid.Layout = Layout;
  18. RenderKid.quote = tools.quote;
  19. RenderKid.tools = tools;
  20. RenderKid._defaultConfig = {
  21. layout: {
  22. terminalWidth: terminalWidth
  23. }
  24. };
  25. function RenderKid(config) {
  26. if (config == null) {
  27. config = {};
  28. }
  29. this.tools = self.tools;
  30. this._config = cloneAndMergeDeep(self._defaultConfig, config);
  31. this._initStyles();
  32. }
  33. RenderKid.prototype._initStyles = function() {
  34. return this._styles = new Styles;
  35. };
  36. RenderKid.prototype.style = function() {
  37. return this._styles.setRule.apply(this._styles, arguments);
  38. };
  39. RenderKid.prototype._getStyleFor = function(el) {
  40. return this._styles.getStyleFor(el);
  41. };
  42. RenderKid.prototype.render = function(input, withColors) {
  43. if (withColors == null) {
  44. withColors = true;
  45. }
  46. return this._paint(this._renderDom(this._toDom(input)), withColors);
  47. };
  48. RenderKid.prototype._toDom = function(input) {
  49. if (typeof input === 'string') {
  50. return this._parse(input);
  51. } else if (isPlainObject(input) || Array.isArray(input)) {
  52. return this._objToDom(input);
  53. } else {
  54. throw Error("Invalid input type. Only strings, arrays and objects are accepted");
  55. }
  56. };
  57. RenderKid.prototype._objToDom = function(o, injectFakeRoot) {
  58. if (injectFakeRoot == null) {
  59. injectFakeRoot = true;
  60. }
  61. if (injectFakeRoot) {
  62. o = {
  63. body: o
  64. };
  65. }
  66. return tools.objectToDom(o);
  67. };
  68. RenderKid.prototype._paint = function(text, withColors) {
  69. var painted;
  70. painted = AnsiPainter.paint(text);
  71. if (withColors) {
  72. return painted;
  73. } else {
  74. return stripAnsi(painted);
  75. }
  76. };
  77. RenderKid.prototype._parse = function(string, injectFakeRoot) {
  78. if (injectFakeRoot == null) {
  79. injectFakeRoot = true;
  80. }
  81. if (injectFakeRoot) {
  82. string = '<body>' + string + '</body>';
  83. }
  84. return tools.stringToDom(string);
  85. };
  86. RenderKid.prototype._renderDom = function(dom) {
  87. var bodyTag, layout, rootBlock;
  88. bodyTag = dom[0];
  89. layout = new Layout(this._config.layout);
  90. rootBlock = layout.getRootBlock();
  91. this._renderBlockNode(bodyTag, null, rootBlock);
  92. return layout.get();
  93. };
  94. RenderKid.prototype._renderChildrenOf = function(parentNode, parentBlock) {
  95. var i, len, node, nodes;
  96. nodes = parentNode.children;
  97. for (i = 0, len = nodes.length; i < len; i++) {
  98. node = nodes[i];
  99. this._renderNode(node, parentNode, parentBlock);
  100. }
  101. };
  102. RenderKid.prototype._renderNode = function(node, parentNode, parentBlock) {
  103. if (node.type === 'text') {
  104. this._renderText(node, parentNode, parentBlock);
  105. } else if (node.name === 'br') {
  106. this._renderBr(node, parentNode, parentBlock);
  107. } else if (this._isBlock(node)) {
  108. this._renderBlockNode(node, parentNode, parentBlock);
  109. } else if (this._isNone(node)) {
  110. return;
  111. } else {
  112. this._renderInlineNode(node, parentNode, parentBlock);
  113. }
  114. };
  115. RenderKid.prototype._renderText = function(node, parentNode, parentBlock) {
  116. var ref, text;
  117. text = node.data;
  118. text = text.replace(/\s+/g, ' ');
  119. if ((parentNode != null ? (ref = parentNode.styles) != null ? ref.display : void 0 : void 0) !== 'inline') {
  120. text = text.trim();
  121. }
  122. if (text.length === 0) {
  123. return;
  124. }
  125. text = text.replace(/&nl;/g, "\n");
  126. return parentBlock.write(text);
  127. };
  128. RenderKid.prototype._renderBlockNode = function(node, parentNode, parentBlock) {
  129. var after, before, block, blockConfig, ref;
  130. ref = blockStyleApplier.applyTo(node, this._getStyleFor(node)), before = ref.before, after = ref.after, blockConfig = ref.blockConfig;
  131. block = parentBlock.openBlock(blockConfig);
  132. if (before !== '') {
  133. block.write(before);
  134. }
  135. this._renderChildrenOf(node, block);
  136. if (after !== '') {
  137. block.write(after);
  138. }
  139. return block.close();
  140. };
  141. RenderKid.prototype._renderInlineNode = function(node, parentNode, parentBlock) {
  142. var after, before, ref;
  143. ref = inlineStyleApplier.applyTo(node, this._getStyleFor(node)), before = ref.before, after = ref.after;
  144. if (before !== '') {
  145. parentBlock.write(before);
  146. }
  147. this._renderChildrenOf(node, parentBlock);
  148. if (after !== '') {
  149. return parentBlock.write(after);
  150. }
  151. };
  152. RenderKid.prototype._renderBr = function(node, parentNode, parentBlock) {
  153. return parentBlock.write("\n");
  154. };
  155. RenderKid.prototype._isBlock = function(node) {
  156. return !(node.type === 'text' || node.name === 'br' || this._getStyleFor(node).display !== 'block');
  157. };
  158. RenderKid.prototype._isNone = function(node) {
  159. return !(node.type === 'text' || node.name === 'br' || this._getStyleFor(node).display !== 'none');
  160. };
  161. return RenderKid;
  162. })();