PageRenderTime 19ms CodeModel.GetById 11ms app.highlight 6ms RepoModel.GetById 1ms app.codeStats 0ms

/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
11require("app/JsToke.js");
12
13/**
14 * @class Turn source code into HTML with tokens marked for hilighting with CSS.
15 */
16function JsHilite(sourceCode, charset) {
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	if (!charset) charset = "utf-8";
25	
26	this.header = '<html><head><meta http-equiv="content-type" content="text/html; charset='+charset+'"> '+
27	"<style>\n\
28	.KEYW {color: #933;}\n\
29	.COMM {color: #bbb; font-style: italic;}\n\
30	.NUMB {color: #393;}\n\
31	.STRN {color: #393;}\n\
32	.REGX {color: #339;}\n\
33	.linenumber {border-right: 1px dotted #666; color: #666; font-style: normal;}\n\
34	</style></head><body><pre>";
35	this.footer = "</pre></body></html>";
36	this.showLinenumbers = true;
37}
38
39JsHilite.prototype.hilite = function() {
40	var hilited = this.tokenizer.tokenize().join("");
41	var linenumber = 1;
42	if (this.showLinenumbers) hilited = hilited.replace(/(^|\n)/g, function(m){return m+"<span class='linenumber'>"+((linenumber<10)? " ":"")+((linenumber<100)? " ":"")+(linenumber++)+"</span> "});
43	
44	return this.header+hilited+this.footer;
45}