Skip to content

Commit 380ea0f

Browse files
committed
Fix regex bug: quote issue and empty alt issue (#14 #19)
1 parent 04e57fa commit 380ea0f

File tree

3 files changed

+63
-53
lines changed

3 files changed

+63
-53
lines changed

CHANGELOG

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
mkdocs-glightbox-0.3.4 (2023-04-25)
2+
3+
* Fixed regex bug: quote issue and empty alt issue (#14 #19)
4+
15
mkdocs-glightbox-0.3.3 (2023-04-20)
26

37
* Refactored processing logic with regex (#14)

mkdocs_glightbox/plugin.py

+58-52
Original file line numberDiff line numberDiff line change
@@ -114,60 +114,66 @@ def on_page_content(self, html, page, config, **kwargs):
114114

115115
def wrap_img_with_anchor(self, match, plugin_config, skip_class, meta):
116116
"""Wrap image tags with anchor tags"""
117-
a_pattern = re.compile(r"<a(?P<attr>.*?)>")
118-
if a_pattern.match(match.group(0)):
119-
return match.group(0)
120-
121-
img_tag = match.group(0)
122-
img_attr = match.group("attr")
123-
classes = re.findall(r'class="([^"]+)"', img_attr)
124-
classes = [c for match in classes for c in match.split()]
125-
126-
if set(skip_class) & set(classes):
127-
return img_tag
128-
129-
src = re.search(r"src=[\"\']([^\"\']+)", img_attr).group(1)
130-
a_tag = f'<a class="glightbox" href="{src}" data-type="image"'
131-
# setting data-width and data-height with plugin options
132-
for k, v in plugin_config.items():
133-
a_tag += f' data-{k}="{v}"'
134-
slide_options = [
135-
"title",
136-
"description",
137-
"caption-position",
138-
"gallery",
139-
]
140-
for option in slide_options:
141-
attr = f"data-{option}"
142-
if attr == "data-title":
143-
val = re.search(r"data-title=[\"\']([^\"\']+)", img_attr)
144-
if self.config["auto_caption"] or (
145-
"glightbox.auto_caption" in meta
146-
and meta.get("glightbox.auto_caption", False) is True
147-
):
148-
val = (
149-
val.group(1)
150-
if val
151-
else re.search(r"alt=[\"\']([^\"\']+)", img_attr).group(1)
152-
)
117+
try:
118+
a_pattern = re.compile(r"<a(?P<attr>.*?)>")
119+
if a_pattern.match(match.group(0)):
120+
return match.group(0)
121+
122+
img_tag = match.group(0)
123+
img_attr = match.group("attr")
124+
classes = re.findall(r'class="([^"]+)"', img_attr)
125+
classes = [c for match in classes for c in match.split()]
126+
127+
if set(skip_class) & set(classes):
128+
return img_tag
129+
130+
src = re.search(r"src=[\"\']([^\"\']+)", img_attr).group(1)
131+
a_tag = f'<a class="glightbox" href="{src}" data-type="image"'
132+
# setting data-width and data-height with plugin options
133+
for k, v in plugin_config.items():
134+
a_tag += f' data-{k}="{v}"'
135+
slide_options = [
136+
"title",
137+
"description",
138+
"caption-position",
139+
"gallery",
140+
]
141+
for option in slide_options:
142+
attr = f"data-{option}"
143+
if attr == "data-title":
144+
val = re.search(r"data-title=[\"]([^\"]+)", img_attr)
145+
if self.config["auto_caption"] or (
146+
"glightbox.auto_caption" in meta
147+
and meta.get("glightbox.auto_caption", False) is True
148+
):
149+
if val:
150+
val = val.group(1)
151+
else:
152+
val = re.search(r"alt=[\"]([^\"]+)", img_attr)
153+
val = val.group(1) if val else ""
154+
else:
155+
val = val.group(1) if val else ""
156+
elif attr == "data-caption-position":
157+
val = re.search(r"data-caption-position=[\"]([^\"]+)", img_attr)
158+
val = val.group(1) if val else self.config["caption_position"]
153159
else:
160+
val = re.search(f'{attr}=["]([^"]+)', img_attr)
154161
val = val.group(1) if val else ""
155-
elif attr == "data-caption-position":
156-
val = re.search(r"data-caption-position=[\"\']([^\"\']+)", img_attr)
157-
val = val.group(1) if val else self.config["caption_position"]
158-
else:
159-
val = re.search(f"{attr}=[\"']([^\"']+)", img_attr)
160-
val = val.group(1) if val else ""
161-
162-
# skip val is empty
163-
if val != "":
164-
# convert data-caption-position to data-desc-position
165-
if attr == "data-caption-position":
166-
a_tag += f' data-desc-position="{val}"'
167-
else:
168-
a_tag += f' {attr}="{val}"'
169-
a_tag += f">{img_tag}</a>"
170-
return a_tag
162+
163+
# skip val is empty
164+
if val != "":
165+
# convert data-caption-position to data-desc-position
166+
if attr == "data-caption-position":
167+
a_tag += f' data-desc-position="{val}"'
168+
else:
169+
a_tag += f' {attr}="{val}"'
170+
a_tag += f">{img_tag}</a>"
171+
return a_tag
172+
except Exception as e:
173+
log.warning(
174+
f"Error in wrapping img tag with anchor tag: {e} {match.group(0)}"
175+
)
176+
return match.group(0)
171177

172178
def on_post_build(self, config, **kwargs):
173179
"""Copy glightbox"s css and js files to assets directory"""

setup.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="mkdocs-glightbox",
8-
version="0.3.3",
8+
version="0.3.4",
99
author="Blueswen",
1010
author_email="blueswen.tw@gmail.com",
1111
url="https://blueswen.github.io/mkdocs-glightbox",

0 commit comments

Comments
 (0)