/tags/jsdoc_toolkit-1.3.3/app/JsHilite.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 42 lines · 26 code · 4 blank · 12 comment · 1 complexity · 86cdd8a986306186ee7267aab4df5bdb MD5 · raw file

  1. /**
  2. * @fileOverview
  3. * @name JsHilite
  4. * @author Michael Mathews micmath@gmail.com
  5. * @url $HeadURL: http://jsdoc-toolkit.googlecode.com/svn/tags/jsdoc_toolkit-1.3.3/app/JsHilite.js $
  6. * @revision $Id: JsHilite.js 213 2007-08-22 10:21:50Z micmath $
  7. * @license <a href="http://en.wikipedia.org/wiki/MIT_License">X11/MIT License</a>
  8. * (See the accompanying README file for full details.)
  9. */
  10. require("app/JsToke.js");
  11. /**
  12. * @class Turn source code into HTML with tokens marked for hilighting with CSS.
  13. */
  14. function JsHilite(sourceCode) {
  15. this.tokenizer = new TokenReader(sourceCode);
  16. this.tokenizer.keepComments = true;
  17. this.tokenizer.keepDocs = true;
  18. this.tokenizer.keepWhite = true;
  19. Token.prototype.toString = function() {
  20. return "<span class=\""+this.type+"\">"+this.data.replace(/</g, "&lt;")+"</span>";
  21. }
  22. this.header = "<html><head><style>\n\
  23. .KEYW {color: #933;}\n\
  24. .COMM {color: #bbb; font-style: italic;}\n\
  25. .NUMB {color: #393;}\n\
  26. .STRN {color: #393;}\n\
  27. .REGX {color: #339;}\n\
  28. .linenumber {border-right: 1px dotted #666; color: #666; font-style: normal;}\n\
  29. </style></head><body><pre>";
  30. this.footer = "</pre></body></html>";
  31. this.showLinenumbers = true;
  32. }
  33. JsHilite.prototype.hilite = function() {
  34. var hilited = this.tokenizer.tokenize().join("");
  35. var linenumber = 1;
  36. if (this.showLinenumbers) hilited = hilited.replace(/(^|\n)/g, function(m){return m+"<span class='linenumber'>"+((linenumber<10)? " ":"")+((linenumber<100)? " ":"")+(linenumber++)+"</span> "});
  37. return this.header+hilited+this.footer;
  38. }