thirdparty/jsdoctoolkit/app/plugins/publishSrcHilite.js
changeset 3041 c8f47f0b6697
equal deleted inserted replaced
3040:8f9580309846 3041:c8f47f0b6697
       
     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 		
       
    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 
       
    24 function 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 
       
    54 JsHilite.cache = {};
       
    55 
       
    56 JsHilite.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 }