summaryrefslogtreecommitdiff
path: root/sandbox/py-rest-doc/sphinx/web/oldurls.py
blob: 441dc11521eaa6a9b64e35a6679c6e6943dc3595 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# -*- coding: utf-8 -*-
"""
    sphinx.web.oldurls
    ~~~~~~~~~~~~~~~~~~

    Handle old URLs gracefully.

    :copyright: 2007 by Georg Brandl.
    :license: Python license.
"""

import re

from .wsgiutil import RedirectResponse, NotFound


_module_re = re.compile(r'module-(.*)\.html')
_modobj_re = re.compile(r'(.*)-objects\.html')
_modsub_re = re.compile(r'(.*?)-(.*)\.html')


special_module_names = {
    'main': '__main__',
    'builtin': '__builtin__',
    'future': '__future__',
    'pycompile': 'py_compile',
}

tutorial_nodes = [
    '', '', '',
    'appetite',
    'interpreter',
    'introduction',
    'controlflow',
    'datastructures',
    'modules',
    'inputoutput',
    'errors',
    'classes',
    'stdlib',
    'stdlib2',
    'whatnow',
    'interactive',
    'floatingpoint',
    '',
    'glossary',
]


def handle_html_url(req, url):
    def inner():
        # global special pages
        if url.endswith('/contents.html'):
            return 'contents/'
        if url.endswith('/genindex.html'):
            return 'genindex/'
        if url.endswith('/about.html'):
            return 'about/'
        if url.endswith('/reporting-bugs.html'):
            return 'bugs/'
        if url == 'modindex.html' or url.endswith('/modindex.html'):
            return 'modindex/'
        # modules, macmodules
        if url[:4] in ('lib/', 'mac/'):
            p = '' if url[0] == 'l' else 'mac'
            m = _module_re.match(url[4:])
            if m:
                mn = m.group(1)
                return p + 'modules/' + special_module_names.get(mn, mn)
            # module sub-pages
            m = _modsub_re.match(url[4:])
            if m and not _modobj_re.match(url[4:]):
                mn = m.group(1)
                return p + 'modules/' + special_module_names.get(mn, mn)
        # XXX: handle all others
        # tutorial
        elif url[:4] == 'tut/':
            try:
                node = int(url[8:].partition('.html')[0])
            except ValueError:
                pass
            else:
                if tutorial_nodes[node]:
                    return 'tutorial/' + tutorial_nodes[node]
        # installing: all in one (ATM)
        elif url[:5] == 'inst/':
            return 'install/'
        # no mapping for "documenting Python..."
        # nothing found
        raise NotFound()
    return RedirectResponse('%s?oldurl=1' % inner())