RenderKid.js 5.3 KB

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