Changeset ada37fa in ammosreader for doc/html/_static/searchtools.js


Ignore:
Timestamp:
05/04/22 12:00:45 (3 years ago)
Author:
Enrico Schwass <ennoausberlin@…>
Branches:
AmmosSource, guix
Children:
64565fe
Parents:
1846087
Message:

docs generated

File:
1 edited

Legend:

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

    r1846087 rada37fa  
    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-2021 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      }
     
    314331      for (var name in objects[prefix]) {
    315332        var fullname = (prefix ? prefix + '.' : '') + name;
    316         if (fullname.toLowerCase().indexOf(object) > -1) {
     333        var fullnameLower = fullname.toLowerCase()
     334        if (fullnameLower.indexOf(object) > -1) {
    317335          var score = 0;
    318           var parts = fullname.split('.');
     336          var parts = fullnameLower.split('.');
    319337          // check for different match types: exact matches of full name or
    320338          // "last name" (i.e. last dotted part)
    321           if (fullname == object || parts[parts.length - 1] == object) {
     339          if (fullnameLower == object || parts[parts.length - 1] == object) {
    322340            score += Scorer.objNameMatch;
    323341          // matches in last name
     
    366384
    367385  /**
     386   * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
     387   */
     388  escapeRegExp : function(string) {
     389    return string.replace(/[.*+\-?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
     390  },
     391
     392  /**
    368393   * search for full-text terms in the index
    369394   */
     
    386411        {files: titleterms[word], score: Scorer.title}
    387412      ];
     413      // add support for partial matches
     414      if (word.length > 2) {
     415        var word_regex = this.escapeRegExp(word);
     416        for (var w in terms) {
     417          if (w.match(word_regex) && !terms[word]) {
     418            _o.push({files: terms[w], score: Scorer.partialTerm})
     419          }
     420        }
     421        for (var w in titleterms) {
     422          if (w.match(word_regex) && !titleterms[word]) {
     423              _o.push({files: titleterms[w], score: Scorer.partialTitle})
     424          }
     425        }
     426      }
    388427
    389428      // no match but word was a required one
     
    405444          file = _files[j];
    406445          if (!(file in scoreMap))
    407             scoreMap[file] = {}
     446            scoreMap[file] = {};
    408447          scoreMap[file][word] = o.score;
    409448        }
     
    413452      for (j = 0; j < files.length; j++) {
    414453        file = files[j];
    415         if (file in fileMap)
     454        if (file in fileMap && fileMap[file].indexOf(word) === -1)
    416455          fileMap[file].push(word);
    417456        else
     
    425464
    426465      // check if all requirements are matched
    427       if (fileMap[file].length != searchterms.length)
    428           continue;
     466      var filteredTermCount = // as search terms with length < 3 are discarded: ignore
     467        searchterms.filter(function(term){return term.length > 2}).length
     468      if (
     469        fileMap[file].length != searchterms.length &&
     470        fileMap[file].length != filteredTermCount
     471      ) continue;
    429472
    430473      // ensure that none of the excluded terms is in the search result
     
    457500   * latter for highlighting it.
    458501   */
    459   makeSearchSummary : function(text, keywords, hlwords) {
     502  makeSearchSummary : function(htmlText, keywords, hlwords) {
     503    var text = Search.htmlToText(htmlText);
     504    if (text == "") {
     505      return null;
     506    }
    460507    var textLower = text.toLowerCase();
    461508    var start = 0;
     
    469516      $.trim(text.substr(start, 240)) +
    470517      ((start + 240 - text.length) ? '...' : '');
    471     var rv = $('<div class="context"></div>').text(excerpt);
     518    var rv = $('<p class="context"></p>').text(excerpt);
    472519    $.each(hlwords, function() {
    473520      rv = rv.highlightText(this, 'highlighted');
Note: See TracChangeset for help on using the changeset viewer.