PageRenderTime 8ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/trunk/jsdoc-toolkit/app/plugins/publishSrcHilite.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 62 lines | 49 code | 12 blank | 1 comment | 4 complexity | 6cfcbe8f69e6591477b33c18f592c4f8 MD5 | raw file
  1. JSDOC.PluginManager.registerPlugin(
  2. "JSDOC.publishSrcHilite",
  3. {
  4. onPublishSrc: function(src) {
  5. if (src.path in JsHilite.cache) {
  6. return; // already generated src code
  7. }
  8. else JsHilite.cache[src.path] = true;
  9. try {
  10. var sourceCode = IO.readFile(src.path);
  11. }
  12. catch(e) {
  13. print(e.message);
  14. quit();
  15. }
  16. var hiliter = new JsHilite(sourceCode, src.charset);
  17. src.hilited = hiliter.hilite();
  18. }
  19. }
  20. );
  21. function JsHilite(src, charset) {
  22. var tr = new JSDOC.TokenReader();
  23. tr.keepComments = true;
  24. tr.keepDocs = true;
  25. tr.keepWhite = true;
  26. this.tokens = tr.tokenize(new JSDOC.TextStream(src));
  27. // TODO is redefining toString() the best way?
  28. JSDOC.Token.prototype.toString = function() {
  29. return "<span class=\""+this.type+"\">"+this.data.replace(/</g, "&lt;")+"</span>";
  30. }
  31. if (!charset) charset = "utf-8";
  32. this.header = '<html><head><meta http-equiv="content-type" content="text/html; charset='+charset+'"> '+
  33. "<style>\n\
  34. .KEYW {color: #933;}\n\
  35. .COMM {color: #bbb; font-style: italic;}\n\
  36. .NUMB {color: #393;}\n\
  37. .STRN {color: #393;}\n\
  38. .REGX {color: #339;}\n\
  39. .line {border-right: 1px dotted #666; color: #666; font-style: normal;}\n\
  40. </style></head><body><pre>";
  41. this.footer = "</pre></body></html>";
  42. this.showLinenumbers = true;
  43. }
  44. JsHilite.cache = {};
  45. JsHilite.prototype.hilite = function() {
  46. var hilited = this.tokens.join("");
  47. var line = 1;
  48. if (this.showLinenumbers) hilited = hilited.replace(/(^|\n)/g, function(m){return m+"<span class='line'>"+((line<10)? " ":"")+((line<100)? " ":"")+(line++)+"</span> "});
  49. return this.header+hilited+this.footer;
  50. }