@@ -34,7 +34,10 @@ function performSymbolSearch(maxlen)
34
34
{
35
35
if ( maxlen === 'undefined' ) maxlen = 26 ;
36
36
37
- var searchstring = $ ( "#symbolSearch" ) . val ( ) . toLowerCase ( ) ;
37
+ var el = $ ( "#symbolSearch" ) ;
38
+ if ( el . length === 0 ) el = $ ( "#q" ) ;
39
+
40
+ var searchstring = el . val ( ) . toLowerCase ( ) ;
38
41
39
42
if ( searchstring == lastSearchString ) return ;
40
43
lastSearchString = searchstring ;
@@ -118,7 +121,20 @@ function performSymbolSearch(maxlen)
118
121
if ( np > 0 ) shortname = ".." + shortname ;
119
122
else shortname = shortname . substr ( 1 ) ;
120
123
121
- el . append ( '<a href="' + symbolSearchRootDir + sym . path + '" title="' + name + '" tabindex="1001">' + shortname + '</a>' ) ;
124
+ if ( typeof ( symbolSearchRootDir ) === "undefined" ) {
125
+ // translate ddox path into ddoc path - this is a big messy
126
+ var module = ddoxSymbolToDdocModule ( sym ) ;
127
+ var path ;
128
+ if ( sym . kind === "module" ) {
129
+ path = module ;
130
+ } else {
131
+ var symbol = ddoxSymbolToDdocSymbol ( sym ) ;
132
+ path = module + "#" + symbol ; // combine module + symbol, e.g. core_atomic.html#testCAS
133
+ }
134
+ el . append ( '<a href="' + path + '" title="' + name + '" tabindex="1001">' + shortname + '</a>' ) ;
135
+ } else {
136
+ el . append ( '<a href="' + symbolSearchRootDir + sym . path + '" title="' + name + '" tabindex="1001">' + shortname + '</a>' ) ;
137
+ }
122
138
$ ( '#symbolSearchResults' ) . append ( el ) ;
123
139
}
124
140
@@ -129,12 +145,46 @@ function performSymbolSearch(maxlen)
129
145
$ ( '#symbolSearchResults' ) . show ( ) ;
130
146
}
131
147
148
+ function ddoxSymbolToDdocModule ( sym )
149
+ {
150
+ // sym.path: ./core/atomic/test_cas.html
151
+ // ddoc has an individual page for each top-level symbol, so we need to go one level higher to get the actual module name
152
+ if ( sym . kind === "module" )
153
+ return sym . name . split ( "." ) . join ( "_" ) + ".html" ;
154
+
155
+ var path = sym . path . slice ( 0 , - 5 ) ; // strip the html extension from the path, e.g. ./core/atomic/test_cas
156
+ path = path . replace ( / ( .* ) \/ ( .* ) $ / g, "$1.html" ) ; // the module is one level higher, e.g. ./core/atomic.html
157
+ path = path . replace ( / \/ / g, "_" ) ; // ddoc uses _ to divide modules, e.g. ._core_atomic.html
158
+ path = path . replace ( "._" , "" ) ; // remove the beginning excess, e.g. core_atomic.html
159
+ return path ;
160
+ }
161
+
162
+ function ddoxSymbolToDdocSymbol ( sym )
163
+ {
164
+ var pathBaseName = sym . path . replace ( / .* ?( [ ^ \/ ] * ) [ . ] h t m l ( .* ) / g, "$1$2" ) ; // the basename on the HTML, e.g. socket_option_level.html#IP
165
+ if ( sym . path . indexOf ( "#" ) >= 0 || pathBaseName . indexOf ( "." ) >= 0 ) {
166
+ // sym.name: std.socket.SocketOptionLevel.IP, sym.path: "./std/socket/socket_option_level.html#IP -> SocketOptionLevel.IP
167
+ // this is the difficult case where the symbol is nested
168
+ // strategy: take the last two portions of the package name and hope for the best
169
+ var parts = sym . name . split ( "." ) . slice ( - 2 ) ;
170
+ if ( parts [ 0 ] === parts [ 1 ] ) {
171
+ // an eponymous template
172
+ return parts [ 0 ] ;
173
+ } else {
174
+ return "." + parts . join ( "." ) ;
175
+ }
176
+ } else {
177
+ // sym.name: core.atomic.testCAS
178
+ return sym . name . replace ( / ( .* ) [ . ] ( .* ) $ / g, "$2" ) // testCAS
179
+ }
180
+ }
181
+
132
182
$ ( function ( ) {
133
- $ ( "#search-box form" ) . on ( "submit" , function ( e ) {
134
- var searchResults = $ ( '#symbolSearchResults' ) . children ( ) ;
135
- if ( searchResults . length > 0 ) {
136
- window . location = searchResults . first ( ) . find ( "a" ) . attr ( "href" ) ;
137
- e . preventDefault ( ) ;
138
- }
139
- } ) ;
183
+ $ ( "#search-box form" ) . on ( "submit" , function ( e ) {
184
+ var searchResults = $ ( '#symbolSearchResults' ) . children ( ) ;
185
+ if ( searchResults . length > 0 ) {
186
+ window . location = searchResults . first ( ) . find ( "a" ) . attr ( "href" ) ;
187
+ e . preventDefault ( ) ;
188
+ }
189
+ } ) ;
140
190
} ) ;
0 commit comments