PageRenderTime 19ms CodeModel.GetById 9ms app.highlight 7ms RepoModel.GetById 1ms app.codeStats 0ms

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