doctools.js 9.0KB

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