web 3d图形渲染器
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123 lines
3.2 KiB

  1. // Generated by CoffeeScript 1.9.3
  2. var AnsiPainter, styles, tags, tools,
  3. hasProp = {}.hasOwnProperty,
  4. slice = [].slice;
  5. tools = require('./tools');
  6. tags = require('./ansiPainter/tags');
  7. styles = require('./ansiPainter/styles');
  8. module.exports = AnsiPainter = (function() {
  9. var self;
  10. function AnsiPainter() {}
  11. AnsiPainter.tags = tags;
  12. AnsiPainter.prototype.paint = function(s) {
  13. return this._replaceSpecialStrings(this._renderDom(this._parse(s)));
  14. };
  15. AnsiPainter.prototype._replaceSpecialStrings = function(str) {
  16. return str.replace(/&sp;/g, ' ').replace(/&lt;/g, '<').replace(/&gt;/g, '>').replace(/&quot;/g, '"').replace(/&amp;/g, '&');
  17. };
  18. AnsiPainter.prototype._parse = function(string, injectFakeRoot) {
  19. if (injectFakeRoot == null) {
  20. injectFakeRoot = true;
  21. }
  22. if (injectFakeRoot) {
  23. string = '<none>' + string + '</none>';
  24. }
  25. return tools.toDom(string);
  26. };
  27. AnsiPainter.prototype._renderDom = function(dom) {
  28. var parentStyles;
  29. parentStyles = {
  30. bg: 'none',
  31. color: 'none'
  32. };
  33. return this._renderChildren(dom, parentStyles);
  34. };
  35. AnsiPainter.prototype._renderChildren = function(children, parentStyles) {
  36. var child, n, ret;
  37. ret = '';
  38. for (n in children) {
  39. if (!hasProp.call(children, n)) continue;
  40. child = children[n];
  41. ret += this._renderNode(child, parentStyles);
  42. }
  43. return ret;
  44. };
  45. AnsiPainter.prototype._renderNode = function(node, parentStyles) {
  46. if (node.type === 'text') {
  47. return this._renderTextNode(node, parentStyles);
  48. } else {
  49. return this._renderTag(node, parentStyles);
  50. }
  51. };
  52. AnsiPainter.prototype._renderTextNode = function(node, parentStyles) {
  53. return this._wrapInStyle(node.data, parentStyles);
  54. };
  55. AnsiPainter.prototype._wrapInStyle = function(str, style) {
  56. return styles.color(style.color) + styles.bg(style.bg) + str + styles.none();
  57. };
  58. AnsiPainter.prototype._renderTag = function(node, parentStyles) {
  59. var currentStyles, tagStyles;
  60. tagStyles = this._getStylesForTagName(node.name);
  61. currentStyles = this._mixStyles(parentStyles, tagStyles);
  62. return this._renderChildren(node.children, currentStyles);
  63. };
  64. AnsiPainter.prototype._mixStyles = function() {
  65. var final, i, key, len, style, styles, val;
  66. styles = 1 <= arguments.length ? slice.call(arguments, 0) : [];
  67. final = {};
  68. for (i = 0, len = styles.length; i < len; i++) {
  69. style = styles[i];
  70. for (key in style) {
  71. if (!hasProp.call(style, key)) continue;
  72. val = style[key];
  73. if ((final[key] == null) || val !== 'inherit') {
  74. final[key] = val;
  75. }
  76. }
  77. }
  78. return final;
  79. };
  80. AnsiPainter.prototype._getStylesForTagName = function(name) {
  81. if (tags[name] == null) {
  82. throw Error("Unknown tag name `" + name + "`");
  83. }
  84. return tags[name];
  85. };
  86. self = AnsiPainter;
  87. AnsiPainter.getInstance = function() {
  88. if (self._instance == null) {
  89. self._instance = new self;
  90. }
  91. return self._instance;
  92. };
  93. AnsiPainter.paint = function(str) {
  94. return self.getInstance().paint(str);
  95. };
  96. AnsiPainter.strip = function(s) {
  97. return s.replace(/\x1b\[[0-9]+m/g, '');
  98. };
  99. return AnsiPainter;
  100. })();