PageRenderTime 18ms CodeModel.GetById 10ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
11require("app/JsToke.js");
12
13/**
14 * @class Turn source code into HTML with tokens marked for hilighting with CSS.
15 */
16function JsHilite(sourceCode) {
17	this.tokenizer = new TokenReader(sourceCode);
18	this.tokenizer.keepComments = true;
19	this.tokenizer.keepDocs = true;
20	this.tokenizer.keepWhite = true;
21	Token.prototype.toString = function() { 
22		return "<span class=\""+this.type+"\">"+this.data.replace(/</g, "&lt;")+"</span>";
23	}
24	this.header = "<html><head><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
36JsHilite.prototype.hilite = function() {
37	var hilited = this.tokenizer.tokenize().join("");
38	var linenumber = 1;
39	if (this.showLinenumbers) hilited = hilited.replace(/(^|\n)/g, function(m){return m+"<span class='linenumber'>"+((linenumber<10)? " ":"")+((linenumber<100)? " ":"")+(linenumber++)+"</span> "});
40	
41	return this.header+hilited+this.footer;
42}