@@ -76,17 +76,28 @@ def findlast(fname, tocheck, *, _cache={}):
76
76
<html lang="en">
77
77
<head>
78
78
<meta charset="utf-8">
79
- <meta http-equiv="refresh" content="0;url= %s" />
80
- <link rel="canonical" href="https://matplotlib.org%s" />
79
+ <meta http-equiv="refresh" content="0;%s" />
80
+ <link rel="canonical" href="url= https://matplotlib.org%s" />
81
81
</head>
82
82
<body>
83
83
<h1>
84
- The page been moved <a href="%s">here </a>!
84
+ The page been moved to <a href="%s"</a>
85
85
</h1>
86
86
</body>
87
87
</html>
88
88
"""
89
89
90
+ # note these are all one line so they are easy to search and replace in the
91
+ # html files (otherwise we need to close tags)
92
+ warn_banner_exists = ('<div id="olddocs-message"> You are reading an old '
93
+ 'version of the documentation (v%s). For the latest version see '
94
+ '<a href="%s">%s</a></div>\n ' )
95
+
96
+
97
+ warn_banner_old = ('<div id="olddocs-message"> You are reading an old '
98
+ 'version of the documentation (v%s). For the latest version see '
99
+ '<a href="/stable/">https://matplotlib.org/stable/</a> </div>\n ' )
100
+
90
101
91
102
def do_links (root0 ):
92
103
"""
@@ -141,19 +152,17 @@ def do_canonicals(dname):
141
152
basename = pathlib .Path (* p .parts [1 :])
142
153
last = findlast (basename , tocheck )
143
154
if last is not None :
144
- update_canonical (fullname , last )
155
+ update_canonical (fullname , last , dname == tocheck [ 1 ] )
145
156
146
- for d in dirs :
147
- _log .info (f"DIR: { d } " )
148
- do_canonicals (os .path .join (dname , d ))
149
157
150
-
151
- def update_canonical (fullname , last ):
158
+ def update_canonical (fullname , last , newest ):
152
159
"""
153
160
Change the canonical link in *fullname* to the same link in the
154
161
version given by *last*. We do this with a regexp to prevent
155
162
removing any other content on a line that has the canonical link.
156
163
164
+ Also add a banner (div) in the body if an old version of the docs.
165
+
157
166
Note that if for some reason there are more than one canonical link
158
167
this will change all of them.
159
168
"""
@@ -169,14 +178,31 @@ def update_canonical(fullname, last):
169
178
for line in fin :
170
179
if not found and b'<link rel="canonical"' in line :
171
180
new = bytes (
172
- f'<link rel="canonical" href="{ newcanon } "' , encoding = "utf-8"
181
+ f'<link rel="canonical" href="{ newcanon } "' ,
182
+ encoding = "utf-8"
173
183
)
174
184
ll = rec .sub (new , line )
175
185
_log .debug (f"new { line } ->{ ll } " )
176
186
fout .write (ll )
177
187
found = True
188
+ elif b'<body>' in line and not newest :
189
+ # add a warning right under:
190
+ fout .write (line )
191
+ line = next (fin )
192
+ if last == 'stable' :
193
+ new = warn_banner_exists % (p .parts [0 ], newcanon ,
194
+ newcanon )
195
+ else :
196
+ new = warn_banner_old % (p .parts [0 ])
197
+ fout .write (bytes (new , encoding = "utf-8" ))
198
+ if not b'<div id="olddocs-message">' in line :
199
+ # write the line out if it wasnt' an olddocs-message:
200
+ fout .write (line )
201
+
202
+
178
203
else :
179
204
fout .write (line )
205
+
180
206
shutil .move (fout .name , fullname )
181
207
182
208
@@ -198,6 +224,10 @@ def update_canonical(fullname, last):
198
224
else :
199
225
np = None
200
226
227
+ # figure out the newest version and trim tocheck at the same time:
228
+ tocheck = [t for t in tocheck if os .path .exists (t )]
229
+ print (tocheck )
230
+
201
231
# html redirect or soft link most things in the top-level directory that
202
232
# are not other modules or versioned docs.
203
233
if not args .no_redirects :
0 commit comments