Main.hx 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. package tools.haxedoc;
  26. import haxe.rtti.CType;
  27. class Main {
  28. static var parser = new haxe.rtti.XmlParser();
  29. static function loadFile(file,platform,?remap) {
  30. var data = neko.io.File.getContent(neko.Web.getCwd()+file);
  31. var x = Xml.parse(data).firstElement();
  32. if( remap != null )
  33. transformPackage(x,remap,platform);
  34. parser.process(x,platform);
  35. }
  36. static function transformPackage( x : Xml, p1, p2 ) {
  37. switch( x.nodeType ) {
  38. case Xml.Element:
  39. var p = x.get("path");
  40. if( p != null && p.substr(0,6) == p1 + "." )
  41. x.set("path",p2 + "." + p.substr(6));
  42. for( x in x.elements() )
  43. transformPackage(x,p1,p2);
  44. default:
  45. }
  46. }
  47. static function save(html : HtmlPrinter,x,file) {
  48. var f = neko.io.File.write(file,true);
  49. html.output = f.writeString;
  50. html.process(x);
  51. f.close();
  52. neko.Lib.print(".");
  53. }
  54. static function generateEntry(html : HtmlPrinter,e,path) {
  55. switch( e ) {
  56. case TPackage(name,full,entries):
  57. if( html.filtered(full,true) )
  58. return;
  59. var old = html.baseUrl;
  60. html.baseUrl = "../"+html.baseUrl;
  61. path += name + "/";
  62. try neko.FileSystem.createDirectory(path) catch( e : Dynamic ) { }
  63. for( e in entries )
  64. generateEntry(html,e,path);
  65. html.baseUrl = old;
  66. default:
  67. var inf = TypeApi.typeInfos(e);
  68. if( html.filtered(inf.path,false) )
  69. return;
  70. var pack = inf.path.split(".");
  71. var name = pack.pop();
  72. save(html,e,path+name+".html");
  73. }
  74. }
  75. static function generateAll(filters : List<String>) {
  76. var html = new HtmlPrinter("content/",".html","../index");
  77. for( f in filters )
  78. html.addFilter(f);
  79. save(html,TPackage("root","root",parser.root),"index.html");
  80. html.baseUrl = "";
  81. try neko.FileSystem.createDirectory("content") catch( e : Dynamic ) { }
  82. for( e in parser.root )
  83. generateEntry(html,e,"content/");
  84. }
  85. static function findClass( t : TypeRoot, path : Array<String>, pos : Int ) {
  86. var name = path[pos];
  87. var pack = (pos != path.length - 1);
  88. var def = null;
  89. for( c in t )
  90. switch( c ) {
  91. case TPackage(pname,_,subs):
  92. if( name == pname ) {
  93. if( pack )
  94. return findClass(subs,path,pos+1);
  95. def = c;
  96. }
  97. default:
  98. if( pack ) continue;
  99. var inf = TypeApi.typeInfos(c);
  100. if( inf.path.toLowerCase() == path.join(".") )
  101. return c;
  102. }
  103. return def;
  104. }
  105. public static function main() {
  106. if( neko.Web.isModNeko ) {
  107. var h = neko.Web.getParams();
  108. var dataFile = neko.Web.getCwd()+".data";
  109. var data : TypeRoot = try neko.Lib.unserialize(neko.io.File.getContent(dataFile)) catch( e : Dynamic ) null;
  110. if( h.get("reload") != null || data == null ) {
  111. var baseDir = "../data/media/";
  112. loadFile(baseDir+"flash.xml","flash");
  113. loadFile(baseDir+"flash9.xml","flash9","flash");
  114. loadFile(baseDir+"neko.xml","neko");
  115. loadFile(baseDir+"js.xml","js");
  116. loadFile(baseDir+"php.xml","php");
  117. parser.sort();
  118. data = parser.root;
  119. var str = neko.Lib.serialize(data);
  120. var f = neko.io.File.write(dataFile,true);
  121. f.writeString(str);
  122. f.close();
  123. }
  124. var html = new HtmlPrinter("/api/","","");
  125. var clname = h.get("class");
  126. if( clname == "index" )
  127. clname = null;
  128. if( clname == null )
  129. html.process(TPackage("root","root",data));
  130. else {
  131. var clpath = clname.toLowerCase().split("/").join(".").split(".");
  132. var f = findClass(data,clpath,0);
  133. if( f == null )
  134. throw "Class not found : "+clpath.join(".");
  135. html.process(f);
  136. }
  137. } else {
  138. var filter = false;
  139. var filters = new List();
  140. for( x in neko.Sys.args() ) {
  141. if( x == "-f" )
  142. filter = true;
  143. else if( filter ) {
  144. filters.add(x);
  145. filter = false;
  146. } else {
  147. var f = x.split(";");
  148. loadFile(f[0],f[1],f[2]);
  149. }
  150. }
  151. parser.sort();
  152. if( parser.root.length == 0 ) {
  153. neko.Lib.println("Haxe Doc Generator 2.0 - (c)2006 Motion-Twin");
  154. neko.Lib.println(" Usage : haxedoc [xml files] [-f filter]");
  155. neko.Sys.exit(1);
  156. }
  157. generateAll(filters);
  158. }
  159. }
  160. }