PageRenderTime 26ms CodeModel.GetById 8ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/tags/jsdoc_toolkit-2.2.1/jsdoc-toolkit/templates/jsdoc/publish.js

http://jsdoc-toolkit.googlecode.com/
JavaScript | 184 lines | 129 code | 34 blank | 21 comment | 25 complexity | 36f5145a1bfa554e9b68f09f6758f686 MD5 | raw file
  1/** Called automatically by JsDoc Toolkit. */
  2function publish(symbolSet) {
  3	publish.conf = {  // trailing slash expected for dirs
  4		ext:         ".html",
  5		outDir:      JSDOC.opt.d || SYS.pwd+"../out/jsdoc/",
  6		templatesDir: JSDOC.opt.t || SYS.pwd+"../templates/jsdoc/",
  7		symbolsDir:  "symbols/",
  8		srcDir:      "symbols/src/"
  9	};
 10	
 11	// is source output is suppressed, just display the links to the source file
 12	if (JSDOC.opt.s && defined(Link) && Link.prototype._makeSrcLink) {
 13		Link.prototype._makeSrcLink = function(srcFilePath) {
 14			return "<"+srcFilePath+">";
 15		}
 16	}
 17	
 18	// create the folders and subfolders to hold the output
 19	IO.mkPath((publish.conf.outDir+"symbols/src").split("/"));
 20		
 21	// used to allow Link to check the details of things being linked to
 22	Link.symbolSet = symbolSet;
 23
 24	// create the required templates
 25	try {
 26		var classTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"class.tmpl");
 27		var classesTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"allclasses.tmpl");
 28	}
 29	catch(e) {
 30		print("Couldn't create the required templates: "+e);
 31		quit();
 32	}
 33	
 34	// some ustility filters
 35	function hasNoParent($) {return ($.memberOf == "")}
 36	function isaFile($) {return ($.is("FILE"))}
 37	function isaClass($) {return ($.is("CONSTRUCTOR") || $.isNamespace)}
 38	
 39	// get an array version of the symbolset, useful for filtering
 40	var symbols = symbolSet.toArray();
 41	
 42	// create the hilited source code files
 43	var files = JSDOC.opt.srcFiles;
 44 	for (var i = 0, l = files.length; i < l; i++) {
 45 		var file = files[i];
 46 		var srcDir = publish.conf.outDir + "symbols/src/";
 47		makeSrcFile(file, srcDir);
 48 	}
 49 	
 50 	// get a list of all the classes in the symbolset
 51 	var classes = symbols.filter(isaClass).sort(makeSortby("alias"));
 52	
 53	// create a class index, displayed in the left-hand column of every class page
 54	Link.base = "../";
 55 	publish.classesIndex = classesTemplate.process(classes); // kept in memory
 56	
 57	// create each of the class pages
 58	for (var i = 0, l = classes.length; i < l; i++) {
 59		var symbol = classes[i];
 60		
 61		symbol.events = symbol.getEvents();   // 1 order matters
 62		symbol.methods = symbol.getMethods(); // 2
 63		
 64		var output = "";
 65		output = classTemplate.process(symbol);
 66		
 67		IO.saveFile(publish.conf.outDir+"symbols/", symbol.alias+publish.conf.ext, output);
 68	}
 69	
 70	// regenerate the index with different relative links, used in the index pages
 71	Link.base = "";
 72	publish.classesIndex = classesTemplate.process(classes);
 73	
 74	// create the class index page
 75	try {
 76		var classesindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"index.tmpl");
 77	}
 78	catch(e) { print(e.message); quit(); }
 79	
 80	var classesIndex = classesindexTemplate.process(classes);
 81	IO.saveFile(publish.conf.outDir, "index"+publish.conf.ext, classesIndex);
 82	classesindexTemplate = classesIndex = classes = null;
 83	
 84	// create the file index page
 85	try {
 86		var fileindexTemplate = new JSDOC.JsPlate(publish.conf.templatesDir+"allfiles.tmpl");
 87	}
 88	catch(e) { print(e.message); quit(); }
 89	
 90	var documentedFiles = symbols.filter(isaFile); // files that have file-level docs
 91	var allFiles = []; // not all files have file-level docs, but we need to list every one
 92	
 93	for (var i = 0; i < files.length; i++) {
 94		allFiles.push(new JSDOC.Symbol(files[i], [], "FILE", new JSDOC.DocComment("/** */")));
 95	}
 96	
 97	for (var i = 0; i < documentedFiles.length; i++) {
 98		var offset = files.indexOf(documentedFiles[i].alias);
 99		allFiles[offset] = documentedFiles[i];
100	}
101		
102	allFiles = allFiles.sort(makeSortby("name"));
103
104	// output the file index page
105	var filesIndex = fileindexTemplate.process(allFiles);
106	IO.saveFile(publish.conf.outDir, "files"+publish.conf.ext, filesIndex);
107	fileindexTemplate = filesIndex = files = null;
108}
109
110
111/** Just the first sentence (up to a full stop). Should not break on dotted variable names. */
112function summarize(desc) {
113	if (typeof desc != "undefined")
114		return desc.match(/([\w\W]+?\.)[^a-z0-9_$]/i)? RegExp.$1 : desc;
115}
116
117/** Make a symbol sorter by some attribute. */
118function makeSortby(attribute) {
119	return function(a, b) {
120		if (a[attribute] != undefined && b[attribute] != undefined) {
121			a = a[attribute].toLowerCase();
122			b = b[attribute].toLowerCase();
123			if (a < b) return -1;
124			if (a > b) return 1;
125			return 0;
126		}
127	}
128}
129
130/** Pull in the contents of an external file at the given path. */
131function include(path) {
132	var path = publish.conf.templatesDir+path;
133	return IO.readFile(path);
134}
135
136/** Turn a raw source file into a code-hilited page in the docs. */
137function makeSrcFile(path, srcDir, name) {
138	if (JSDOC.opt.s) return;
139	
140	if (!name) {
141		name = path.replace(/\.\.?[\\\/]/g, "").replace(/[\\\/]/g, "_");
142		name = name.replace(/\:/g, "_");
143	}
144	
145	var src = {path: path, name:name, charset: IO.encoding, hilited: ""};
146	
147	if (defined(JSDOC.PluginManager)) {
148		JSDOC.PluginManager.run("onPublishSrc", src);
149	}
150
151	if (src.hilited) {
152		IO.saveFile(srcDir, name+publish.conf.ext, src.hilited);
153	}
154}
155
156/** Build output for displaying function parameters. */
157function makeSignature(params) {
158	if (!params) return "()";
159	var signature = "("
160	+
161	params.filter(
162		function($) {
163			return $.name.indexOf(".") == -1; // don't show config params in signature
164		}
165	).map(
166		function($) {
167			return $.name;
168		}
169	).join(", ")
170	+
171	")";
172	return signature;
173}
174
175/** Find symbol {@link ...} strings in text and turn into html links */
176function resolveLinks(str, from) {
177	str = str.replace(/\{@link ([^} ]+) ?\}/gi,
178		function(match, symbolName) {
179			return new Link().toSymbol(symbolName);
180		}
181	);
182	
183	return str;
184}