1/* 2 * doctools.js 3 * ~~~~~~~~~~~ 4 * 5 * Sphinx JavaScript utilities for all documentation. 6 * 7 * :copyright: Copyright 2007-2021 by the Sphinx team, see AUTHORS. 8 * :license: BSD, see LICENSE for details. 9 * 10 */ 11 12/** 13 * select a different prefix for underscore 14 */ 15$u = _.noConflict(); 16 17/** 18 * make the code below compatible with browsers without 19 * an installed firebug like debugger 20if (!window.console || !console.firebug) { 21 var names = ["log", "debug", "info", "warn", "error", "assert", "dir", 22 "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", 23 "profile", "profileEnd"]; 24 window.console = {}; 25 for (var i = 0; i < names.length; ++i) 26 window.console[names[i]] = function() {}; 27} 28 */ 29 30/** 31 * small helper function to urldecode strings 32 * 33 * See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/decodeURIComponent#Decoding_query_parameters_from_a_URL 34 */ 35jQuery.urldecode = function(x) { 36 if (!x) { 37 return x 38 } 39 return decodeURIComponent(x.replace(/\+/g, ' ')); 40}; 41 42/** 43 * small helper function to urlencode strings 44 */ 45jQuery.urlencode = encodeURIComponent; 46 47/** 48 * This function returns the parsed url parameters of the 49 * current request. Multiple values per key are supported, 50 * it will always return arrays of strings for the value parts. 51 */ 52jQuery.getQueryParameters = function(s) { 53 if (typeof s === 'undefined') 54 s = document.location.search; 55 var parts = s.substr(s.indexOf('?') + 1).split('&'); 56 var result = {}; 57 for (var i = 0; i < parts.length; i++) { 58 var tmp = parts[i].split('=', 2); 59 var key = jQuery.urldecode(tmp[0]); 60 var value = jQuery.urldecode(tmp[1]); 61 if (key in result) 62 result[key].push(value); 63 else 64 result[key] = [value]; 65 } 66 return result; 67}; 68 69/** 70 * highlight a given string on a jquery object by wrapping it in 71 * span elements with the given class name. 72 */ 73jQuery.fn.highlightText = function(text, className) { 74 function highlight(node, addItems) { 75 if (node.nodeType === 3) { 76 var val = node.nodeValue; 77 var pos = val.toLowerCase().indexOf(text); 78 if (pos >= 0 && 79 !jQuery(node.parentNode).hasClass(className) && 80 !jQuery(node.parentNode).hasClass("nohighlight")) { 81 var span; 82 var isInSVG = jQuery(node).closest("body, svg, foreignObject").is("svg"); 83 if (isInSVG) { 84 span = document.createElementNS("http://www.w3.org/2000/svg", "tspan"); 85 } else { 86 span = document.createElement("span"); 87 span.className = className; 88 } 89 span.appendChild(document.createTextNode(val.substr(pos, text.length))); 90 node.parentNode.insertBefore(span, node.parentNode.insertBefore( 91 document.createTextNode(val.substr(pos + text.length)), 92 node.nextSibling)); 93 node.nodeValue = val.substr(0, pos); 94 if (isInSVG) { 95 var rect = document.createElementNS("http://www.w3.org/2000/svg", "rect"); 96 var bbox = node.parentElement.getBBox(); 97 rect.x.baseVal.value = bbox.x; 98 rect.y.baseVal.value = bbox.y; 99 rect.width.baseVal.value = bbox.width; 100 rect.height.baseVal.value = bbox.height; 101 rect.setAttribute('class', className); 102 addItems.push({ 103 "parent": node.parentNode, 104 "target": rect}); 105 } 106 } 107 } 108 else if (!jQuery(node).is("button, select, textarea")) { 109 jQuery.each(node.childNodes, function() { 110 highlight(this, addItems); 111 }); 112 } 113 } 114 var addItems = []; 115 var result = this.each(function() { 116 highlight(this, addItems); 117 }); 118 for (var i = 0; i < addItems.length; ++i) { 119 jQuery(addItems[i].parent).before(addItems[i].target); 120 } 121 return result; 122}; 123 124/* 125 * backward compatibility for jQuery.browser 126 * This will be supported until firefox bug is fixed. 127 */ 128if (!jQuery.browser) { 129 jQuery.uaMatch = function(ua) { 130 ua = ua.toLowerCase(); 131 132 var match = /(chrome)[ \/]([\w.]+)/.exec(ua) || 133 /(webkit)[ \/]([\w.]+)/.exec(ua) || 134 /(opera)(?:.*version|)[ \/]([\w.]+)/.exec(ua) || 135 /(msie) ([\w.]+)/.exec(ua) || 136 ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec(ua) || 137 []; 138 139 return { 140 browser: match[ 1 ] || "", 141 version: match[ 2 ] || "0" 142 }; 143 }; 144 jQuery.browser = {}; 145 jQuery.browser[jQuery.uaMatch(navigator.userAgent).browser] = true; 146} 147 148/** 149 * Small JavaScript module for the documentation. 150 */ 151var Documentation = { 152 153 init : function() { 154 this.fixFirefoxAnchorBug(); 155 this.highlightSearchWords(); 156 this.initIndexTable(); 157 if (DOCUMENTATION_OPTIONS.NAVIGATION_WITH_KEYS) { 158 this.initOnKeyListeners(); 159 } 160 }, 161 162 /** 163 * i18n support 164 */ 165 TRANSLATIONS : {}, 166 PLURAL_EXPR : function(n) { return n === 1 ? 0 : 1; }, 167 LOCALE : 'unknown', 168 169 // gettext and ngettext don't access this so that the functions 170 // can safely bound to a different name (_ = Documentation.gettext) 171 gettext : function(string) { 172 var translated = Documentation.TRANSLATIONS[string]; 173 if (typeof translated === 'undefined') 174 return string; 175 return (typeof translated === 'string') ? translated : translated[0]; 176 }, 177 178 ngettext : function(singular, plural, n) { 179 var translated = Documentation.TRANSLATIONS[singular]; 180 if (typeof translated === 'undefined') 181 return (n == 1) ? singular : plural; 182 return translated[Documentation.PLURALEXPR(n)]; 183 }, 184 185 addTranslations : function(catalog) { 186 for (var key in catalog.messages) 187 this.TRANSLATIONS[key] = catalog.messages[key]; 188 this.PLURAL_EXPR = new Function('n', 'return +(' + catalog.plural_expr + ')'); 189 this.LOCALE = catalog.locale; 190 }, 191 192 /** 193 * add context elements like header anchor links 194 */ 195 addContextElements : function() { 196 $('div[id] > :header:first').each(function() { 197 $('<a class="headerlink">\u00B6</a>'). 198 attr('href', '#' + this.id). 199 attr('title', _('Permalink to this headline')). 200 appendTo(this); 201 }); 202 $('dt[id]').each(function() { 203 $('<a class="headerlink">\u00B6</a>'). 204 attr('href', '#' + this.id). 205 attr('title', _('Permalink to this definition')). 206 appendTo(this); 207 }); 208 }, 209 210 /** 211 * workaround a firefox stupidity 212 * see: https://bugzilla.mozilla.org/show_bug.cgi?id=645075 213 */ 214 fixFirefoxAnchorBug : function() { 215 if (document.location.hash && $.browser.mozilla) 216 window.setTimeout(function() { 217 document.location.href += ''; 218 }, 10); 219 }, 220 221 /** 222 * highlight the search words provided in the url in the text 223 */ 224 highlightSearchWords : function() { 225 var params = $.getQueryParameters(); 226 var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : []; 227 if (terms.length) { 228 var body = $('div.body'); 229 if (!body.length) { 230 body = $('body'); 231 } 232 window.setTimeout(function() { 233 $.each(terms, function() { 234 body.highlightText(this.toLowerCase(), 'highlighted'); 235 }); 236 }, 10); 237 $('<p class="highlight-link"><a href="javascript:Documentation.' + 238 'hideSearchWords()">' + _('Hide Search Matches') + '</a></p>') 239 .appendTo($('#searchbox')); 240 } 241 }, 242 243 /** 244 * init the domain index toggle buttons 245 */ 246 initIndexTable : function() { 247 var togglers = $('img.toggler').click(function() { 248 var src = $(this).attr('src'); 249 var idnum = $(this).attr('id').substr(7); 250 $('tr.cg-' + idnum).toggle(); 251 if (src.substr(-9) === 'minus.png') 252 $(this).attr('src', src.substr(0, src.length-9) + 'plus.png'); 253 else 254 $(this).attr('src', src.substr(0, src.length-8) + 'minus.png'); 255 }).css('display', ''); 256 if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) { 257 togglers.click(); 258 } 259 }, 260 261 /** 262 * helper function to hide the search marks again 263 */ 264 hideSearchWords : function() { 265 $('#searchbox .highlight-link').fadeOut(300); 266 $('span.highlighted').removeClass('highlighted'); 267 }, 268 269 /** 270 * make the url absolute 271 */ 272 makeURL : function(relativeURL) { 273 return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL; 274 }, 275 276 /** 277 * get the current relative url 278 */ 279 getCurrentURL : function() { 280 var path = document.location.pathname; 281 var parts = path.split(/\//); 282 $.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() { 283 if (this === '..') 284 parts.pop(); 285 }); 286 var url = parts.join('/'); 287 return path.substring(url.lastIndexOf('/') + 1, path.length - 1); 288 }, 289 290 initOnKeyListeners: function() { 291 $(document).keydown(function(event) { 292 var activeElementType = document.activeElement.tagName; 293 // don't navigate when in search box, textarea, dropdown or button 294 if (activeElementType !== 'TEXTAREA' && activeElementType !== 'INPUT' && activeElementType !== 'SELECT' 295 && activeElementType !== 'BUTTON' && !event.altKey && !event.ctrlKey && !event.metaKey 296 && !event.shiftKey) { 297 switch (event.keyCode) { 298 case 37: // left 299 var prevHref = $('link[rel="prev"]').prop('href'); 300 if (prevHref) { 301 window.location.href = prevHref; 302 return false; 303 } 304 break; 305 case 39: // right 306 var nextHref = $('link[rel="next"]').prop('href'); 307 if (nextHref) { 308 window.location.href = nextHref; 309 return false; 310 } 311 break; 312 } 313 } 314 }); 315 } 316}; 317 318// quick alias for translations 319_ = Documentation.gettext; 320 321$(document).ready(function() { 322 Documentation.init(); 323}); 324