doctools.js 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * doctools.js
  3. * ~~~~~~~~~~~
  4. *
  5. * Sphinx JavaScript utilities for all documentation.
  6. *
  7. * :copyright: Copyright 2007-2011 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. * small function to check if an array contains
  60. * a given item.
  61. */
  62. jQuery.contains = function(arr, item) {
  63. for (var i = 0; i < arr.length; i++) {
  64. if (arr[i] == item)
  65. return true;
  66. }
  67. return false;
  68. };
  69. /**
  70. * highlight a given string on a jquery object by wrapping it in
  71. * span elements with the given class name.
  72. */
  73. jQuery.fn.highlightText = function(text, className) {
  74. function highlight(node) {
  75. if (node.nodeType == 3) {
  76. var val = node.nodeValue;
  77. var pos = val.toLowerCase().indexOf(text);
  78. if (pos >= 0 && !jQuery(node.parentNode).hasClass(className)) {
  79. var span = document.createElement("span");
  80. span.className = className;
  81. span.appendChild(document.createTextNode(val.substr(pos, text.length)));
  82. node.parentNode.insertBefore(span, node.parentNode.insertBefore(
  83. document.createTextNode(val.substr(pos + text.length)),
  84. node.nextSibling));
  85. node.nodeValue = val.substr(0, pos);
  86. }
  87. }
  88. else if (!jQuery(node).is("button, select, textarea")) {
  89. jQuery.each(node.childNodes, function() {
  90. highlight(this);
  91. });
  92. }
  93. }
  94. return this.each(function() {
  95. highlight(this);
  96. });
  97. };
  98. /**
  99. * Small JavaScript module for the documentation.
  100. */
  101. var Documentation = {
  102. init : function() {
  103. this.fixFirefoxAnchorBug();
  104. this.highlightSearchWords();
  105. this.initIndexTable();
  106. },
  107. /**
  108. * i18n support
  109. */
  110. TRANSLATIONS : {},
  111. PLURAL_EXPR : function(n) { return n == 1 ? 0 : 1; },
  112. LOCALE : 'unknown',
  113. // gettext and ngettext don't access this so that the functions
  114. // can safely bound to a different name (_ = Documentation.gettext)
  115. gettext : function(string) {
  116. var translated = Documentation.TRANSLATIONS[string];
  117. if (typeof translated == 'undefined')
  118. return string;
  119. return (typeof translated == 'string') ? translated : translated[0];
  120. },
  121. ngettext : function(singular, plural, n) {
  122. var translated = Documentation.TRANSLATIONS[singular];
  123. if (typeof translated == 'undefined')
  124. return (n == 1) ? singular : plural;
  125. return translated[Documentation.PLURALEXPR(n)];
  126. },
  127. addTranslations : function(catalog) {
  128. for (var key in catalog.messages)
  129. this.TRANSLATIONS[key] = catalog.messages[key];
  130. this.PLURAL_EXPR = new Function('n', 'return +(' + catalog.plural_expr + ')');
  131. this.LOCALE = catalog.locale;
  132. },
  133. /**
  134. * add context elements like header anchor links
  135. */
  136. addContextElements : function() {
  137. $('div[id] > :header:first').each(function() {
  138. $('<a class="headerlink">\u00B6</a>').
  139. attr('href', '#' + this.id).
  140. attr('title', _('Permalink to this headline')).
  141. appendTo(this);
  142. });
  143. $('dt[id]').each(function() {
  144. $('<a class="headerlink">\u00B6</a>').
  145. attr('href', '#' + this.id).
  146. attr('title', _('Permalink to this definition')).
  147. appendTo(this);
  148. });
  149. },
  150. /**
  151. * workaround a firefox stupidity
  152. */
  153. fixFirefoxAnchorBug : function() {
  154. if (document.location.hash && $.browser.mozilla)
  155. window.setTimeout(function() {
  156. document.location.href += '';
  157. }, 10);
  158. },
  159. /**
  160. * highlight the search words provided in the url in the text
  161. */
  162. highlightSearchWords : function() {
  163. var params = $.getQueryParameters();
  164. var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : [];
  165. if (terms.length) {
  166. var body = $('div.body');
  167. window.setTimeout(function() {
  168. $.each(terms, function() {
  169. body.highlightText(this.toLowerCase(), 'highlighted');
  170. });
  171. }, 10);
  172. $('<p class="highlight-link"><a href="javascript:Documentation.' +
  173. 'hideSearchWords()">' + _('Hide Search Matches') + '</a></p>')
  174. .appendTo($('#searchbox'));
  175. }
  176. },
  177. /**
  178. * init the domain index toggle buttons
  179. */
  180. initIndexTable : function() {
  181. var togglers = $('img.toggler').click(function() {
  182. var src = $(this).attr('src');
  183. var idnum = $(this).attr('id').substr(7);
  184. $('tr.cg-' + idnum).toggle();
  185. if (src.substr(-9) == 'minus.png')
  186. $(this).attr('src', src.substr(0, src.length-9) + 'plus.png');
  187. else
  188. $(this).attr('src', src.substr(0, src.length-8) + 'minus.png');
  189. }).css('display', '');
  190. if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) {
  191. togglers.click();
  192. }
  193. },
  194. /**
  195. * helper function to hide the search marks again
  196. */
  197. hideSearchWords : function() {
  198. $('#searchbox .highlight-link').fadeOut(300);
  199. $('span.highlighted').removeClass('highlighted');
  200. },
  201. /**
  202. * make the url absolute
  203. */
  204. makeURL : function(relativeURL) {
  205. return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL;
  206. },
  207. /**
  208. * get the current relative url
  209. */
  210. getCurrentURL : function() {
  211. var path = document.location.pathname;
  212. var parts = path.split(/\//);
  213. $.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() {
  214. if (this == '..')
  215. parts.pop();
  216. });
  217. var url = parts.join('/');
  218. return path.substring(url.lastIndexOf('/') + 1, path.length - 1);
  219. }
  220. };
  221. // quick alias for translations
  222. _ = Documentation.gettext;
  223. $(document).ready(function() {
  224. Documentation.init();
  225. });