Python Library Stringtools
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

doctools.js 8.9KB

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