Skip to content

Commit 6f71147

Browse files
committed
Merge branch '6-handle-deprecated-urls' into 'master'
Handle deprecated docs Closes #6 See merge request tnir/docs.djangoproject.jp!2
2 parents 51632ff + e7d90dc commit 6f71147

File tree

2 files changed

+94
-17
lines changed

2 files changed

+94
-17
lines changed

redirect.go

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,68 @@ package redirect
22

33
import (
44
"net/http"
5+
"regexp"
56
"strings"
67
)
78

89
const (
9-
domain = "https://docs.djangoproject.com/ja/2.0/"
10-
prefix = "/en/latest/"
11-
suffix = ".html"
10+
base = "https://docs.djangoproject.com/ja/2.0/"
11+
prefix = "/en/latest/"
12+
suffix = ".html"
13+
srcPrefix = "_sources/"
14+
srcSuffix = ".txt"
1215
)
1316

14-
func mapUrl(s string) (r string) {
15-
if strings.HasSuffix(s, suffix) {
16-
s = strings.TrimSuffix(s, suffix) + "/"
17+
func migrateUrl(s string) string {
18+
m := map[string]string{
19+
`howto/apache-auth/`: "howto/deployment/wsgi/apache-auth/",
20+
`howto/deployment/fastcgi/`: "internals/deprecation/#deprecation-removed-in-1-9",
21+
`howto/deployment/modpython/`: "internals/deprecation/#deprecation-removed-in-1-5",
22+
`internals/committers/`: "internals/organization/#prerogatives",
23+
`internals/documentation/`: "internals/contributing/writing-documentation/",
24+
`obsolete/.*`: "internals/deprecation/",
25+
`ref/contrib/comments/.*`: "releases/1.8/#features-removed-in-1-8",
26+
`ref/contrib/csrf/`: "ref/csrf/",
27+
`ref/contrib/databrowse/`: "releases/1.4/#django-contrib-databrowse",
28+
`ref/contrib/formtools/.*`: "releases/1.8/#removal-of-django-contrib-formtools",
29+
`ref/contrib/localflavor/`: "internals/deprecation/#deprecation-removed-in-1-6",
30+
`ref/contrib/webdesign/`: "releases/1.8/#django-contrib-webdesign",
31+
`.*/generic-views.*/`: "topics/class-based-views/",
32+
`releases/1.0-.*/`: "releases/1.0/",
33+
`releases/1.1-.*/`: "releases/1.1/",
1734
}
1835

19-
r = domain
20-
if strings.HasPrefix(s, prefix) {
21-
r += strings.TrimPrefix(s, prefix)
36+
for k, v := range m {
37+
if regexp.MustCompile(k).MatchString(s) {
38+
return v
39+
}
2240
}
2341

24-
return r
42+
return s
43+
}
44+
45+
func mapUrl(s string) string {
46+
if !strings.HasPrefix(s, prefix) {
47+
return base
48+
}
49+
50+
// Remove .html and .txt
51+
for _, _suffix := range []string{suffix, srcSuffix} {
52+
if strings.HasSuffix(s, _suffix) {
53+
s = strings.TrimSuffix(s, _suffix) + "/"
54+
}
55+
}
56+
57+
// Remove /en/latest and _sources
58+
for _, _prefix := range []string{prefix, srcPrefix} {
59+
if strings.HasPrefix(s, _prefix) {
60+
s = strings.TrimPrefix(s, _prefix)
61+
}
62+
}
63+
64+
s = migrateUrl(s)
65+
66+
return base + s
2567
}
2668

2769
func redirect(w http.ResponseWriter, r *http.Request) {

redirect_test.go

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ import (
44
"testing"
55
)
66

7-
func TestMapUrl(t *testing.T) {
7+
func TestMapUrlSimple(t *testing.T) {
88
m := map[string]string{
9-
"": "https://docs.djangoproject.com/ja/2.0/",
10-
"/": "https://docs.djangoproject.com/ja/2.0/",
11-
"/favicon.ico": "https://docs.djangoproject.com/ja/2.0/",
12-
"/en/latest/faq/": "https://docs.djangoproject.com/ja/2.0/faq/",
13-
"/ja/latest/faq/": "https://docs.djangoproject.com/ja/2.0/",
14-
"/en/latest/contents.html": "https://docs.djangoproject.com/ja/2.0/contents/",
9+
"": "https://docs.djangoproject.com/ja/2.0/",
10+
"/": "https://docs.djangoproject.com/ja/2.0/",
11+
"/favicon.ico": "https://docs.djangoproject.com/ja/2.0/",
12+
"/en/latest/faq/": "https://docs.djangoproject.com/ja/2.0/faq/",
13+
"/ja/latest/faq/": "https://docs.djangoproject.com/ja/2.0/",
14+
"/en/latest/contents.html": "https://docs.djangoproject.com/ja/2.0/contents/",
15+
"/en/latest/_sources/ref/forms/fields.txt": "https://docs.djangoproject.com/ja/2.0/ref/forms/fields/",
1516
}
1617

1718
for k, v := range m {
@@ -20,3 +21,37 @@ func TestMapUrl(t *testing.T) {
2021
}
2122
}
2223
}
24+
25+
func TestMapUrlMigration(t *testing.T) {
26+
m := map[string]string{
27+
"/en/latest/howto/apache-auth.html": "https://docs.djangoproject.com/ja/2.0/howto/deployment/wsgi/apache-auth/",
28+
"/en/latest/howto/deployment/fastcgi.html": "https://docs.djangoproject.com/ja/2.0/internals/deprecation/#deprecation-removed-in-1-9",
29+
"/en/latest/howto/deployment/modpython.html": "https://docs.djangoproject.com/ja/2.0/internals/deprecation/#deprecation-removed-in-1-5",
30+
"/en/latest/internals/committers.html": "https://docs.djangoproject.com/ja/2.0/internals/organization/#prerogatives",
31+
"/en/latest/internals/documentation.html": "https://docs.djangoproject.com/ja/2.0/internals/contributing/writing-documentation/",
32+
"/en/latest/obsolete/": "https://docs.djangoproject.com/ja/2.0/internals/deprecation/",
33+
"/en/latest/obsolete/admin-css.html": "https://docs.djangoproject.com/ja/2.0/internals/deprecation/",
34+
"/en/latest/ref/contrib/comments/": "https://docs.djangoproject.com/ja/2.0/releases/1.8/#features-removed-in-1-8",
35+
"/en/latest/ref/contrib/comments/custom.html": "https://docs.djangoproject.com/ja/2.0/releases/1.8/#features-removed-in-1-8",
36+
"/en/latest/ref/contrib/csrf.html": "https://docs.djangoproject.com/ja/2.0/ref/csrf/",
37+
"/en/latest/ref/contrib/databrowse.html": "https://docs.djangoproject.com/ja/2.0/releases/1.4/#django-contrib-databrowse",
38+
"/en/latest/ref/contrib/formtools/": "https://docs.djangoproject.com/ja/2.0/releases/1.8/#removal-of-django-contrib-formtools",
39+
"/en/latest/ref/contrib/formtools/form-preview.html": "https://docs.djangoproject.com/ja/2.0/releases/1.8/#removal-of-django-contrib-formtools",
40+
"/en/latest/ref/contrib/localflavor.html": "https://docs.djangoproject.com/ja/2.0/internals/deprecation/#deprecation-removed-in-1-6",
41+
"/en/latest/ref/contrib/webdesign.html": "https://docs.djangoproject.com/ja/2.0/releases/1.8/#django-contrib-webdesign",
42+
"/en/latest/ref/generic-views.html": "https://docs.djangoproject.com/ja/2.0/topics/class-based-views/",
43+
"/en/latest/topics/generic-views-migration.html": "https://docs.djangoproject.com/ja/2.0/topics/class-based-views/",
44+
"/en/latest/topics/http/generic-views.html": "https://docs.djangoproject.com/ja/2.0/topics/class-based-views/",
45+
"/en/latest/topics/generic-views.html": "https://docs.djangoproject.com/ja/2.0/topics/class-based-views/",
46+
"/en/latest/releases/1.0-alpha-1.html": "https://docs.djangoproject.com/ja/2.0/releases/1.0/",
47+
"/en/latest/releases/1.0-alpha-2.html": "https://docs.djangoproject.com/ja/2.0/releases/1.0/",
48+
"/en/latest/releases/1.0-beta.html": "https://docs.djangoproject.com/ja/2.0/releases/1.0/",
49+
"/en/latest/releases/1.1-alpha-1.html": "https://docs.djangoproject.com/ja/2.0/releases/1.1/",
50+
}
51+
52+
for k, v := range m {
53+
if r := mapUrl(k); r != v {
54+
t.Errorf("mapUrl(%q) is expected with migration: %q, but actually %q", k, v, r)
55+
}
56+
}
57+
}

0 commit comments

Comments
 (0)