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.

95 lines
2.7 KiB

  1. // Generated by CoffeeScript 1.9.3
  2. var cloneDeep, htmlparser, isPlainObject, merge, objectToDom, self;
  3. htmlparser = require('htmlparser2');
  4. objectToDom = require('dom-converter').objectToDom;
  5. merge = require('lodash/merge');
  6. cloneDeep = require('lodash/cloneDeep');
  7. isPlainObject = require('lodash/isPlainObject');
  8. module.exports = self = {
  9. repeatString: function(str, times) {
  10. var i, j, output, ref;
  11. output = '';
  12. for (i = j = 0, ref = times; 0 <= ref ? j < ref : j > ref; i = 0 <= ref ? ++j : --j) {
  13. output += str;
  14. }
  15. return output;
  16. },
  17. cloneAndMergeDeep: function(base, toAppend) {
  18. return merge(cloneDeep(base), toAppend);
  19. },
  20. toDom: function(subject) {
  21. if (typeof subject === 'string') {
  22. return self.stringToDom(subject);
  23. } else if (isPlainObject(subject)) {
  24. return self._objectToDom(subject);
  25. } else {
  26. throw Error("tools.toDom() only supports strings and objects");
  27. }
  28. },
  29. stringToDom: function(string) {
  30. var handler, parser;
  31. handler = new htmlparser.DomHandler;
  32. parser = new htmlparser.Parser(handler);
  33. parser.write(string);
  34. parser.end();
  35. return handler.dom;
  36. },
  37. _fixQuotesInDom: function(input) {
  38. var j, len, node;
  39. if (Array.isArray(input)) {
  40. for (j = 0, len = input.length; j < len; j++) {
  41. node = input[j];
  42. self._fixQuotesInDom(node);
  43. }
  44. return input;
  45. }
  46. node = input;
  47. if (node.type === 'text') {
  48. return node.data = self._quoteNodeText(node.data);
  49. } else {
  50. return self._fixQuotesInDom(node.children);
  51. }
  52. },
  53. objectToDom: function(o) {
  54. if (!Array.isArray(o)) {
  55. if (!isPlainObject(o)) {
  56. throw Error("objectToDom() only accepts a bare object or an array");
  57. }
  58. }
  59. return self._fixQuotesInDom(objectToDom(o));
  60. },
  61. quote: function(str) {
  62. return String(str).replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;').replace(/\ /g, '&sp;').replace(/\n/g, '<br />');
  63. },
  64. _quoteNodeText: function(text) {
  65. return String(text).replace(/\&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/\"/g, '&quot;').replace(/\ /g, '&sp;').replace(/\n/g, "&nl;");
  66. },
  67. getCols: function() {
  68. var cols, tty;
  69. tty = require('tty');
  70. cols = (function() {
  71. try {
  72. if (tty.isatty(1) && tty.isatty(2)) {
  73. if (process.stdout.getWindowSize) {
  74. return process.stdout.getWindowSize(1)[0];
  75. } else if (tty.getWindowSize) {
  76. return tty.getWindowSize()[1];
  77. } else if (process.stdout.columns) {
  78. return process.stdout.columns;
  79. }
  80. }
  81. } catch (_error) {}
  82. })();
  83. if (typeof cols === 'number' && cols > 30) {
  84. return cols;
  85. } else {
  86. return 80;
  87. }
  88. }
  89. };