/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

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