tools.js 2.6 KB

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