ANSIdom.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. var path = require('path'),
  2. jsdom = require('jsdom'),
  3. fs = require('fs');
  4. var ANSIdom = exports.ANSIdom = function(){
  5. //
  6. // Require DOM element to ANSI mappings
  7. //
  8. this.elements = require('./ANSIdom/elements');
  9. this.helpers = require('./ANSIdom/helpers');
  10. return this;
  11. };
  12. ANSIdom.prototype.init = function (callback) {
  13. var self = this;
  14. jsdom.env( "<!DOCTYPE html>\n<html><body></body></html>", ['./jquery.js'], function (err, window) {
  15. if (err) {
  16. return callback(err);
  17. }
  18. self.window = window;
  19. callback(null);
  20. });
  21. };
  22. ANSIdom.prototype.render = function (html, stream, box) {
  23. var charm = require('charm')(stream),
  24. window = this.window,
  25. $ = this.window.$,
  26. self = this;
  27. //
  28. // Remark: Not really the right place for this, but it works
  29. //
  30. charm.on('^C', process.exit);
  31. var div = window.document.createElement('div');
  32. html = html.replace(/^\W+/g, "");
  33. html = html.replace(/\n/g, "");
  34. div.innerHTML = html;
  35. charm.reset();
  36. charm.down(2);
  37. var rowCount = self.elements.render($, $('body div', div), charm);
  38. if(box === true){
  39. self.helpers.box(0, 0, 15, 60, charm);
  40. }
  41. charm.column(1).down(3).write('');
  42. charm.cursor(0,60);
  43. //charm.destroy();
  44. };