Skip to content

Commit 2e37c10

Browse files
committed
Added support for theme.custom_dir in social plugin
1 parent 2bb8536 commit 2e37c10

File tree

2 files changed

+52
-6
lines changed

2 files changed

+52
-6
lines changed

material/plugins/social/plugin.py

+26-3
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

src/plugins/social/plugin.py

+26-3
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

0 commit comments

Comments
 (0)