PageSourceSearch

https://numpy.org/doc/_static/versionwarning.js

js numpy.org collected 2026-10-01 07:20:56 UTC 3,207 bytes, 83 lines download raw bytes

1/* -*- js -*-
2 * versionwarning.js
3 *
4 * Display a warning box for obsolete doc versions, and add
5 * <link rel=canonical> to lead search engines to the right place.
6 *
7 * Docs can include this in any script via
8 *
9 * var script = document.createElement("script"); script.type = "text/javascript"; script.src = "/doc/_static/versionwarning.js"; document.head.appendChild(script);
10 *
11 */
12
13$(document).ready(function() {
14    const pathPattern = /^\/doc\/()([0-9.]+)(.*)/;
15    const latestStable = {
16        "": "2.5"
17    };
18    const names = {
19        "": "NumPy"
20    };
21    const getCanonicalUrl = (name, path) => {
22        // Fixup Numpy URL for some pages known to be moved
23        path = path.replace("/reference/generated/numpy.random.",
24                            "/reference/random/generated/numpy.random.");
25        return "https://numpy.org/doc/stable" + path;
26    };
27    const latestUrl = {
28        "": "https://numpy.org/doc/stable/"
29    };
30
31    const showWarning = (msg) => {
32        $('.body').prepend(
33            '<p style="padding: 1em; border: 1px solid red; background: pink;" class="versionwarning">'
34                + msg + '</p>');
35    };
36    const addCanonicalRel = (href) => {
37        $('head').append(
38            '<link rel="canonical" href="' + href + '"/>'
39        );
40    };
41
42    const m = location.pathname.match(pathPattern);
43    if (m !== null) {
44        if ($('.versionwarning').length > 0) {
45            return;
46        }
47
48        // Always add canonical rel tag
49        const canonicalUrl = getCanonicalUrl(m[1], m[3]);
50        addCanonicalRel(canonicalUrl);
51
52        if (m[2] != latestStable[m[1]]) {
53            // For obsolete versions, show a warning
54
55            // Generate an URL for searching this page in stable docs, in
56            // case the canonical URL is not valid
57            let searchWords = document.title.replace(/—[^—]*$/, '').trim();
58            if (!searchWords) {
59                searchWords = document.title.trim();
60            }
61            const searchUrl = latestUrl[m[1]] + "search.html?q=" + encodeURI(searchWords);
62
63            showWarning('This is documentation for an old release of '
64                        + names[m[1]] + ' (version ' + m[2] + '). '
65                        + '<span class="versionwarning-readlink">Read <a href="'
66                        + canonicalUrl + '">this page</a></span> '
67                        + '<span class="versionwarning-searchlink" style="display: none;">Search for <a href="'
68                        + searchUrl + '">this page</a></span>'
69                        + ' in the <a href="' + latestUrl[m[1]] + '">documentation of the '
70                        + 'latest stable release</a> (version ' + latestStable[m[1]] + ').');
71
72            // Check if the canonical page actually exists, and if not,
73            // replace the direct link with a link to the search page
74            $.ajax({
75                url: canonicalUrl,
76                cache: true
77            }).fail(function (jqXHR, textStatus) {
78                $('.versionwarning-readlink').attr('style', 'display: none;');
79                $('.versionwarning-searchlink').attr('style', 'display: inline;');
80            });
81        }
82    }
83});

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.