@@ -76,17 +76,28 @@ def findlast(fname, tocheck, *, _cache={}):
7676<html lang="en">
7777 <head>
7878 <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" />
8181 </head>
8282 <body>
8383 <h1>
84- The page been moved <a href="%s">here </a>!
84+ The page been moved to <a href="%s"</a>
8585 </h1>
8686 </body>
8787</html>
8888"""
8989
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+
90101
91102def do_links (root0 ):
92103 """
@@ -141,19 +152,17 @@ def do_canonicals(dname):
141152 basename = pathlib .Path (* p .parts [1 :])
142153 last = findlast (basename , tocheck )
143154 if last is not None :
144- update_canonical (fullname , last )
155+ update_canonical (fullname , last , dname == tocheck [ 1 ] )
145156
146- for d in dirs :
147- _log .info (f"DIR: { d } " )
148- do_canonicals (os .path .join (dname , d ))
149157
150-
151- def update_canonical (fullname , last ):
158+ def update_canonical (fullname , last , newest ):
152159 """
153160 Change the canonical link in *fullname* to the same link in the
154161 version given by *last*. We do this with a regexp to prevent
155162 removing any other content on a line that has the canonical link.
156163
164+ Also add a banner (div) in the body if an old version of the docs.
165+
157166 Note that if for some reason there are more than one canonical link
158167 this will change all of them.
159168 """
@@ -169,14 +178,31 @@ def update_canonical(fullname, last):
169178 for line in fin :
170179 if not found and b'<link rel="canonical"' in line :
171180 new = bytes (
172- f'<link rel="canonical" href="{ newcanon } "' , encoding = "utf-8"
181+ f'<link rel="canonical" href="{ newcanon } "' ,
182+ encoding = "utf-8"
173183 )
174184 ll = rec .sub (new , line )
175185 _log .debug (f"new { line } ->{ ll } " )
176186 fout .write (ll )
177187 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+
178203 else :
179204 fout .write (line )
205+
180206 shutil .move (fout .name , fullname )
181207
182208
@@ -198,6 +224,10 @@ def update_canonical(fullname, last):
198224 else :
199225 np = None
200226
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+
201231 # html redirect or soft link most things in the top-level directory that
202232 # are not other modules or versioned docs.
203233 if not args .no_redirects :
0 commit comments