HtmlPrinter.hx 15 KB

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