44
44
from .config import PrivacyConfig
45
45
from .parser import FragmentParser
46
46
47
+ DEFAULT_TIMEOUT_IN_SECS = 5
48
+
47
49
# -----------------------------------------------------------------------------
48
50
# Classes
49
51
# -----------------------------------------------------------------------------
@@ -310,21 +312,24 @@ def replace(match: Match):
310
312
# Replace external favicon, preload hint or style sheet
311
313
if rel in ("icon" , "preload" , "stylesheet" ):
312
314
file = self ._queue (url , config )
313
- el .set ("href" , resolve (file ))
315
+ if file :
316
+ el .set ("href" , resolve (file ))
314
317
315
318
# Handle external script or image
316
319
if el .tag == "script" or el .tag == "img" :
317
320
url = urlparse (el .get ("src" ))
318
321
if not self ._is_excluded (url , initiator ):
319
322
file = self ._queue (url , config )
320
- el .set ("src" , resolve (file ))
323
+ if file :
324
+ el .set ("src" , resolve (file ))
321
325
322
326
# Handle external image in SVG
323
327
if el .tag == "image" :
324
328
url = urlparse (el .get ("href" ))
325
329
if not self ._is_excluded (url , initiator ):
326
330
file = self ._queue (url , config )
327
- el .set ("href" , resolve (file ))
331
+ if file :
332
+ el .set ("href" , resolve (file ))
328
333
329
334
# Return element as string
330
335
return self ._print (el )
@@ -380,7 +385,8 @@ def _queue(self, url: URL, config: MkDocsConfig, concurrent = False):
380
385
# Fetch external asset synchronously, as it either has no extension
381
386
# or is fetched from a context in which replacements are done
382
387
else :
383
- self ._fetch (file , config )
388
+ if not self ._fetch (file , config ):
389
+ return None
384
390
385
391
# Register external asset as file - it might have already been
386
392
# registered, and since MkDocs 1.6, trigger a deprecation warning
@@ -404,18 +410,28 @@ def _fetch(self, file: File, config: MkDocsConfig):
404
410
405
411
# Download external asset
406
412
log .info (f"Downloading external file: { file .url } " )
407
- res = requests .get (file .url , headers = {
408
-
409
- # Set user agent explicitly, so Google Fonts gives us *.woff2
410
- # files, which according to caniuse.com is the only format we
411
- # need to download as it covers the entire range of browsers
412
- # we're officially supporting.
413
- "User-Agent" : " " .join ([
414
- "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" ,
415
- "AppleWebKit/537.36 (KHTML, like Gecko)" ,
416
- "Chrome/98.0.4758.102 Safari/537.36"
417
- ])
418
- })
413
+ try :
414
+ res = requests .get (
415
+ file .url ,
416
+ headers = {
417
+ # Set user agent explicitly, so Google Fonts gives us *.woff2
418
+ # files, which according to caniuse.com is the only format we
419
+ # need to download as it covers the entire range of browsers
420
+ # we're officially supporting.
421
+ "User-Agent" : " " .join (
422
+ [
423
+ "Mozilla/5.0 (Windows NT 10.0; Win64; x64)" ,
424
+ "AppleWebKit/537.36 (KHTML, like Gecko)" ,
425
+ "Chrome/98.0.4758.102 Safari/537.36" ,
426
+ ]
427
+ )
428
+ },
429
+ timeout = DEFAULT_TIMEOUT_IN_SECS ,
430
+ )
431
+ res .raise_for_status ()
432
+ except Exception as error : # this could be a ConnectionError or an HTTPError
433
+ log .warning (f"Could not retrieve { file .url } : { error } " )
434
+ return False
419
435
420
436
# Compute expected file extension and append if missing
421
437
mime = res .headers ["content-type" ].split (";" )[0 ]
0 commit comments