import ide.doc;
namespace ide.doc;

class markdown{
	ctor( fullpath ){ {
	
			var kernelLibName;
			var kernel = {table=1;string=1;raw=1;io=1;com=1;math=1;time=1;thread=1 };
			if( kernel[fullpath] ){
				kernelLibName = fullpath;
				fullpath = "kernel";
			}
			else{
				if( fullpath == "win.form" ) fullpath = "win.ui";
				
				if( !..io.exist(fullpath) && ..string.match(fullpath,"^[a-zA-Z][\w\.]+$") ){
					var p = fullpath;
					fullpath = ..io.libpath(p);
					if(!fullpath){
						fullpath = ..io.libpath("win.ui.ctrl." + p);
					}
				}
				if( !..io.exist(fullpath) ) return;
			}
			
           	this = ..ide.doc( fullpath ); 
           	if( kernelLibName ){
           		var sections = { 
           			[kernelLibName] = this.sections[kernelLibName]; 
           			[kernelLibName+"Object"] = this.sections[kernelLibName+"Object"]; 
           		}; 
           		
           		this.sections = sections;
           		this.const = null;
           		this.define = null;
           		this.libPath = kernelLibName;
           		this.fileName = kernelLibName;
           		this.filePath = kernelLibName;
           	}
           	
           	
           	var tOutput = {};
			..table.push(tOutput,this.libPath,"帮助文档",'\r\n'
				,"===========================================");

			var sortNames;
			if( this.libPath ) {
				sortNames = { this.libPath;this.libPath + "Object"};
				var h = ..string.match(this.libPath,"^[^.]+");
				if( h != this.libPath ) ..table.unshift(sortNames,h);
				if( this.libPath == "win.ui" ) sortNames = { "win.ui";"win.form";"winform"};
				elseif( this.libPath == "web.form" ) sortNames = { "web";"web.form";"wb"};
				elseif( this.libPath == "web.layout" ) sortNames = { "web.layout";"wbLayout"}; 
				elseif(  ..string.startWith(this.libPath,"win.ui.ctrl.",true) ){
					sortNames = { "win.ui.ctrl";..string.match(this.libPath,"[^.]+$") + "Object"}; 
				}
			};
			
			for(path,section in ..table.eachName(this.sections,,sortNames)){
					if(!..table.next(section) ) continue;
					
					..table.push(tOutput, '\r\n<a id="',(#path? path : "global"),'"></a>\r\n',#path? path : "全局对象",' 成员列表\r\n'
						,"-------------------------------------------------------------------------------------------------"
						,'\r\n');
					 
					if( section[path] ){ 
						var v = section[path];
						if( ..string.startWith(v,path) ){
							v = ..string.right(v,-(#path+1));
						}
						..table.push(tOutput,v,'\r\n');
						section[path] = null;
					}
					 
					for(k,v in ..table.eachName(section)){  
						..table.push(tOutput,'\r\n<h6 id="', (..string.match(k,"(.+\.[^.]+)\(")  or k ) ,'">',k,' </h6>\r\n ');
						..table.push(tOutput, v,'\r\n' ); 
			  		}  
			}  
 
			if( this.const ){
				..table.push(tOutput,  '\r\n\r\n',"全局常量函数",'\r\n'
					,"-------------------------------------------------------------------------------------------------"
					,'\r\n');
				for(k,v in ..table.eachName(this.const)){ 
					..table.push(tOutput,k,"=",v,'  \r\n');
				}
			}
			
			if( this.define ){
				..table.push(tOutput,  '\r\n\r\n',"自动完成常量",'\r\n'
					,"-------------------------------------------------------------------------------------------------"
					,'\r\n');
				for(k,v in ..table.eachName(this.define)){ 
					..table.push(tOutput,k,"=",v,'  \r\n');
				}
			}
			
			this.outputString = ..string.join(tOutput);
	} };
	@_meta;
}

namespace markdown{
	_meta = {
		_tostring = function(){
			return owner.outputString;
		} 
	}
}

/*intellisense(ide.doc)
markdown(.("库路径") = 返回对象可使用tostring函数转换为Markdown格式文档\n参数可以是库完整路径或名字空间
markdown() = !libDocument.
end intellisense*/