Skip to content

Commit 5c57458

Browse files
authored
Merge pull request #5447 from kamilkrzyskow/social-patch
Fix crash in social plugin and add support for logo in custom_dir
2 parents 84959a5 + 9485b6f commit 5c57458

File tree

3 files changed

+59
-14
lines changed

3 files changed

+59
-14
lines changed

docs/setup/setting-up-social-cards.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,11 @@ The social preview image for the page on [setting up site analytics].
1919
</figure>
2020

2121
[^1]:
22-
Both types of logos, images (`theme.logo`) and icons (`theme.icon.logo`)
23-
are supported. While an image logo is used as-is, icons are filled with the
24-
color used in the header (white or black), which depends on the primary
25-
color. Note that custom logos and icons must reside in the `docs_dir` for
26-
the plugin to find them. For guidance, see #4920. This limitation will be
27-
lifted in the future when the social plugin will receive its next update.
22+
Both types of logos, images ([`theme.logo`](changing-the-logo-and-icons.md#image))
23+
and icons ([`theme.icon.logo`](changing-the-logo-and-icons.md#icon-bundled)) are supported.
24+
While an image logo is used as-is, icons are filled with the
25+
([`social.cards_color.text`](#+social.cards_color)) color. Valid file paths inside the
26+
[`custom_dir`](../customization.md#setup-and-theme-structure) will take priority.
2827

2928
[colors]: changing-the-colors.md#primary-color
3029
[fonts]: changing-the-fonts.md#regular-font

material/plugins/social/plugin.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
6868

6969
def __init__(self):
7070
self._executor = concurrent.futures.ThreadPoolExecutor(4)
71+
self.custom_dir = None
7172

7273
# Retrieve configuration
7374
def on_config(self, config):
@@ -112,6 +113,13 @@ def on_config(self, config):
112113
# Retrieve color overrides
113114
self.color = { **self.color, **self.config.cards_color }
114115

116+
# Retrieve custom_dir path
117+
for user_config in config.user_configs:
118+
custom_dir = user_config.get("theme", {}).get("custom_dir")
119+
if custom_dir:
120+
self.custom_dir = custom_dir
121+
break
122+
115123
# Retrieve logo and font
116124
self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config)
117125
self.font = self._load_font(config)
@@ -344,28 +352,43 @@ def _load_logo(self, config):
344352
if "logo" in theme:
345353
_, extension = os.path.splitext(theme["logo"])
346354

347-
# Load SVG and convert to PNG
348355
path = os.path.join(config.docs_dir, theme["logo"])
356+
357+
# Allow users to put the logo inside their custom_dir (theme["logo"] case)
358+
if self.custom_dir:
359+
custom_dir_logo = os.path.join(self.custom_dir, theme["logo"])
360+
if os.path.exists(custom_dir_logo):
361+
path = custom_dir_logo
362+
363+
# Load SVG and convert to PNG
349364
if extension == ".svg":
350365
return self._load_logo_svg(path)
351366

352367
# Load PNG, JPEG, etc.
353368
return Image.open(path).convert("RGBA")
354369

355370
# Handle icons
356-
logo = "material/library"
357371
icon = theme["icon"] or {}
358372
if "logo" in icon and icon["logo"]:
359373
logo = icon["logo"]
374+
else:
375+
logo = "material/library"
360376

361377
# Resolve path of package
362378
base = os.path.abspath(os.path.join(
363379
os.path.dirname(__file__),
364380
"../.."
365381
))
366382

367-
# Load icon data and fill with color
368383
path = f"{base}/.icons/{logo}.svg"
384+
385+
# Allow users to put the logo inside their custom_dir (theme["icon"]["logo"] case)
386+
if self.custom_dir:
387+
custom_dir_logo = os.path.join(self.custom_dir, ".icons", f"{logo}.svg")
388+
if os.path.exists(custom_dir_logo):
389+
path = custom_dir_logo
390+
391+
# Load icon data and fill with color
369392
return self._load_logo_svg(path, self.color["text"])
370393

371394
# Load SVG file and convert to PNG
@@ -388,7 +411,7 @@ def _load_font(self, config):
388411

389412
# Retrieve from theme (default: Roboto)
390413
theme = config.theme
391-
if theme["font"]:
414+
if isinstance(theme["font"], dict) and "text" in theme["font"]:
392415
name = theme["font"]["text"]
393416
else:
394417
name = "Roboto"

src/plugins/social/plugin.py

+27-4
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
6868

6969
def __init__(self):
7070
self._executor = concurrent.futures.ThreadPoolExecutor(4)
71+
self.custom_dir = None
7172

7273
# Retrieve configuration
7374
def on_config(self, config):
@@ -112,6 +113,13 @@ def on_config(self, config):
112113
# Retrieve color overrides
113114
self.color = { **self.color, **self.config.cards_color }
114115

116+
# Retrieve custom_dir path
117+
for user_config in config.user_configs:
118+
custom_dir = user_config.get("theme", {}).get("custom_dir")
119+
if custom_dir:
120+
self.custom_dir = custom_dir
121+
break
122+
115123
# Retrieve logo and font
116124
self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config)
117125
self.font = self._load_font(config)
@@ -344,28 +352,43 @@ def _load_logo(self, config):
344352
if "logo" in theme:
345353
_, extension = os.path.splitext(theme["logo"])
346354

347-
# Load SVG and convert to PNG
348355
path = os.path.join(config.docs_dir, theme["logo"])
356+
357+
# Allow users to put the logo inside their custom_dir (theme["logo"] case)
358+
if self.custom_dir:
359+
custom_dir_logo = os.path.join(self.custom_dir, theme["logo"])
360+
if os.path.exists(custom_dir_logo):
361+
path = custom_dir_logo
362+
363+
# Load SVG and convert to PNG
349364
if extension == ".svg":
350365
return self._load_logo_svg(path)
351366

352367
# Load PNG, JPEG, etc.
353368
return Image.open(path).convert("RGBA")
354369

355370
# Handle icons
356-
logo = "material/library"
357371
icon = theme["icon"] or {}
358372
if "logo" in icon and icon["logo"]:
359373
logo = icon["logo"]
374+
else:
375+
logo = "material/library"
360376

361377
# Resolve path of package
362378
base = os.path.abspath(os.path.join(
363379
os.path.dirname(__file__),
364380
"../.."
365381
))
366382

367-
# Load icon data and fill with color
368383
path = f"{base}/.icons/{logo}.svg"
384+
385+
# Allow users to put the logo inside their custom_dir (theme["icon"]["logo"] case)
386+
if self.custom_dir:
387+
custom_dir_logo = os.path.join(self.custom_dir, ".icons", f"{logo}.svg")
388+
if os.path.exists(custom_dir_logo):
389+
path = custom_dir_logo
390+
391+
# Load icon data and fill with color
369392
return self._load_logo_svg(path, self.color["text"])
370393

371394
# Load SVG file and convert to PNG
@@ -388,7 +411,7 @@ def _load_font(self, config):
388411

389412
# Retrieve from theme (default: Roboto)
390413
theme = config.theme
391-
if theme["font"]:
414+
if isinstance(theme["font"], dict) and "text" in theme["font"]:
392415
name = theme["font"]["text"]
393416
else:
394417
name = "Roboto"

0 commit comments

Comments
 (0)