Merge pull request #10263 from nicoa/escape_base_uri_in_extlinks

escape base_uri in extlinks
This commit is contained in:
Takeshi KOMIYA
2022-03-28 00:06:22 +09:00
committed by GitHub

View File

@@ -18,6 +18,7 @@ Both, the url string and the caption string must escape ``%`` as ``%%``.
"""
import re
import sys
import warnings
from typing import Any, Dict, List, Tuple
@@ -65,7 +66,13 @@ class ExternalLinksChecker(SphinxPostTransform):
title = refnode.astext()
for alias, (base_uri, _caption) in self.app.config.extlinks.items():
uri_pattern = re.compile(base_uri.replace('%s', '(?P<value>.+)'))
if sys.version_info < (3, 7):
# Replace a leading backslash because re.escape() inserts a backslash before %
# on python 3.6
uri_pattern = re.compile(re.escape(base_uri).replace('\\%s', '(?P<value>.+)'))
else:
uri_pattern = re.compile(re.escape(base_uri).replace('%s', '(?P<value>.+)'))
match = uri_pattern.match(uri)
if match and match.groupdict().get('value'):
# build a replacement suggestion