/bin/std/tools/haxedoc/Main.hx

http://github.com/Yoomee/clippy · Haxe · 149 lines · 116 code · 9 blank · 24 comment · 33 complexity · 7584851ffe01a512a1d93a773cf68d57 MD5 · raw file

  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. public static function main() {
  86. if( neko.Web.isModNeko ) {
  87. var h = neko.Web.getParams();
  88. var dataFile = neko.Web.getCwd()+".data";
  89. var data : TypeRoot = try neko.Lib.unserialize(neko.io.File.getContent(dataFile)) catch( e : Dynamic ) null;
  90. if( h.get("reload") != null || data == null ) {
  91. var baseDir = "../data/media/";
  92. loadFile(baseDir+"flash.xml","flash");
  93. loadFile(baseDir+"flash9.xml","flash9","flash");
  94. loadFile(baseDir+"neko.xml","neko");
  95. loadFile(baseDir+"js.xml","js");
  96. loadFile(baseDir+"php.xml","php");
  97. parser.sort();
  98. data = parser.root;
  99. var str = neko.Lib.serialize(data);
  100. var f = neko.io.File.write(dataFile,true);
  101. f.writeString(str);
  102. f.close();
  103. }
  104. var html = new HtmlPrinter("/api/","","");
  105. var clname = h.get("class");
  106. if( clname == "index" )
  107. clname = null;
  108. if( clname == null )
  109. html.process(TPackage("root","root",data));
  110. else {
  111. var clpath = clname.toLowerCase().split("/").join(".").split(".");
  112. var f = html.find(data,clpath,0);
  113. if( f == null )
  114. throw "Class not found : "+clpath.join(".");
  115. html.process(f);
  116. }
  117. } else {
  118. var filter = false;
  119. var filters = new List();
  120. for( x in neko.Sys.args() ) {
  121. if( x == "-f" )
  122. filter = true;
  123. else if( filter ) {
  124. filters.add(x);
  125. filter = false;
  126. } else {
  127. var f = x.split(";");
  128. loadFile(f[0],f[1],f[2]);
  129. }
  130. }
  131. parser.sort();
  132. if( parser.root.length == 0 ) {
  133. neko.Lib.println("Haxe Doc Generator 2.0 - (c)2006 Motion-Twin");
  134. neko.Lib.println(" Usage : haxedoc [xml files] [-f filter]");
  135. neko.Sys.exit(1);
  136. }
  137. generateAll(filters);
  138. }
  139. }
  140. }