@@ -34,7 +34,10 @@ function performSymbolSearch(maxlen)
3434{
3535 if ( maxlen === 'undefined' ) maxlen = 26 ;
3636
37- var searchstring = $ ( "#symbolSearch" ) . val ( ) . toLowerCase ( ) ;
37+ var el = $ ( "#symbolSearch" ) ;
38+ if ( el . length === 0 ) el = $ ( "#q" ) ;
39+
40+ var searchstring = el . val ( ) . toLowerCase ( ) ;
3841
3942 if ( searchstring == lastSearchString ) return ;
4043 lastSearchString = searchstring ;
@@ -118,7 +121,20 @@ function performSymbolSearch(maxlen)
118121 if ( np > 0 ) shortname = ".." + shortname ;
119122 else shortname = shortname . substr ( 1 ) ;
120123
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+ }
122138 $ ( '#symbolSearchResults' ) . append ( el ) ;
123139 }
124140
@@ -129,6 +145,40 @@ function performSymbolSearch(maxlen)
129145 $ ( '#symbolSearchResults' ) . show ( ) ;
130146}
131147
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+
132182$ ( function ( ) {
133183 $ ( "#search-box form" ) . on ( "submit" , function ( e ) {
134184 var searchResults = $ ( '#symbolSearchResults' ) . children ( ) ;
0 commit comments