github.com/dougm/docker@v1.5.0/docs/theme/mkdocs/tipuesearch/tipuesearch.js (about)

     1  
     2  /*
     3  Tipue Search 3.1
     4  Copyright (c) 2013 Tipue
     5  Tipue Search is released under the MIT License
     6  http://www.tipue.com/search
     7  */ 
     8  
     9  
    10  (function($) {
    11  
    12       $.fn.tipuesearch = function(options) {
    13  
    14            var set = $.extend( {
    15            
    16                 'show'                   : 7,
    17                 'newWindow'              : false,
    18                 'showURL'                : true,
    19                 'minimumLength'          : 3,
    20                 'descriptiveWords'       : 25,
    21                 'highlightTerms'         : true,
    22                 'highlightEveryTerm'     : false,
    23                 'mode'                   : 'static',
    24                 'liveDescription'        : '*',
    25                 'liveContent'            : '*',
    26                 'contentLocation'        : 'tipuesearch/tipuesearch_content.json'
    27            
    28            }, options);
    29            
    30            return this.each(function() {
    31  
    32                 var tipuesearch_in = {
    33                      pages: []
    34                 };
    35                 $.ajaxSetup({
    36                      async: false
    37                 });
    38  
    39                 if (set.mode == 'live')
    40                 {
    41                      for (var i = 0; i < tipuesearch_pages.length; i++)
    42                      {
    43                           $.get(tipuesearch_pages[i], '',
    44                                function (html)
    45                                {
    46                                     var cont = $(set.liveContent, html).text();
    47                                     cont = cont.replace(/\s+/g, ' ');
    48                                     var desc = $(set.liveDescription, html).text();
    49                                     desc = desc.replace(/\s+/g, ' ');
    50                                                                        
    51                                     var t_1 = html.toLowerCase().indexOf('<title>');
    52                                     var t_2 = html.toLowerCase().indexOf('</title>', t_1 + 7);
    53                                     if (t_1 != -1 && t_2 != -1)
    54                                     {
    55                                          var tit = html.slice(t_1 + 7, t_2);
    56                                     }
    57                                     else
    58                                     {
    59                                          var tit = 'No title';
    60                                     }
    61  
    62                                     tipuesearch_in.pages.push({
    63                                          "title": tit,
    64                                          "text": desc,
    65                                          "tags": cont,
    66                                          "loc": tipuesearch_pages[i] 
    67                                     });    
    68                                }
    69                           );
    70                      }
    71                 }
    72                 
    73                 if (set.mode == 'json')
    74                 {
    75                      $.getJSON(set.contentLocation,
    76                           function(json)
    77                           {
    78                                tipuesearch_in = $.extend({}, json);
    79                           }
    80                      );
    81                 }
    82  
    83                 if (set.mode == 'static')
    84                 {
    85                      tipuesearch_in = $.extend({}, tipuesearch);
    86                 }                              
    87                 
    88                 var tipue_search_w = '';
    89                 if (set.newWindow)
    90                 {
    91                      tipue_search_w = ' target="_blank"';      
    92                 }
    93  
    94                 function getURLP(name)
    95                 {
    96                      return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20')) || null;
    97                 }
    98                 if (getURLP('q'))
    99                 {
   100                      $('#tipue_search_input').val(getURLP('q'));
   101                      getTipueSearch(0, true);
   102                 }               
   103                               
   104                 $('#tipue_search_button').click(function()
   105                 {
   106                      getTipueSearch(0, true);
   107                 });
   108                 $(this).keyup(function(event)
   109                 {
   110                      if(event.keyCode == '13')
   111                      {
   112                           getTipueSearch(0, true);
   113                      }
   114                 });
   115  
   116                 function getTipueSearch(start, replace)
   117                 {
   118                      $('#tipue_search_content').hide();
   119                      var out = '';
   120                      var results = '';
   121                      var show_replace = false;
   122                      var show_stop = false;
   123                      
   124                      var d = $('#tipue_search_input').val().toLowerCase();
   125                      d = $.trim(d);
   126                      var d_w = d.split(' ');
   127                      d = '';
   128                      for (var i = 0; i < d_w.length; i++)
   129                      {
   130                           var a_w = true;
   131                           for (var f = 0; f < tipuesearch_stop_words.length; f++)
   132                           {
   133                                if (d_w[i] == tipuesearch_stop_words[f])
   134                                {
   135                                     a_w = false;
   136                                     show_stop = true;          
   137                                }
   138                           }
   139                           if (a_w)
   140                           {
   141                                d = d + ' ' + d_w[i];
   142                           }
   143                      }
   144                      d = $.trim(d);
   145                      d_w = d.split(' ');
   146                      
   147                      if (d.length >= set.minimumLength)
   148                      {
   149                           if (replace)
   150                           {
   151                                var d_r = d;
   152                                for (var i = 0; i < d_w.length; i++)
   153                                {
   154                                     for (var f = 0; f < tipuesearch_replace.words.length; f++)
   155                                     {
   156                                          if (d_w[i] == tipuesearch_replace.words[f].word)
   157                                          {
   158                                               d = d.replace(d_w[i], tipuesearch_replace.words[f].replace_with);
   159                                               show_replace = true;
   160                                          }
   161                                     }
   162                                }
   163                                d_w = d.split(' ');
   164                           }                   
   165                           
   166                           var d_t = d;
   167                           for (var i = 0; i < d_w.length; i++)
   168                           {
   169                                for (var f = 0; f < tipuesearch_stem.words.length; f++)
   170                                {
   171                                     if (d_w[i] == tipuesearch_stem.words[f].word)
   172                                     {
   173                                          d_t = d_t + ' ' + tipuesearch_stem.words[f].stem;
   174                                     }
   175                                }
   176                           }
   177                           d_w = d_t.split(' ');
   178  
   179                           var c = 0;
   180                           found = new Array();
   181                           for (var i = 0; i < tipuesearch_in.pages.length; i++)
   182                           {
   183                                var score = 1000000000;
   184                                var s_t = tipuesearch_in.pages[i].text;
   185                                for (var f = 0; f < d_w.length; f++)
   186                                {
   187                                     var pat = new RegExp(d_w[f], 'i');
   188                                     if (tipuesearch_in.pages[i].title.search(pat) != -1)
   189                                     {
   190                                          score -= (200000 - i);
   191                                     }
   192                                     if (tipuesearch_in.pages[i].text.search(pat) != -1)
   193                                     {
   194                                          score -= (150000 - i);
   195                                     }
   196                                     
   197                                     if (set.highlightTerms)
   198                                     {
   199                                          if (set.highlightEveryTerm) 
   200                                          {
   201                                               var patr = new RegExp('(' + d_w[f] + ')', 'gi');
   202                                          }
   203                                          else
   204                                          {
   205                                               var patr = new RegExp('(' + d_w[f] + ')', 'i');
   206                                          }
   207                                          s_t = s_t.replace(patr, "<b>$1</b>");
   208                                     }
   209                                     if (tipuesearch_in.pages[i].tags.search(pat) != -1)
   210                                     {
   211                                          score -= (100000 - i);
   212                                     }
   213                      
   214                                }
   215                                if (score < 1000000000)
   216                                {
   217                                     found[c++] = score + '^' + tipuesearch_in.pages[i].title + '^' + s_t + '^' + tipuesearch_in.pages[i].loc;                                                                   
   218                                }
   219                           }                         
   220                           
   221                           if (c != 0)
   222                           {
   223                                if (show_replace == 1)
   224                                {
   225                                     out += '<div id="tipue_search_warning_head">Showing results for ' + d + '</div>';
   226                                     out += '<div id="tipue_search_warning">Search for <a href="javascript:void(0)" id="tipue_search_replaced">' + d_r + '</a></div>'; 
   227                                }
   228                                if (c == 1)
   229                                {
   230                                     out += '<div id="tipue_search_results_count">1 result</div>';
   231                                }
   232                                else
   233                                {
   234                                     c_c = c.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
   235                                     out += '<div id="tipue_search_results_count">' + c_c + ' results</div>';
   236                                }
   237                                
   238                                found.sort();
   239                                var l_o = 0;
   240                                for (var i = 0; i < found.length; i++)
   241                                {
   242                                     var fo = found[i].split('^');
   243                                     if (l_o >= start && l_o < set.show + start)
   244                                     {
   245                                          out += '<div class="tipue_search_content_title"><a href="' + fo[3] + '"' + tipue_search_w + '>' +  fo[1] + '</a></div>';
   246                                                                                  
   247                                          var t = fo[2];
   248                                          var t_d = '';
   249                                          var t_w = t.split(' ');
   250                                          if (t_w.length < set.descriptiveWords)
   251                                          {
   252                                               t_d = t;
   253                                          }
   254                                          else
   255                                          {
   256                                               for (var f = 0; f < set.descriptiveWords; f++)
   257                                               {
   258                                                    t_d += t_w[f] + ' '; 	
   259                                               }
   260                                          }
   261                                          t_d = $.trim(t_d);
   262                                          if (t_d.charAt(t_d.length - 1) != '.')
   263                                          {
   264                                               t_d += ' ...';
   265                                          }
   266                                          out += '<div class="tipue_search_content_text">' + t_d + '</div>';
   267                                          
   268                                          if (set.showURL)
   269                                          {  
   270                                               out += '<div class="tipue_search_content_loc"><a href="' + fo[3] + '"' + tipue_search_w + '>' + fo[3] + '</a></div>';
   271                                          }
   272                                     }
   273                                     l_o++;     
   274                                }
   275                                
   276                                if (c > set.show)
   277                                {
   278                                     var pages = Math.ceil(c / set.show);
   279                                     var page = (start / set.show);
   280                                     out += '<div id="tipue_search_foot"><ul id="tipue_search_foot_boxes">';
   281                                     
   282                                     if (start > 0)
   283                                     {
   284                                         out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (start - set.show) + '_' + replace + '">&#171; Prev</a></li>'; 
   285                                     }
   286                                                         
   287                                     if (page <= 2)
   288                                     {
   289                                          var p_b = pages;
   290                                          if (pages > 3)
   291                                          {
   292                                               p_b = 3;
   293                                          }                    
   294                                          for (var f = 0; f < p_b; f++)
   295                                          {
   296                                               if (f == page)
   297                                               {
   298                                                    out += '<li class="current">' + (f + 1) + '</li>';
   299                                               }
   300                                               else
   301                                               {
   302                                                    out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
   303                                               }
   304                                          }
   305                                     }
   306                                     else
   307                                     {
   308                                          var p_b = pages + 2;
   309                                          if (p_b > pages)
   310                                          {
   311                                               p_b = pages; 
   312                                          }
   313                                          for (var f = page; f < p_b; f++)
   314                                          {
   315                                               if (f == page)
   316                                               {
   317                                                    out += '<li class="current">' + (f + 1) + '</li>';
   318                                               }
   319                                               else
   320                                               {
   321                                                    out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (f * set.show) + '_' + replace + '">' + (f + 1) + '</a></li>';
   322                                               }
   323                                          }                         
   324                                     }
   325                                                        
   326                                     if (page + 1 != pages)
   327                                     {
   328                                         out += '<li><a href="javascript:void(0)" class="tipue_search_foot_box" id="' + (start + set.show) + '_' + replace + '">Next &#187;</a></li>'; 
   329                                     }                    
   330                                     
   331                                     out += '</ul></div>';
   332                                }                        
   333                           }
   334                           else
   335                           {
   336                                out += '<div id="tipue_search_warning_head">Nothing found</div>'; 
   337                           }
   338                      }
   339                      else
   340                      {
   341                           if (show_stop)
   342                           {
   343                                out += '<div id="tipue_search_warning_head">Nothing found</div><div id="tipue_search_warning">Common words are largely ignored</div>';     
   344                           }
   345                           else
   346                           {
   347                                out += '<div id="tipue_search_warning_head">Search too short</div>';
   348                                if (set.minimumLength == 1)
   349                                {
   350                                     out += '<div id="tipue_search_warning">Should be one character or more</div>';
   351                                }
   352                                else
   353                                {
   354                                     out += '<div id="tipue_search_warning">Should be ' + set.minimumLength + ' characters or more</div>';
   355                                }
   356                           }
   357                      }
   358                 
   359                      $('#tipue_search_content').html(out);
   360                      $('#tipue_search_content').slideDown(200);
   361                      
   362                      $('#tipue_search_replaced').click(function()
   363                      {
   364                           getTipueSearch(0, false);
   365                      });                
   366                 
   367                      $('.tipue_search_foot_box').click(function()
   368                      {
   369                           var id_v = $(this).attr('id');
   370                           var id_a = id_v.split('_');
   371                      
   372                           getTipueSearch(parseInt(id_a[0]), id_a[1]);
   373                      });                                                       
   374                 }          
   375            
   376            });
   377       };
   378     
   379  })(jQuery);
   380  
   381  
   382  
   383