1
1
import argparse
2
- import glob
3
2
import logging
4
3
import multiprocessing
5
4
import os
6
5
import pathlib
7
6
import re
8
- import subprocess
9
- import sys
10
7
import tempfile
11
8
import shutil
12
9
@@ -89,14 +86,16 @@ def findlast(fname, tocheck, *, _cache={}):
89
86
90
87
# note these are all one line so they are easy to search and replace in the
91
88
# 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 ' )
89
+ warn_banner_exists = (
90
+ '<div id="olddocs-message"> You are reading an old version of the'
91
+ 'documentation (v%s). For the latest version see '
92
+ '<a href="%s">%s</a></div>\n ' )
95
93
96
94
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 ' )
95
+ warn_banner_old = (
96
+ '<div id="olddocs-message"> You are reading an old version of the'
97
+ 'documentation (v%s). For the latest version see '
98
+ '<a href="/stable/">https://matplotlib.org/stable/</a> </div>\n ' )
100
99
101
100
102
101
def do_links (root0 ):
@@ -152,7 +151,7 @@ def do_canonicals(dname):
152
151
basename = pathlib .Path (* p .parts [1 :])
153
152
last = findlast (basename , tocheck )
154
153
if last is not None :
155
- update_canonical (fullname , last , dname == tocheck [1 ])
154
+ update_canonical (fullname , last , dname == tocheck [1 ])
156
155
157
156
158
157
def update_canonical (fullname , last , newest ):
@@ -162,26 +161,23 @@ def update_canonical(fullname, last, newest):
162
161
removing any other content on a line that has the canonical link.
163
162
164
163
Also add a banner (div) in the body if an old version of the docs.
165
-
164
+
166
165
Note that if for some reason there are more than one canonical link
167
166
this will change all of them.
168
167
"""
169
168
p = pathlib .Path (fullname )
170
169
pre = "https://matplotlib.org/"
171
170
pnew = pathlib .Path (last , * p .parts [1 :])
172
- newcanon = f"{ pre + str (pnew )} "
173
- _log .info (f"{ p } to { pre + str (pnew )} " )
171
+ newcanon = f"{ pre } { str (pnew )} "
172
+ _log .info (f"{ p } to { pre } { str (pnew )} " )
174
173
rec = re .compile (b'<link rel="canonical" href=".*"' )
175
174
with tempfile .NamedTemporaryFile (delete = False ) as fout :
176
175
found = False
177
176
with open (fullname , "rb" ) as fin :
178
177
for line in fin :
179
178
if not found and b'<link rel="canonical"' in line :
180
- new = bytes (
181
- f'<link rel="canonical" href="{ newcanon } "' ,
182
- encoding = "utf-8"
183
- )
184
- ll = rec .sub (new , line )
179
+ new = f'<link rel="canonical" href="{ newcanon } "'
180
+ ll = rec .sub (new .encode ("utf-8" ), line )
185
181
_log .debug (f"new { line } ->{ ll } " )
186
182
fout .write (ll )
187
183
found = True
@@ -194,12 +190,11 @@ def update_canonical(fullname, last, newest):
194
190
newcanon )
195
191
else :
196
192
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:
193
+ fout .write (new . encode ( "utf-8" ))
194
+ if b'<div id="olddocs-message">' not in line :
195
+ # write the line out if it wasn't an olddocs-message:
200
196
fout .write (line )
201
197
202
-
203
198
else :
204
199
fout .write (line )
205
200
@@ -211,12 +206,10 @@ def update_canonical(fullname, last, newest):
211
206
parser = argparse .ArgumentParser (description = "Optional app description" )
212
207
213
208
parser .add_argument ("--np" , type = int , help = "Number of processors to use" )
214
- parser .add_argument (
215
- "--no_canonicals" , help = "do not do canonical links" , action = "store_true"
216
- )
217
- parser .add_argument (
218
- "--no_redirects" , help = "do not do redirects links" , action = "store_true"
219
- )
209
+ parser .add_argument ("--no_canonicals" , help = "do not do canonical links" ,
210
+ action = "store_true" )
211
+ parser .add_argument ("--no_redirects" , help = "do not do redirects links" ,
212
+ action = "store_true" )
220
213
221
214
args = parser .parse_args ()
222
215
if args .np :
@@ -244,7 +237,8 @@ def update_canonical(fullname, last, newest):
244
237
_log .info (f"Rewriting HTML: { fullname } in { last } " )
245
238
with open (fullname , "w" ) as fout :
246
239
oldname = os .path .join (last , fullname )
247
- st = html_redirect % (oldname , "/" + oldname , oldname )
240
+ st = html_redirect % (oldname , "/" + oldname ,
241
+ oldname )
248
242
fout .write (st )
249
243
_log .info ("Done links and redirects" )
250
244
0 commit comments