PageRenderTime 45ms CodeModel.GetById 15ms app.highlight 24ms RepoModel.GetById 2ms app.codeStats 0ms

/bin/std/tools/haxedoc/HtmlPrinter.hx

http://github.com/Yoomee/clippy
Haxe | 570 lines | 524 code | 39 blank | 7 comment | 158 complexity | 4a2f026728bd2a712864346d5a64aed4 MD5 | raw file
  1package tools.haxedoc;
  2import haxe.rtti.CType;
  3
  4class HtmlPrinter {
  5
  6	static function loadTemplate() {
  7		var hdata = try
  8			// load in current local/web directory
  9			neko.io.File.getContent(neko.Web.getCwd()+"template.xml")
 10		catch( e : Dynamic ) try {
 11			// load in haxe subdirectory (TODO : make it work on linux/osx)
 12			var p = ~/[\/\\]/g.split(neko.Sys.executablePath());
 13			p.pop();
 14			neko.io.File.getContent(p.join("/")+"/std/tools/template.xml");
 15		} catch( e : Dynamic )
 16			default_template;
 17		return Xml.parse(hdata);
 18	}
 19
 20	static var default_template = "<html><body><data/></body></html>";
 21	static var template = loadTemplate();
 22
 23	public var baseUrl : String;
 24	var indexUrl : String;
 25	var fileExtension : String;
 26	var curpackage : String;
 27	var filters : List<String>;
 28	var typeParams : TypeParams;
 29
 30	public function new( baseUrl, fileExtension, indexUrl ) {
 31		this.baseUrl = baseUrl;
 32		this.fileExtension = fileExtension;
 33		this.indexUrl = indexUrl;
 34		filters = new List();
 35		typeParams = new Array();
 36	}
 37
 38	public function find( t : TypeRoot, path : Array<String>, pos : Int ) {
 39		var name = path[pos];
 40		var pack = (pos != path.length - 1);
 41		var def = null;
 42		for( c in t )
 43			switch( c ) {
 44			case TPackage(pname,_,subs):
 45				if( name == pname ) {
 46					if( pack )
 47						return find(subs,path,pos+1);
 48					def = c;
 49				}
 50			default:
 51				if( pack ) continue;
 52				var inf = TypeApi.typeInfos(c);
 53				if( inf.path.toLowerCase() == path.join(".") )
 54					return c;
 55			}
 56		return def;
 57	}
 58
 59
 60	public dynamic function output(str) {
 61		neko.Lib.print(str);
 62	}
 63
 64	public function addFilter(f) {
 65		filters.add(f);
 66	}
 67
 68	public function print(str, ?params : Dynamic ) {
 69		if( params != null )
 70			for( f in Reflect.fields(params) )
 71				str = StringTools.replace(str, "$"+f, Std.string(Reflect.field(params, f)));
 72		output(str);
 73	}
 74
 75	public function process(t) {
 76		processHtml(t,template);
 77	}
 78
 79	public function filtered( path : Path, isPackage : Bool ) {
 80		if( isPackage && path == "Remoting" )
 81			return true;
 82		if( StringTools.endsWith(path,"__") )
 83			return true;
 84		if( filters.isEmpty() )
 85			return false;
 86		for( x in filters )
 87			if( StringTools.startsWith(path,x) )
 88				return false;
 89		return true;
 90	}
 91
 92	function makeUrl( url, text, css ) {
 93		return "<a href=\"" + baseUrl + url + fileExtension + "\" class=\""+css+"\">"+text+"</a>";
 94	}
 95
 96	function prefix( arr : Array<String>, path : String ) {
 97		var arr = arr.copy();
 98		for( i in 0...arr.length )
 99			arr[i] = path + "." + arr[i];
100		return arr;
101	}
102
103	function makePathUrl( path : Path, css ) {
104		var p = path.split(".");
105		var name = p.pop();
106		var local = (p.join(".") == curpackage);
107		for( x in typeParams )
108			if( x == path )
109				return name;
110		p.push(name);
111		if( local )
112			return makeUrl(p.join("/"),name,css);
113		return makeUrl(p.join("/"),fmtpath(path),css);
114	}
115
116	function fmtpath(path : String) {
117		if( path.substr(0,7) == "flash9." )
118			return "flash."+path.substr(7);
119		var pack = path.split(".");
120		if( pack.length > 1 && pack[pack.length-2].charAt(0) == "_" ) {
121			pack.splice(-2,1);
122			path = pack.join(".");
123		}
124		return path;
125	}
126
127	public function processHtml(t,html : Xml) {
128		var ht = html.nodeType;
129		if( ht == Xml.Element ) {
130			if( html.nodeName == "data" ) {
131				processPage(t);
132				return;
133			}
134			if( !html.iterator().hasNext() ) {
135				print(html.toString());
136				return;
137			}
138			print("<");
139			print(html.nodeName);
140			for( k in html.attributes() )
141				print(" "+k+"=\""+html.get(k)+"\"");
142			print(">");
143			for( x in html )
144				processHtml(t,x);
145			print("</"+html.nodeName+">");
146		} else if( ht == Xml.Document )
147			for( x in html )
148				processHtml(t,x);
149		else
150			print(html.toString());
151	}
152
153	public function processPage(t) {
154		switch(t) {
155		case TPackage(p,full,list):
156			processPackage(p,list);
157		default:
158			var head = '<a href="#" onclick="javascript:history.back(-1); return false" class="index">Back</a> | '+makeUrl(indexUrl,"Index","index");
159			print(head);
160			var inf = TypeApi.typeInfos(t);
161			typeParams = prefix(inf.params,inf.path);
162			var p = inf.path.split(".");
163			p.pop();
164			curpackage = p.join(".");
165			switch(t) {
166			case TClassdecl(c): processClass(c);
167			case TEnumdecl(e): processEnum(e);
168			case TTypedecl(t): processTypedef(t);
169			case TPackage(_,_,_): throw "ASSERT";
170			}
171			print(head);
172		}
173	}
174
175	function processPackage(name,list : Array<TypeTree> ) {
176		print('<ul class="entry">');
177		for( e in list ) {
178			switch e {
179			case TPackage(name,full,list):
180				if( filtered(full,true) )
181					continue;
182				var isPrivate = name.charAt(0) == "_";
183				if( !isPrivate )
184					print('<li><a href="#" class="package" onclick="return toggle(\'$id\')">$name</a><div id="$id" class="package_content">', { id : full.split(".").join("_"), name : name });
185				var old = curpackage;
186				curpackage = full;
187				processPackage(name,list);
188				curpackage = old;
189				if( !isPrivate )
190					print("</div></li>");
191			default:
192				var i = TypeApi.typeInfos(e);
193				if( i.isPrivate || i.path == "@Main" || filtered(i.path,false) )
194					continue;
195				print("<li>"+makePathUrl(i.path,"entry")+"</li>");
196			}
197		}
198		print("</ul>");
199	}
200
201	function processInfos(t : TypeInfos) {
202		if( t.module != null )
203			print('<div class="importmod">import $module</div>',{ module : t.module });
204		if( !t.platforms.isEmpty() ) {
205			print('<div class="platforms">Available in ');
206			display(t.platforms,output,", ");
207			print('</div>');
208		}
209		if( t.doc != null ) {
210			print('<div class="classdoc">');
211			processDoc(t.doc);
212			print('</div>');
213		}
214	}
215
216	function processClass(c : Classdef) {
217		print('<div class="classname">');
218		if( c.isExtern )
219			keyword("extern");
220		if( c.isPrivate )
221			keyword("private");
222		if( c.isInterface )
223			keyword("interface");
224		else
225			keyword("class");
226		print(fmtpath(c.path));
227		if( c.params.length != 0 ) {
228			print("&lt;");
229			print(c.params.join(", "));
230			print("&gt;");
231		}
232		print('</div>');
233		if( c.superClass != null ) {
234			print('<div class="extends">extends ');
235			processPath(c.superClass.path,c.superClass.params);
236			print('</div>');
237		}
238		for( i in c.interfaces ) {
239			print('<div class="implements">implements ');
240			processPath(i.path,i.params);
241			print('</div>');
242		}
243		if( c.tdynamic != null ) {
244			var d = new List();
245			d.add(c.tdynamic);
246			print('<div class="implements">implements ');
247			processPath("Dynamic",d);
248			print('</div>');
249		}
250		processInfos(c);
251		print('<dl>');
252		for( f in c.fields )
253			processClassField(c.platforms,f,false);
254		for( f in c.statics )
255			processClassField(c.platforms,f,true);
256		print('</dl>');
257	}
258
259	function processClassField(platforms : Platforms,f : ClassField,stat) {
260		if( !f.isPublic || f.isOverride )
261			return;
262		var oldParams = typeParams;
263		if( f.params != null )
264			typeParams = typeParams.concat(prefix(f.params,f.name));
265		print('<dt>');
266		if( stat ) keyword("static");
267		var isMethod = false;
268		var isInline = (f.get == RInline && f.set == RNo);
269		switch( f.type ) {
270		case CFunction(args,ret):
271			if( (f.get == RNormal && (f.set == RMethod || f.set == RDynamic)) || isInline ) {
272				isMethod = true;
273				if( f.set == RDynamic )
274					keyword("dynamic");
275				if( isInline )
276					keyword("inline");
277				keyword("function");
278				print(f.name);
279				if( f.params != null )
280					print("&lt;"+f.params.join(", ")+"&gt;");
281				print("(");
282				var me = this;
283				display(args,function(a) {
284					if( a.opt )
285						me.print("?");
286					if( a.name != null && a.name != "" ) {
287						me.print(a.name);
288						me.print(" : ");
289					}
290					me.processType(a.t);
291				},", ");
292				print(") : ");
293				processType(ret);
294			}
295		default:
296		}
297		if( !isMethod ) {
298			if( isInline )
299				keyword("inline");
300			keyword("var");
301			print(f.name);
302			if( !isInline && (f.get != RNormal || f.set != RNormal) )
303				print("("+rightsStr(f,true,f.get)+","+rightsStr(f,false,f.set)+")");
304			print(" : ");
305			processType(f.type);
306		}
307		if( f.platforms.length != platforms.length ) {
308			print('<div class="platforms">Available in ');
309			display(f.platforms,output,", ");
310			print('</div>');
311		}
312		print('</dt>');
313		print('<dd>');
314		processDoc(f.doc);
315		print('</dd>');
316		if( f.params != null )
317			typeParams = oldParams;
318	}
319
320	function processEnum(e : Enumdef) {
321		print('<div class="classname">');
322		if( e.isExtern )
323			keyword("extern");
324		if( e.isPrivate )
325			keyword("private");
326		keyword("enum");
327		print(fmtpath(e.path));
328		if( e.params.length != 0 ) {
329			print("&lt;");
330			print(e.params.join(", "));
331			print("&gt;");
332		}
333		print('</div>');
334		processInfos(e);
335		print('<dl>');
336		for( c in e.constructors ) {
337			print('<dt>');
338			print(c.name);
339			if( c.args != null ) {
340				print("(");
341				var me = this;
342				display(c.args,function(a) {
343					if( a.opt )
344						me.print("?");
345					me.print(a.name);
346					me.print(" : ");
347					me.processType(a.t);
348				},",");
349				print(")");
350			}
351			print("</dt>");
352			print("<dd>");
353			processDoc(c.doc);
354			print("</dd>");
355		}
356		print('</dl>');
357	}
358
359	function processTypedef(t : Typedef) {
360		print('<div class="classname">');
361		if( t.isPrivate )
362			keyword("private");
363		keyword("typedef");
364		print(fmtpath(t.path));
365		if( t.params.length != 0 ) {
366			print("&lt;");
367			print(t.params.join(", "));
368			print("&gt;");
369		}
370		print('</div>');
371		processInfos(t);
372		if( t.platforms.length == 0 ) {
373			processTypedefType(t.type,t.platforms,t.platforms);
374			return;
375		}
376		var platforms = new List();
377		for( p in t.platforms )
378			platforms.add(p);
379		for( p in t.types.keys() ) {
380			var td = t.types.get(p);
381			var support = new List();
382			for( p2 in platforms )
383				if( TypeApi.typeEq(td,t.types.get(p2)) ) {
384					platforms.remove(p2);
385					support.add(p2);
386				}
387			if( support.length == 0 )
388				continue;
389			processTypedefType(td,t.platforms,support);
390		}
391	}
392
393	function processTypedefType(t,all,platforms) {
394		switch( t ) {
395		case CAnonymous(fields):
396			print('<dl>');
397			for( f in fields ) {
398				processClassField(all,{
399					name : f.name,
400					type : f.t,
401					isPublic : true,
402					isOverride : false,
403					doc : null,
404					get : RNormal,
405					set : RNormal,
406					params : null,
407					platforms : platforms,
408				},false);
409			}
410			print('</dl>');
411		default:
412			if( all.length != platforms.length ) {
413				print('<div class="platforms">Defined in ');
414				display(platforms,output,", ");
415				print('</div>');
416			}
417			print('<div class="typedef">= ');
418			processType(t);
419			print('</div>');
420		}
421	}
422
423	function processPath( path : Path, ?params : List<CType> ) {
424		print(makePathUrl(path,"type"));
425		if( params != null && !params.isEmpty() ) {
426			print("&lt;");
427			for( t in params )
428				processType(t);
429			print("&gt;");
430		}
431	}
432
433	function processType( t : CType ) {
434		switch( t ) {
435		case CUnknown:
436			print("Unknown");
437		case CEnum(path,params):
438			processPath(path,params);
439		case CClass(path,params):
440			processPath(path,params);
441		case CTypedef(path,params):
442			processPath(path,params);
443		case CFunction(args,ret):
444			if( args.isEmpty() ) {
445				processPath("Void");
446				print(" -> ");
447			}
448			for( a in args ) {
449				if( a.opt )
450					print("?");
451				if( a.name != null && a.name != "" )
452					print(a.name+" : ");
453				processTypeFun(a.t,true);
454				print(" -> ");
455			}
456			processTypeFun(ret,false);
457		case CAnonymous(fields):
458			print("{ ");
459			var me = this;
460			display(fields,function(f) {
461				me.print(f.name+" : ");
462				me.processType(f.t);
463			},", ");
464			print("}");
465		case CDynamic(t):
466			if( t == null )
467				processPath("Dynamic");
468			else {
469				var l = new List();
470				l.add(t);
471				processPath("Dynamic",l);
472			}
473		}
474	}
475
476	function processTypeFun( t : CType, isArg ) {
477		var parent =  switch( t ) { case CFunction(_,_): true; case CEnum(n,_): isArg && n == "Void"; default : false; };
478		if( parent )
479			print("(");
480		processType(t);
481		if( parent )
482			print(")");
483	}
484
485	function rightsStr(f:ClassField,get,r) {
486		return switch(r) {
487		case RNormal: "default";
488		case RNo: "null";
489		case RCall(m): if( m == ((get?"get_":"set_")+f.name) ) "dynamic" else m;
490		case RMethod, RDynamic, RInline: throw "assert";
491		}
492	}
493
494	function keyword(w) {
495		print('<span class="kwd">'+w+' </span>');
496	}
497
498	function processDoc(doc : String) {
499		if( doc == null )
500			return;
501
502		// unixify line endings
503		doc = doc.split("\r\n").join("\n").split("\r").join("\n");
504
505		// trim stars
506		doc = ~/^([ \t]*)\*+/gm.replace(doc, "$1");
507		doc = ~/\**[ \t]*$/gm.replace(doc, "");
508
509		// process [] blocks
510		var rx = ~/\[/;
511		var tmp = new StringBuf();
512		var codes = new List();
513		while (rx.match(doc)) {
514			tmp.add( rx.matchedLeft() );
515
516			var code = rx.matchedRight();
517			var brackets = 1;
518			var i = 0;
519			while( i < code.length && brackets > 0 ) {
520				switch( code.charCodeAt(i++) ) {
521				case 91: brackets++;
522				case 93: brackets--;
523				}
524			}
525			doc = code.substr(i);
526			code = code.substr(0, i-1);
527			code = ~/&/g.replace(code, "&amp;");
528			code = ~/</g.replace(code, "&lt;");
529			code = ~/>/g.replace(code, "&gt;");
530			var tag = "##__code__"+codes.length+"##";
531			if( code.indexOf('\n') != -1 ) {
532				tmp.add("<pre>");
533				tmp.add(tag);
534				tmp.add("</pre>");
535				codes.add(code.split("\t").join("    "));
536			} else {
537				tmp.add("<code>");
538				tmp.add(tag);
539				tmp.add("</code>");
540				codes.add(code);
541			}
542		}
543		tmp.add(doc);
544
545		// separate into paragraphs
546		var parts = ~/\n[ \t]*\n/g.split(tmp.toString());
547		if( parts.length == 1 )
548			doc = parts[0];
549		else
550			doc = Lambda.map(parts,function(x) { return "<p>"+StringTools.trim(x)+"</p>"; }).join("\n");
551
552		// put back code parts
553		var i = 0;
554		for( c in codes )
555			doc = doc.split("##__code__"+(i++)+"##").join(c);
556		print(doc);
557	}
558
559	function display<T>( l : List<T>, f : T -> Void, sep : String ) {
560		var first = true;
561		for( x in l ) {
562			if( first )
563				first = false;
564			else
565				print(sep);
566			f(x);
567		}
568	}
569
570}