11import argparse
2- import glob
32import logging
43import multiprocessing
54import os
65import pathlib
76import re
8- import subprocess
9- import sys
107import tempfile
118import shutil
129
@@ -89,14 +86,16 @@ def findlast(fname, tocheck, *, _cache={}):
8986
9087# note these are all one line so they are easy to search and replace in the
9188# 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 ' )
9593
9694
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 ' )
10099
101100
102101def do_links (root0 ):
@@ -152,7 +151,7 @@ def do_canonicals(dname):
152151 basename = pathlib .Path (* p .parts [1 :])
153152 last = findlast (basename , tocheck )
154153 if last is not None :
155- update_canonical (fullname , last , dname == tocheck [1 ])
154+ update_canonical (fullname , last , dname == tocheck [1 ])
156155
157156
158157def update_canonical (fullname , last , newest ):
@@ -162,26 +161,23 @@ def update_canonical(fullname, last, newest):
162161 removing any other content on a line that has the canonical link.
163162
164163 Also add a banner (div) in the body if an old version of the docs.
165-
164+
166165 Note that if for some reason there are more than one canonical link
167166 this will change all of them.
168167 """
169168 p = pathlib .Path (fullname )
170169 pre = "https://matplotlib.org/"
171170 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 )} " )
174173 rec = re .compile (b'<link rel="canonical" href=".*"' )
175174 with tempfile .NamedTemporaryFile (delete = False ) as fout :
176175 found = False
177176 with open (fullname , "rb" ) as fin :
178177 for line in fin :
179178 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 )
185181 _log .debug (f"new { line } ->{ ll } " )
186182 fout .write (ll )
187183 found = True
@@ -194,12 +190,11 @@ def update_canonical(fullname, last, newest):
194190 newcanon )
195191 else :
196192 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:
200196 fout .write (line )
201197
202-
203198 else :
204199 fout .write (line )
205200
@@ -211,12 +206,10 @@ def update_canonical(fullname, last, newest):
211206 parser = argparse .ArgumentParser (description = "Optional app description" )
212207
213208 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" )
220213
221214 args = parser .parse_args ()
222215 if args .np :
@@ -244,7 +237,8 @@ def update_canonical(fullname, last, newest):
244237 _log .info (f"Rewriting HTML: { fullname } in { last } " )
245238 with open (fullname , "w" ) as fout :
246239 oldname = os .path .join (last , fullname )
247- st = html_redirect % (oldname , "/" + oldname , oldname )
240+ st = html_redirect % (oldname , "/" + oldname ,
241+ oldname )
248242 fout .write (st )
249243 _log .info ("Done links and redirects" )
250244
0 commit comments