doctools.js 11 KB

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