8186f9ddd02d782e2eee046dcebe3e7c03dab588
[jquery.git] / build / docs / docs.js
1 load("build/js/json.js", "build/js/xml.js", "build/js/writeFile.js");
2
3 var types = {
4         jQuery: "A jQuery object.",
5         Object: "A simple Javascript object. For example, it could be a String or a Number.",
6         String: "A string of characters.",
7         Number: "A numeric valid.",
8         Element: "The Javascript object representation of a DOM Element.",
9         Hash: "A Javascript object that contains key/value pairs in the form of properties and values.",
10         "Array<Element>": "An Array of DOM Elements.",
11         "Array<String>": "An Array of strings.",
12         Function: "A reference to a Javascript function."
13 };
14
15 var f = readFile(arguments[0]);
16
17 var c = [], bm, m;
18 var blockMatch = /\/\*\*\s*((.|\n)*?)\s*\*\//g;
19 var paramMatch = /\@(\S+) *((.|\n)*?)(?=\n\@|!!!)/m;
20
21 while ( bm = blockMatch.exec(f) ) {
22         block = bm[1].replace(/^\s*\* ?/mg,"") + "!!!";
23         var ret = { params: [], examples: [] };
24
25         while ( m = paramMatch.exec( block ) ) {
26                 block = block.replace( paramMatch, "" );
27
28                 var n = m[1];
29                 var v = m[2]
30                         .replace(/\s*$/g,"")
31                         .replace(/^\s*/g,"")
32                         .replace(/&/g, "&")
33                         .replace(/</g, "&lt;")
34                         .replace(/>/g, "&gt;")
35                         .replace(/\n/g, "<br/>")
36                         /*.replace(/(\s\s+)/g, function(a){
37                                 var ret = "";
38                                 for ( var i = 0; i < a.length; i++ )
39                                         ret += "&nbsp;";
40                                 return ret;
41                         })*/ || 1;
42
43                 if ( n == 'param' || n == 'any' ) {
44                         var args = v.split(/\s+/);
45                         v = args.slice( 2, args.length );
46                         v = { type: args[0], name: args[1], desc: v.join(' ') };
47                         if ( n == 'any' ) v.any = 1;
48                         n = "params";
49                 } else if ( n == 'example' ) {
50                         v = { code: v };
51                         n = "examples";
52                 }
53
54                 if ( n == 'desc' || n == 'before' || n == 'after' || n == 'result' ) {
55                         ret.examples[ ret.examples.length - 1 ][ n ] = v;
56                 } else {
57                         if ( ret[ n ] ) {
58                                 if ( ret[ n ].constructor == Array ) {
59                                         ret[ n ].push( v );
60                                 } else {
61                                         ret[ n ] = [ ret[ n ], v ];
62                                 }
63                         } else {
64                                 ret[ n ] = v;
65                         }
66                 }
67         }
68   
69         ret.desc = block.replace(/\s*!!!$/,"")
70                         .replace(/</g, "&lt;")
71                         .replace(/>/g, "&gt;");
72                         //.replace(/\n\n/g, "<br/><br/>")
73                         //.replace(/\n/g, " ");
74
75         var m = /^((.|\n)*?(\.|$))/.exec( ret.desc );
76         if ( m ) ret['short'] = m[1];
77
78         if ( ret.name ) c.push( ret );
79 }
80
81 var json = Object.toJSON( c );
82
83 writeFile( arguments[1] + "/data/jquery-docs-json.js", json );
84 writeFile( arguments[1] + "/data/jquery-docs-jsonp.js", "docsLoaded(" + json + ")" );
85
86 Object.toXML.force = { desc: 1, code: 1, before: 1, result: 1 };
87
88 var xml = Object.toXML( { method: c }, "docs" );
89
90 writeFile( arguments[1] + "/data/jquery-docs-xml.xml", 
91         "<?xml version='1.0' encoding='ISO-8859-1'?>\n" + xml );
92
93 writeFile( arguments[1] + "/index.xml",
94         "<?xml version='1.0' encoding='ISO-8859-1'?>\n" +
95         "<?xml-stylesheet type='text/xsl' href='style/docs.xsl'?>\n" + xml
96 );