PageRenderTime 18ms CodeModel.GetById 2ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 1ms

/tags/jsdoc_toolkit-2.3.2/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
 1JSDOC.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		
10			try {
11				var sourceCode = IO.readFile(src.path);
12			}
13			catch(e) {
14				print(e.message);
15				quit();
16			}
17
18			var hiliter = new JsHilite(sourceCode, src.charset);
19			src.hilited = hiliter.hilite();
20		}
21	}
22);
23
24function JsHilite(src, charset) {
25
26	var tr = new JSDOC.TokenReader();
27	
28	tr.keepComments = true;
29	tr.keepDocs = true;
30	tr.keepWhite = true;
31	
32	this.tokens = tr.tokenize(new JSDOC.TextStream(src));
33	
34	// TODO is redefining toString() the best way?
35	JSDOC.Token.prototype.toString = function() { 
36		return "<span class=\""+this.type+"\">"+this.data.replace(/</g, "&lt;")+"</span>";
37	}
38	
39	if (!charset) charset = "utf-8";
40	
41	this.header = '<html><head><meta http-equiv="content-type" content="text/html; charset='+charset+'"> '+
42	"<style>\n\
43	.KEYW {color: #933;}\n\
44	.COMM {color: #bbb; font-style: italic;}\n\
45	.NUMB {color: #393;}\n\
46	.STRN {color: #393;}\n\
47	.REGX {color: #339;}\n\
48	.line {border-right: 1px dotted #666; color: #666; font-style: normal;}\n\
49	</style></head><body><pre>";
50	this.footer = "</pre></body></html>";
51	this.showLinenumbers = true;
52}
53
54JsHilite.cache = {};
55
56JsHilite.prototype.hilite = function() {
57	var hilited = this.tokens.join("");
58	var line = 1;
59	if (this.showLinenumbers) hilited = hilited.replace(/(^|\n)/g, function(m){return m+"<span class='line'>"+((line<10)? " ":"")+((line<100)? " ":"")+(line++)+"</span> "});
60	
61	return this.header+hilited+this.footer;
62}