Fri, 11 May 2012 14:11:05 +0200
Some more tutorial.
ulalume3@11 | 1 | /* |
ulalume3@11 | 2 | * doctools.js |
ulalume3@11 | 3 | * ~~~~~~~~~~~ |
ulalume3@11 | 4 | * |
ulalume3@11 | 5 | * Sphinx JavaScript utilities for all documentation. |
ulalume3@11 | 6 | * |
ulalume3@11 | 7 | * :copyright: Copyright 2007-2011 by the Sphinx team, see AUTHORS. |
ulalume3@11 | 8 | * :license: BSD, see LICENSE for details. |
ulalume3@11 | 9 | * |
ulalume3@11 | 10 | */ |
ulalume3@11 | 11 | |
ulalume3@11 | 12 | /** |
ulalume3@11 | 13 | * select a different prefix for underscore |
ulalume3@11 | 14 | */ |
ulalume3@11 | 15 | $u = _.noConflict(); |
ulalume3@11 | 16 | |
ulalume3@11 | 17 | /** |
ulalume3@11 | 18 | * make the code below compatible with browsers without |
ulalume3@11 | 19 | * an installed firebug like debugger |
ulalume3@11 | 20 | if (!window.console || !console.firebug) { |
ulalume3@11 | 21 | var names = ["log", "debug", "info", "warn", "error", "assert", "dir", |
ulalume3@11 | 22 | "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", |
ulalume3@11 | 23 | "profile", "profileEnd"]; |
ulalume3@11 | 24 | window.console = {}; |
ulalume3@11 | 25 | for (var i = 0; i < names.length; ++i) |
ulalume3@11 | 26 | window.console[names[i]] = function() {}; |
ulalume3@11 | 27 | } |
ulalume3@11 | 28 | */ |
ulalume3@11 | 29 | |
ulalume3@11 | 30 | /** |
ulalume3@11 | 31 | * small helper function to urldecode strings |
ulalume3@11 | 32 | */ |
ulalume3@11 | 33 | jQuery.urldecode = function(x) { |
ulalume3@11 | 34 | return decodeURIComponent(x).replace(/\+/g, ' '); |
ulalume3@11 | 35 | } |
ulalume3@11 | 36 | |
ulalume3@11 | 37 | /** |
ulalume3@11 | 38 | * small helper function to urlencode strings |
ulalume3@11 | 39 | */ |
ulalume3@11 | 40 | jQuery.urlencode = encodeURIComponent; |
ulalume3@11 | 41 | |
ulalume3@11 | 42 | /** |
ulalume3@11 | 43 | * This function returns the parsed url parameters of the |
ulalume3@11 | 44 | * current request. Multiple values per key are supported, |
ulalume3@11 | 45 | * it will always return arrays of strings for the value parts. |
ulalume3@11 | 46 | */ |
ulalume3@11 | 47 | jQuery.getQueryParameters = function(s) { |
ulalume3@11 | 48 | if (typeof s == 'undefined') |
ulalume3@11 | 49 | s = document.location.search; |
ulalume3@11 | 50 | var parts = s.substr(s.indexOf('?') + 1).split('&'); |
ulalume3@11 | 51 | var result = {}; |
ulalume3@11 | 52 | for (var i = 0; i < parts.length; i++) { |
ulalume3@11 | 53 | var tmp = parts[i].split('=', 2); |
ulalume3@11 | 54 | var key = jQuery.urldecode(tmp[0]); |
ulalume3@11 | 55 | var value = jQuery.urldecode(tmp[1]); |
ulalume3@11 | 56 | if (key in result) |
ulalume3@11 | 57 | result[key].push(value); |
ulalume3@11 | 58 | else |
ulalume3@11 | 59 | result[key] = [value]; |
ulalume3@11 | 60 | } |
ulalume3@11 | 61 | return result; |
ulalume3@11 | 62 | }; |
ulalume3@11 | 63 | |
ulalume3@11 | 64 | /** |
ulalume3@11 | 65 | * small function to check if an array contains |
ulalume3@11 | 66 | * a given item. |
ulalume3@11 | 67 | */ |
ulalume3@11 | 68 | jQuery.contains = function(arr, item) { |
ulalume3@11 | 69 | for (var i = 0; i < arr.length; i++) { |
ulalume3@11 | 70 | if (arr[i] == item) |
ulalume3@11 | 71 | return true; |
ulalume3@11 | 72 | } |
ulalume3@11 | 73 | return false; |
ulalume3@11 | 74 | }; |
ulalume3@11 | 75 | |
ulalume3@11 | 76 | /** |
ulalume3@11 | 77 | * highlight a given string on a jquery object by wrapping it in |
ulalume3@11 | 78 | * span elements with the given class name. |
ulalume3@11 | 79 | */ |
ulalume3@11 | 80 | jQuery.fn.highlightText = function(text, className) { |
ulalume3@11 | 81 | function highlight(node) { |
ulalume3@11 | 82 | if (node.nodeType == 3) { |
ulalume3@11 | 83 | var val = node.nodeValue; |
ulalume3@11 | 84 | var pos = val.toLowerCase().indexOf(text); |
ulalume3@11 | 85 | if (pos >= 0 && !jQuery(node.parentNode).hasClass(className)) { |
ulalume3@11 | 86 | var span = document.createElement("span"); |
ulalume3@11 | 87 | span.className = className; |
ulalume3@11 | 88 | span.appendChild(document.createTextNode(val.substr(pos, text.length))); |
ulalume3@11 | 89 | node.parentNode.insertBefore(span, node.parentNode.insertBefore( |
ulalume3@11 | 90 | document.createTextNode(val.substr(pos + text.length)), |
ulalume3@11 | 91 | node.nextSibling)); |
ulalume3@11 | 92 | node.nodeValue = val.substr(0, pos); |
ulalume3@11 | 93 | } |
ulalume3@11 | 94 | } |
ulalume3@11 | 95 | else if (!jQuery(node).is("button, select, textarea")) { |
ulalume3@11 | 96 | jQuery.each(node.childNodes, function() { |
ulalume3@11 | 97 | highlight(this); |
ulalume3@11 | 98 | }); |
ulalume3@11 | 99 | } |
ulalume3@11 | 100 | } |
ulalume3@11 | 101 | return this.each(function() { |
ulalume3@11 | 102 | highlight(this); |
ulalume3@11 | 103 | }); |
ulalume3@11 | 104 | }; |
ulalume3@11 | 105 | |
ulalume3@11 | 106 | /** |
ulalume3@11 | 107 | * Small JavaScript module for the documentation. |
ulalume3@11 | 108 | */ |
ulalume3@11 | 109 | var Documentation = { |
ulalume3@11 | 110 | |
ulalume3@11 | 111 | init : function() { |
ulalume3@11 | 112 | this.fixFirefoxAnchorBug(); |
ulalume3@11 | 113 | this.highlightSearchWords(); |
ulalume3@11 | 114 | this.initIndexTable(); |
ulalume3@11 | 115 | }, |
ulalume3@11 | 116 | |
ulalume3@11 | 117 | /** |
ulalume3@11 | 118 | * i18n support |
ulalume3@11 | 119 | */ |
ulalume3@11 | 120 | TRANSLATIONS : {}, |
ulalume3@11 | 121 | PLURAL_EXPR : function(n) { return n == 1 ? 0 : 1; }, |
ulalume3@11 | 122 | LOCALE : 'unknown', |
ulalume3@11 | 123 | |
ulalume3@11 | 124 | // gettext and ngettext don't access this so that the functions |
ulalume3@11 | 125 | // can safely bound to a different name (_ = Documentation.gettext) |
ulalume3@11 | 126 | gettext : function(string) { |
ulalume3@11 | 127 | var translated = Documentation.TRANSLATIONS[string]; |
ulalume3@11 | 128 | if (typeof translated == 'undefined') |
ulalume3@11 | 129 | return string; |
ulalume3@11 | 130 | return (typeof translated == 'string') ? translated : translated[0]; |
ulalume3@11 | 131 | }, |
ulalume3@11 | 132 | |
ulalume3@11 | 133 | ngettext : function(singular, plural, n) { |
ulalume3@11 | 134 | var translated = Documentation.TRANSLATIONS[singular]; |
ulalume3@11 | 135 | if (typeof translated == 'undefined') |
ulalume3@11 | 136 | return (n == 1) ? singular : plural; |
ulalume3@11 | 137 | return translated[Documentation.PLURALEXPR(n)]; |
ulalume3@11 | 138 | }, |
ulalume3@11 | 139 | |
ulalume3@11 | 140 | addTranslations : function(catalog) { |
ulalume3@11 | 141 | for (var key in catalog.messages) |
ulalume3@11 | 142 | this.TRANSLATIONS[key] = catalog.messages[key]; |
ulalume3@11 | 143 | this.PLURAL_EXPR = new Function('n', 'return +(' + catalog.plural_expr + ')'); |
ulalume3@11 | 144 | this.LOCALE = catalog.locale; |
ulalume3@11 | 145 | }, |
ulalume3@11 | 146 | |
ulalume3@11 | 147 | /** |
ulalume3@11 | 148 | * add context elements like header anchor links |
ulalume3@11 | 149 | */ |
ulalume3@11 | 150 | addContextElements : function() { |
ulalume3@11 | 151 | $('div[id] > :header:first').each(function() { |
ulalume3@11 | 152 | $('<a class="headerlink">\u00B6</a>'). |
ulalume3@11 | 153 | attr('href', '#' + this.id). |
ulalume3@11 | 154 | attr('title', _('Permalink to this headline')). |
ulalume3@11 | 155 | appendTo(this); |
ulalume3@11 | 156 | }); |
ulalume3@11 | 157 | $('dt[id]').each(function() { |
ulalume3@11 | 158 | $('<a class="headerlink">\u00B6</a>'). |
ulalume3@11 | 159 | attr('href', '#' + this.id). |
ulalume3@11 | 160 | attr('title', _('Permalink to this definition')). |
ulalume3@11 | 161 | appendTo(this); |
ulalume3@11 | 162 | }); |
ulalume3@11 | 163 | }, |
ulalume3@11 | 164 | |
ulalume3@11 | 165 | /** |
ulalume3@11 | 166 | * workaround a firefox stupidity |
ulalume3@11 | 167 | */ |
ulalume3@11 | 168 | fixFirefoxAnchorBug : function() { |
ulalume3@11 | 169 | if (document.location.hash && $.browser.mozilla) |
ulalume3@11 | 170 | window.setTimeout(function() { |
ulalume3@11 | 171 | document.location.href += ''; |
ulalume3@11 | 172 | }, 10); |
ulalume3@11 | 173 | }, |
ulalume3@11 | 174 | |
ulalume3@11 | 175 | /** |
ulalume3@11 | 176 | * highlight the search words provided in the url in the text |
ulalume3@11 | 177 | */ |
ulalume3@11 | 178 | highlightSearchWords : function() { |
ulalume3@11 | 179 | var params = $.getQueryParameters(); |
ulalume3@11 | 180 | var terms = (params.highlight) ? params.highlight[0].split(/\s+/) : []; |
ulalume3@11 | 181 | if (terms.length) { |
ulalume3@11 | 182 | var body = $('div.body'); |
ulalume3@11 | 183 | window.setTimeout(function() { |
ulalume3@11 | 184 | $.each(terms, function() { |
ulalume3@11 | 185 | body.highlightText(this.toLowerCase(), 'highlighted'); |
ulalume3@11 | 186 | }); |
ulalume3@11 | 187 | }, 10); |
ulalume3@11 | 188 | $('<p class="highlight-link"><a href="javascript:Documentation.' + |
ulalume3@11 | 189 | 'hideSearchWords()">' + _('Hide Search Matches') + '</a></p>') |
ulalume3@11 | 190 | .appendTo($('#searchbox')); |
ulalume3@11 | 191 | } |
ulalume3@11 | 192 | }, |
ulalume3@11 | 193 | |
ulalume3@11 | 194 | /** |
ulalume3@11 | 195 | * init the domain index toggle buttons |
ulalume3@11 | 196 | */ |
ulalume3@11 | 197 | initIndexTable : function() { |
ulalume3@11 | 198 | var togglers = $('img.toggler').click(function() { |
ulalume3@11 | 199 | var src = $(this).attr('src'); |
ulalume3@11 | 200 | var idnum = $(this).attr('id').substr(7); |
ulalume3@11 | 201 | $('tr.cg-' + idnum).toggle(); |
ulalume3@11 | 202 | if (src.substr(-9) == 'minus.png') |
ulalume3@11 | 203 | $(this).attr('src', src.substr(0, src.length-9) + 'plus.png'); |
ulalume3@11 | 204 | else |
ulalume3@11 | 205 | $(this).attr('src', src.substr(0, src.length-8) + 'minus.png'); |
ulalume3@11 | 206 | }).css('display', ''); |
ulalume3@11 | 207 | if (DOCUMENTATION_OPTIONS.COLLAPSE_INDEX) { |
ulalume3@11 | 208 | togglers.click(); |
ulalume3@11 | 209 | } |
ulalume3@11 | 210 | }, |
ulalume3@11 | 211 | |
ulalume3@11 | 212 | /** |
ulalume3@11 | 213 | * helper function to hide the search marks again |
ulalume3@11 | 214 | */ |
ulalume3@11 | 215 | hideSearchWords : function() { |
ulalume3@11 | 216 | $('#searchbox .highlight-link').fadeOut(300); |
ulalume3@11 | 217 | $('span.highlighted').removeClass('highlighted'); |
ulalume3@11 | 218 | }, |
ulalume3@11 | 219 | |
ulalume3@11 | 220 | /** |
ulalume3@11 | 221 | * make the url absolute |
ulalume3@11 | 222 | */ |
ulalume3@11 | 223 | makeURL : function(relativeURL) { |
ulalume3@11 | 224 | return DOCUMENTATION_OPTIONS.URL_ROOT + '/' + relativeURL; |
ulalume3@11 | 225 | }, |
ulalume3@11 | 226 | |
ulalume3@11 | 227 | /** |
ulalume3@11 | 228 | * get the current relative url |
ulalume3@11 | 229 | */ |
ulalume3@11 | 230 | getCurrentURL : function() { |
ulalume3@11 | 231 | var path = document.location.pathname; |
ulalume3@11 | 232 | var parts = path.split(/\//); |
ulalume3@11 | 233 | $.each(DOCUMENTATION_OPTIONS.URL_ROOT.split(/\//), function() { |
ulalume3@11 | 234 | if (this == '..') |
ulalume3@11 | 235 | parts.pop(); |
ulalume3@11 | 236 | }); |
ulalume3@11 | 237 | var url = parts.join('/'); |
ulalume3@11 | 238 | return path.substring(url.lastIndexOf('/') + 1, path.length - 1); |
ulalume3@11 | 239 | } |
ulalume3@11 | 240 | }; |
ulalume3@11 | 241 | |
ulalume3@11 | 242 | // quick alias for translations |
ulalume3@11 | 243 | _ = Documentation.gettext; |
ulalume3@11 | 244 | |
ulalume3@11 | 245 | $(document).ready(function() { |
ulalume3@11 | 246 | Documentation.init(); |
ulalume3@11 | 247 | }); |