Added test for #237
[jquery.git] / build / js / parse.js
1 function parse( f ) {
2         var c = [], bm, m;
3         var blockMatch = /\/\*\*\s*((.|\n)*?)\s*\*\//g;
4         var paramMatch = /\@(\S+) *((.|\n)*?)(?=\n\@|!!!)/m;
5         
6         while ( bm = blockMatch.exec(f) ) {
7                 block = bm[1].replace(/^\s*\* ?/mg,"") + "!!!";
8                 var ret = { params: [], examples: [], tests: [] };
9         
10                 while ( m = paramMatch.exec( block ) ) {
11                         block = block.replace( paramMatch, "" );
12
13                         var n = m[1];
14                         var v = m[2]
15                                 .replace(/\s*$/g,"")
16                                 .replace(/^\s*/g,"")
17                                 .replace(/&/g, "&")
18                                 .replace(/</g, "&lt;")
19                                 .replace(/>/g, "&gt;")
20                                 //.replace(/\n/g, "<br/>")
21                                 /*.replace(/(\s\s+)/g, function(a){
22                                         var ret = "";
23                                         for ( var i = 0; i < a.length; i++ )
24                                                 ret += "&nbsp;";
25                                         return ret;
26                                 })*/ || 1;
27         
28                         if ( n == 'param' || n == 'any' ) {
29                                 var args = v.split(/\s+/);
30                                 v = args.slice( 2, args.length );
31                                 v = { type: args[0], name: args[1], desc: v.join(' ') };
32                                 if ( n == 'any' ) v.any = 1;
33                                 n = "params";
34                         } else if ( n == 'example' ) {
35                                 v = { code: v };
36                                 n = "examples";
37                         } else if ( n == 'test' ) {
38                                 n = "tests";
39                         }
40         
41                         if ( n == 'desc' || n == 'before' || n == 'after' || n == 'result' ) {
42                                 ret.examples[ ret.examples.length - 1 ][ n ] = v;
43                         } else {
44                                 if ( ret[ n ] ) {
45                                         if ( ret[ n ].constructor == Array ) {
46                                                 ret[ n ].push( v );
47                                         } else {
48                                                 ret[ n ] = [ ret[ n ], v ];
49                                         }
50                                 } else {
51                                         ret[ n ] = v;
52                                 }
53                         }
54                 }
55         
56                 ret.desc = block.replace(/\s*!!!$/,"")
57                                 .replace(/</g, "&lt;")
58                                 .replace(/>/g, "&gt;");
59                                 //.replace(/\n\n/g, "<br/><br/>")
60                                 //.replace(/\n/g, " ");
61         
62                 var m = /^((.|\n)*?(\.|$))/.exec( ret.desc );
63                 if ( m ) ret['short'] = m[1];
64         
65                 if ( ret.name ) c.push( ret );
66         }
67
68         return c;
69 }
70
71 function categorize( json ) {
72         var obj = { cat: [], method: [] };
73
74         for ( var i = 0; i < json.length; i++ ) {
75                 if ( !json[i].cat ) json[i].cat = "";
76
77                 var cat = json[i].cat.split("/");
78
79                 var pos = obj;
80                 for ( var j = 0; j < cat.length; j++ ) {
81                         var c = cat[j];
82                         var curCat = null;
83
84                         // Locate current category
85                         for ( var n = 0; n < pos.cat.length; n++ )
86                                 if ( pos.cat[n].value == c )
87                                         curCat = pos.cat[n];
88
89                         // Create current category
90                         if ( !curCat ) {
91                                 curCat = { value: c, cat: [], method: [] };
92                                 pos.cat.push( curCat )
93                         }
94
95                         // If we're at  the end, add the method
96                         if ( j == cat.length - 1 )
97                                 curCat.method.push( json[i] );
98
99                         // Otherwise, traverse deeper
100                         else
101                                 pos = curCat;
102                 }
103         }
104
105         return obj;
106 }