[FIX] match all occurrences of static/attach

If a markup element contains more than one {static} or {attach} value,
only the last one was replaced.

Simplify the regex to match every occurrence

Fixes getpelican/pelican#3419
This commit is contained in:
Martin 2024-11-03 15:19:20 +01:00
commit 0177773b65
2 changed files with 14 additions and 3 deletions

View file

@ -372,9 +372,7 @@ class Content:
def _get_intrasite_link_regex(self) -> re.Pattern:
intrasite_link_regex = self.settings["INTRASITE_LINK_REGEX"]
regex = rf"""
(?P<markup><[^\>]+ # match tag with all url-value attributes
(?:href|src|poster|data|cite|formaction|action|content)\s*=\s*)
(?P<markup>(?:href|src|poster|data|cite|formaction|action|content)\s*=\s*)
(?P<quote>["\']) # require value to be quoted
(?P<path>{intrasite_link_regex}(?P<value>.*?)) # the url value
(?P=quote)"""

View file

@ -519,6 +519,9 @@ class TestPage(TestBase):
"images/poster.jpg": Static(
"", settings=args["settings"], source_path="images/poster.jpg"
),
"images/placeholder.jpg": Static(
"", settings=args["settings"], source_path="images/placeholder.jpg"
),
}
args["context"]["generated_content"] = {
"article.rst": Article(
@ -547,6 +550,16 @@ class TestPage(TestBase):
content, '<img src="http://static.cool.site/images/poster.jpg"/>'
)
# Image with two links
args["content"] = (
'<img src="{static}/images/placeholder.jpg" data-src="{static}/images/poster.jpg"/>'
)
content = Page(**args).get_content("http://cool.site")
self.assertEqual(
content,
'<img src="http://static.cool.site/images/placeholder.jpg" data-src="http://static.cool.site/images/poster.jpg"/>',
)
# Image link will go to static
args["content"] = '<meta content="{static}/images/poster.jpg"/>'
content = Page(**args).get_content("http://cool.site")