From 554f589fde62473552a16194b4f77f74c3decf11 Mon Sep 17 00:00:00 2001 From: Nico Albers Date: Sun, 13 Mar 2022 11:21:30 +0100 Subject: [PATCH 1/3] escape base_uri in extlinks to avoid regex issues with URIs containing special characters --- sphinx/ext/extlinks.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sphinx/ext/extlinks.py b/sphinx/ext/extlinks.py index 8caba88429..6601f06777 100644 --- a/sphinx/ext/extlinks.py +++ b/sphinx/ext/extlinks.py @@ -70,7 +70,7 @@ def check_uri(self, refnode: nodes.reference) -> None: title = refnode.astext() for alias, (base_uri, _caption) in self.app.config.extlinks.items(): - uri_pattern = re.compile(base_uri.replace('%s', '(?P.+)')) + uri_pattern = re.compile(re.escape(base_uri).replace('%s', '(?P.+)')) match = uri_pattern.match(uri) if match and match.groupdict().get('value'): # build a replacement suggestion From aee4e42b81d56c57e1311176ce175ba3374baa0a Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 27 Mar 2022 23:55:10 +0900 Subject: [PATCH 2/3] extlink: Strip a leading backslash on compiling pattern --- sphinx/ext/extlinks.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/sphinx/ext/extlinks.py b/sphinx/ext/extlinks.py index 6601f06777..a574d665af 100644 --- a/sphinx/ext/extlinks.py +++ b/sphinx/ext/extlinks.py @@ -26,6 +26,7 @@ """ import re +import sys import warnings from typing import Any, Dict, List, Tuple @@ -70,7 +71,12 @@ def check_uri(self, refnode: nodes.reference) -> None: title = refnode.astext() for alias, (base_uri, _caption) in self.app.config.extlinks.items(): - uri_pattern = re.compile(re.escape(base_uri).replace('%s', '(?P.+)')) + 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.+)')) + else: + uri_pattern = re.compile(re.escape(base_uri).replace('%s', '(?P.+)')) + match = uri_pattern.match(uri) if match and match.groupdict().get('value'): # build a replacement suggestion From 81830cc77047ce39eab056a70b44a2d97848550c Mon Sep 17 00:00:00 2001 From: Takeshi KOMIYA Date: Sun, 27 Mar 2022 23:58:39 +0900 Subject: [PATCH 3/3] Fix a flake8 warning --- sphinx/ext/extlinks.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sphinx/ext/extlinks.py b/sphinx/ext/extlinks.py index a574d665af..659a92b799 100644 --- a/sphinx/ext/extlinks.py +++ b/sphinx/ext/extlinks.py @@ -72,7 +72,8 @@ def check_uri(self, refnode: nodes.reference) -> None: for alias, (base_uri, _caption) in self.app.config.extlinks.items(): if sys.version_info < (3, 7): - # Replace a leading backslash because re.escape() inserts a backslash before % on python 3.6 + # 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.+)')) else: uri_pattern = re.compile(re.escape(base_uri).replace('%s', '(?P.+)'))