Changeset 5547430 in ammosreader for doc/build/html/_static/searchtools.js


Ignore:
Timestamp:
05/04/22 16:12:24 (3 years ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
AmmosSource, guix
Children:
1b72462
Parents:
f3421e6
Message:

docs updated

File:
1 edited

Legend:

Unmodified
Added
Removed
  • doc/build/html/_static/searchtools.js

    rf3421e6 r5547430  
    55 * Sphinx JavaScript utilities for the full-text search.
    66 *
    7  * :copyright: Copyright 2007-2019 by the Sphinx team, see AUTHORS.
     7 * :copyright: Copyright 2007-2022 by the Sphinx team, see AUTHORS.
    88 * :license: BSD, see LICENSE for details.
    99 *
     
    3737    // query found in title
    3838    title: 15,
     39    partialTitle: 7,
    3940    // query found in terms
    40     term: 5
     41    term: 5,
     42    partialTerm: 2
    4143  };
    4244}
     
    5658  _queued_query : null,
    5759  _pulse_status : -1,
     60
     61  htmlToText : function(htmlString) {
     62      var virtualDocument = document.implementation.createHTMLDocument('virtual');
     63      var htmlElement = $(htmlString, virtualDocument);
     64      htmlElement.find('.headerlink').remove();
     65      docContent = htmlElement.find('[role=main]')[0];
     66      if(docContent === undefined) {
     67          console.warn("Content block not found. Sphinx search tries to obtain it " +
     68                       "via '[role=main]'. Could you check your theme or template.");
     69          return "";
     70      }
     71      return docContent.textContent || docContent.innerText;
     72  },
    5873
    5974  init : function() {
     
    121136    this.title = $('<h2>' + _('Searching') + '</h2>').appendTo(this.out);
    122137    this.dots = $('<span></span>').appendTo(this.title);
    123     this.status = $('<p style="display: none"></p>').appendTo(this.out);
     138    this.status = $('<p class="search-summary">&nbsp;</p>').appendTo(this.out);
    124139    this.output = $('<ul class="search"/>').appendTo(this.out);
    125140
     
    152167      }
    153168
    154       if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i].match(/^\d+$/) ||
    155           tmp[i] === "") {
     169      if ($u.indexOf(stopwords, tmp[i].toLowerCase()) != -1 || tmp[i] === "") {
    156170        // skip this "word"
    157171        continue;
     
    235249      if (results.length) {
    236250        var item = results.pop();
    237         var listItem = $('<li style="display:none"></li>');
    238         if (DOCUMENTATION_OPTIONS.FILE_SUFFIX === '') {
     251        var listItem = $('<li></li>');
     252        var requestUrl = "";
     253        var linkUrl = "";
     254        if (DOCUMENTATION_OPTIONS.BUILDER === 'dirhtml') {
    239255          // dirhtml builder
    240256          var dirname = item[0] + '/';
     
    244260            dirname = '';
    245261          }
    246           listItem.append($('<a/>').attr('href',
    247             DOCUMENTATION_OPTIONS.URL_ROOT + dirname +
    248             highlightstring + item[2]).html(item[1]));
     262          requestUrl = DOCUMENTATION_OPTIONS.URL_ROOT + dirname;
     263          linkUrl = requestUrl;
     264
    249265        } else {
    250266          // normal html builders
    251           listItem.append($('<a/>').attr('href',
    252             item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX +
     267          requestUrl = DOCUMENTATION_OPTIONS.URL_ROOT + item[0] + DOCUMENTATION_OPTIONS.FILE_SUFFIX;
     268          linkUrl = item[0] + DOCUMENTATION_OPTIONS.LINK_SUFFIX;
     269        }
     270        listItem.append($('<a/>').attr('href',
     271            linkUrl +
    253272            highlightstring + item[2]).html(item[1]));
    254         }
    255273        if (item[3]) {
    256274          listItem.append($('<span> (' + item[3] + ')</span>'));
    257275          Search.output.append(listItem);
    258           listItem.slideDown(5, function() {
     276          setTimeout(function() {
    259277            displayNextItem();
    260           });
     278          }, 5);
    261279        } else if (DOCUMENTATION_OPTIONS.HAS_SOURCE) {
    262           var suffix = DOCUMENTATION_OPTIONS.SOURCELINK_SUFFIX;
    263           if (suffix === undefined) {
    264             suffix = '.txt';
    265           }
    266           $.ajax({url: DOCUMENTATION_OPTIONS.URL_ROOT + '_sources/' + item[5] + (item[5].slice(-suffix.length) === suffix ? '' : suffix),
     280          $.ajax({url: requestUrl,
    267281                  dataType: "text",
    268282                  complete: function(jqxhr, textstatus) {
    269283                    var data = jqxhr.responseText;
    270284                    if (data !== '' && data !== undefined) {
    271                       listItem.append(Search.makeSearchSummary(data, searchterms, hlterms));
     285                      var summary = Search.makeSearchSummary(data, searchterms, hlterms);
     286                      if (summary) {
     287                        listItem.append(summary);
     288                      }
    272289                    }
    273290                    Search.output.append(listItem);
    274                     listItem.slideDown(5, function() {
     291                    setTimeout(function() {
    275292                      displayNextItem();
    276                     });
     293                    }, 5);
    277294                  }});
    278295        } else {
    279296          // no source available, just display title
    280297          Search.output.append(listItem);
    281           listItem.slideDown(5, function() {
     298          setTimeout(function() {
    282299            displayNextItem();
    283           });
     300          }, 5);
    284301        }
    285302      }
     
    312329
    313330    for (var prefix in objects) {
    314       for (var name in objects[prefix]) {
     331      for (var iMatch = 0; iMatch != objects[prefix].length; ++iMatch) {
     332        var match = objects[prefix][iMatch];
     333        var name = match[4];
    315334        var fullname = (prefix ? prefix + '.' : '') + name;
    316         if (fullname.toLowerCase().indexOf(object) > -1) {
     335        var fullnameLower = fullname.toLowerCase()
     336        if (fullnameLower.indexOf(object) > -1) {
    317337          var score = 0;
    318           var parts = fullname.split('.');
     338          var parts = fullnameLower.split('.');
    319339          // check for different match types: exact matches of full name or
    320340          // "last name" (i.e. last dotted part)
    321           if (fullname == object || parts[parts.length - 1] == object) {
     341          if (fullnameLower == object || parts[parts.length - 1] == object) {
    322342            score += Scorer.objNameMatch;
    323343          // matches in last name
     
    325345            score += Scorer.objPartialMatch;
    326346          }
    327           var match = objects[prefix][name];
    328347          var objname = objnames[match[1]][2];
    329348          var title = titles[match[0]];
     
    366385
    367386  /**
     387   * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
     388   */
     389  escapeRegExp : function(string) {
     390    return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
     391  },
     392
     393  /**
    368394   * search for full-text terms in the index
    369395   */
     
    386412        {files: titleterms[word], score: Scorer.title}
    387413      ];
     414      // add support for partial matches
     415      if (word.length > 2) {
     416        var word_regex = this.escapeRegExp(word);
     417        for (var w in terms) {
     418          if (w.match(word_regex) && !terms[word]) {
     419            _o.push({files: terms[w], score: Scorer.partialTerm})
     420          }
     421        }
     422        for (var w in titleterms) {
     423          if (w.match(word_regex) && !titleterms[word]) {
     424              _o.push({files: titleterms[w], score: Scorer.partialTitle})
     425          }
     426        }
     427      }
    388428
    389429      // no match but word was a required one
     
    405445          file = _files[j];
    406446          if (!(file in scoreMap))
    407             scoreMap[file] = {}
     447            scoreMap[file] = {};
    408448          scoreMap[file][word] = o.score;
    409449        }
     
    413453      for (j = 0; j < files.length; j++) {
    414454        file = files[j];
    415         if (file in fileMap)
     455        if (file in fileMap && fileMap[file].indexOf(word) === -1)
    416456          fileMap[file].push(word);
    417457        else
     
    425465
    426466      // check if all requirements are matched
    427       if (fileMap[file].length != searchterms.length)
    428           continue;
     467      var filteredTermCount = // as search terms with length < 3 are discarded: ignore
     468        searchterms.filter(function(term){return term.length > 2}).length
     469      if (
     470        fileMap[file].length != searchterms.length &&
     471        fileMap[file].length != filteredTermCount
     472      ) continue;
    429473
    430474      // ensure that none of the excluded terms is in the search result
     
    457501   * latter for highlighting it.
    458502   */
    459   makeSearchSummary : function(text, keywords, hlwords) {
     503  makeSearchSummary : function(htmlText, keywords, hlwords) {
     504    var text = Search.htmlToText(htmlText);
     505    if (text == "") {
     506      return null;
     507    }
    460508    var textLower = text.toLowerCase();
    461509    var start = 0;
     
    469517      $.trim(text.substr(start, 240)) +
    470518      ((start + 240 - text.length) ? '...' : '');
    471     var rv = $('<div class="context"></div>').text(excerpt);
     519    var rv = $('<p class="context"></p>').text(excerpt);
    472520    $.each(hlwords, function() {
    473521      rv = rv.highlightText(this, 'highlighted');
Note: See TracChangeset for help on using the changeset viewer.