/branches/version1.x/app/JsHilite.js

http://jsdoc-toolkit.googlecode.com/ · JavaScript · 45 lines · 28 code · 5 blank · 12 comment · 2 complexity · cc1535f1e9efc202a2c18537a3b75efd 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/branches/version1.x/app/JsHilite.js $
  6. * @revision $Id: JsHilite.js 300 2007-11-11 16:51:53Z 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, charset) {
  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. if (!charset) charset = "utf-8";
  23. this.header = '<html><head><meta http-equiv="content-type" content="text/html; charset='+charset+'"> '+
  24. "<style>\n\
  25. .KEYW {color: #933;}\n\
  26. .COMM {color: #bbb; font-style: italic;}\n\
  27. .NUMB {color: #393;}\n\
  28. .STRN {color: #393;}\n\
  29. .REGX {color: #339;}\n\
  30. .linenumber {border-right: 1px dotted #666; color: #666; font-style: normal;}\n\
  31. </style></head><body><pre>";
  32. this.footer = "</pre></body></html>";
  33. this.showLinenumbers = true;
  34. }
  35. JsHilite.prototype.hilite = function() {
  36. var hilited = this.tokenizer.tokenize().join("");
  37. var linenumber = 1;
  38. if (this.showLinenumbers) hilited = hilited.replace(/(^|\n)/g, function(m){return m+"<span class='linenumber'>"+((linenumber<10)? " ":"")+((linenumber<100)? " ":"")+(linenumber++)+"</span> "});
  39. return this.header+hilited+this.footer;
  40. }