Main.hx 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 Main {
  25. static var parser = new haxe.rtti.XmlParser();
  26. static function loadFile(file,platform,?remap) {
  27. var data = sys.io.File.getContent(neko.Web.getCwd()+file);
  28. var x = Xml.parse(data).firstElement();
  29. if( remap != null )
  30. transformPackage(x,remap,platform);
  31. parser.process(x,platform);
  32. }
  33. static function transformPackage( x : Xml, p1, p2 ) {
  34. switch( x.nodeType ) {
  35. case Xml.Element:
  36. var p = x.get("path");
  37. if( p != null && p.substr(0,6) == p1 + "." )
  38. x.set("path",p2 + "." + p.substr(6));
  39. for( x in x.elements() )
  40. transformPackage(x,p1,p2);
  41. default:
  42. }
  43. }
  44. static function save(html : HtmlPrinter,x,file) {
  45. var f = sys.io.File.write(file,true);
  46. html.output = f.writeString;
  47. html.process(x);
  48. f.close();
  49. neko.Lib.print(".");
  50. }
  51. static function generateEntry(html : HtmlPrinter,e,path) {
  52. switch( e ) {
  53. case TPackage(name,full,entries):
  54. if( html.filtered(full,true) )
  55. return;
  56. var old = html.baseUrl;
  57. html.baseUrl = "../"+html.baseUrl;
  58. path += name + "/";
  59. try sys.FileSystem.createDirectory(path) catch( e : Dynamic ) { }
  60. for( e in entries )
  61. generateEntry(html,e,path);
  62. html.baseUrl = old;
  63. default:
  64. var inf = TypeApi.typeInfos(e);
  65. if( html.filtered(inf.path,false) )
  66. return;
  67. var pack = inf.path.split(".");
  68. var name = pack.pop();
  69. save(html,e,path+name+".html");
  70. }
  71. }
  72. static function generateAll(filters : List<String>) {
  73. var html = new HtmlPrinter("content/",".html","../index");
  74. for( f in filters )
  75. html.addFilter(f);
  76. save(html,TPackage("root","root",parser.root),"index.html");
  77. html.baseUrl = "";
  78. try sys.FileSystem.createDirectory("content") catch( e : Dynamic ) { }
  79. for( e in parser.root )
  80. generateEntry(html,e,"content/");
  81. }
  82. public static function main() {
  83. if( neko.Web.isModNeko ) {
  84. var h = neko.Web.getParams();
  85. var dataFile = neko.Web.getCwd()+".data";
  86. var data : TypeRoot = try neko.Lib.unserialize(sys.io.File.getBytes(dataFile)) catch( e : Dynamic ) null;
  87. if( h.get("reload") != null || data == null ) {
  88. var baseDir = "../data/media/";
  89. loadFile(baseDir+"flash.xml","flash");
  90. loadFile(baseDir+"flash9.xml","flash9","flash");
  91. loadFile(baseDir+"neko.xml","neko");
  92. loadFile(baseDir+"js.xml","js");
  93. loadFile(baseDir+"php.xml","php");
  94. parser.sort();
  95. data = parser.root;
  96. var bytes = neko.Lib.serialize(data);
  97. var f = sys.io.File.write(dataFile,true);
  98. f.write(bytes);
  99. f.close();
  100. }
  101. var html = new HtmlPrinter("/api/","","");
  102. var clname = h.get("class");
  103. if( clname == "index" )
  104. clname = null;
  105. if( clname == null )
  106. html.process(TPackage("root","root",data));
  107. else {
  108. var clpath = clname.toLowerCase().split("/").join(".").split(".");
  109. var f = html.find(data,clpath,0);
  110. if( f == null )
  111. throw "Class not found : "+clpath.join(".");
  112. html.process(f);
  113. }
  114. } else {
  115. var filter = false;
  116. var filters = new List();
  117. var pf = null;
  118. for( x in Sys.args() ) {
  119. if( x == "-f" )
  120. filter = true;
  121. else if( x == "-v" )
  122. parser.newField = function(c,f) {
  123. if( f.isPublic && !f.isOverride && !c.isPrivate )
  124. Sys.println("[API INCOMPATIBILITY] "+c.path+"."+f.name+" ["+pf+"]");
  125. };
  126. else if( filter ) {
  127. filters.add(x);
  128. filter = false;
  129. } else {
  130. var f = x.split(";");
  131. pf = f[1];
  132. loadFile(f[0],f[1],f[2]);
  133. }
  134. }
  135. parser.sort();
  136. if( parser.root.length == 0 ) {
  137. Sys.println("Haxe Doc Generator 2.0 - (c)2006-2012 Haxe Foundation");
  138. Sys.println(" Usage : haxedoc [xml files] [-f filter]");
  139. Sys.exit(1);
  140. }
  141. generateAll(filters);
  142. }
  143. }
  144. }