From 2bb853687dc7158a82cd117de9fcc91558028c22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Krzy=C5=9Bk=C3=B3w?= Date: Thu, 27 Apr 2023 00:05:02 +0200 Subject: [PATCH 1/3] Fixed social plugin crash when only code font present --- material/plugins/social/plugin.py | 2 +- src/plugins/social/plugin.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/material/plugins/social/plugin.py b/material/plugins/social/plugin.py index e70758268..9d222eff8 100644 --- a/material/plugins/social/plugin.py +++ b/material/plugins/social/plugin.py @@ -388,7 +388,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): # Retrieve from theme (default: Roboto) theme = config.theme - if theme["font"]: + if isinstance(theme["font"], dict) and "text" in theme["font"]: name = theme["font"]["text"] else: name = "Roboto" diff --git a/src/plugins/social/plugin.py b/src/plugins/social/plugin.py index e70758268..9d222eff8 100644 --- a/src/plugins/social/plugin.py +++ b/src/plugins/social/plugin.py @@ -388,7 +388,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): # Retrieve from theme (default: Roboto) theme = config.theme - if theme["font"]: + if isinstance(theme["font"], dict) and "text" in theme["font"]: name = theme["font"]["text"] else: name = "Roboto" From 2e37c10d53a46561292c221d2bd9fdc21551a4cc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Krzy=C5=9Bk=C3=B3w?= Date: Thu, 27 Apr 2023 02:38:46 +0200 Subject: [PATCH 2/3] Added support for `theme.custom_dir` in social plugin --- material/plugins/social/plugin.py | 29 ++++++++++++++++++++++++++--- src/plugins/social/plugin.py | 29 ++++++++++++++++++++++++++--- 2 files changed, 52 insertions(+), 6 deletions(-) diff --git a/material/plugins/social/plugin.py b/material/plugins/social/plugin.py index 9d222eff8..509e4f446 100644 --- a/material/plugins/social/plugin.py +++ b/material/plugins/social/plugin.py @@ -68,6 +68,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): def __init__(self): self._executor = concurrent.futures.ThreadPoolExecutor(4) + self.custom_dir = None # Retrieve configuration def on_config(self, config): @@ -112,6 +113,13 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): # Retrieve color overrides self.color = { **self.color, **self.config.cards_color } + # Retrieve custom_dir path + for user_config in config.user_configs: + custom_dir = user_config.get("theme", {}).get("custom_dir") + if custom_dir: + self.custom_dir = custom_dir + break + # Retrieve logo and font self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config) self.font = self._load_font(config) @@ -344,8 +352,15 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): if "logo" in theme: _, extension = os.path.splitext(theme["logo"]) - # Load SVG and convert to PNG path = os.path.join(config.docs_dir, theme["logo"]) + + # Allow users to put the logo inside their custom_dir (theme["logo"] case) + if self.custom_dir: + custom_dir_logo = os.path.join(self.custom_dir, theme["logo"]) + if os.path.exists(custom_dir_logo): + path = custom_dir_logo + + # Load SVG and convert to PNG if extension == ".svg": return self._load_logo_svg(path) @@ -353,10 +368,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): return Image.open(path).convert("RGBA") # Handle icons - logo = "material/library" icon = theme["icon"] or {} if "logo" in icon and icon["logo"]: logo = icon["logo"] + else: + logo = "material/library" # Resolve path of package base = os.path.abspath(os.path.join( @@ -364,8 +380,15 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): "../.." )) - # Load icon data and fill with color path = f"{base}/.icons/{logo}.svg" + + # Allow users to put the logo inside their custom_dir (theme["icon"]["logo"] case) + if self.custom_dir: + custom_dir_logo = os.path.join(self.custom_dir, ".icons", f"{logo}.svg") + if os.path.exists(custom_dir_logo): + path = custom_dir_logo + + # Load icon data and fill with color return self._load_logo_svg(path, self.color["text"]) # Load SVG file and convert to PNG diff --git a/src/plugins/social/plugin.py b/src/plugins/social/plugin.py index 9d222eff8..509e4f446 100644 --- a/src/plugins/social/plugin.py +++ b/src/plugins/social/plugin.py @@ -68,6 +68,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): def __init__(self): self._executor = concurrent.futures.ThreadPoolExecutor(4) + self.custom_dir = None # Retrieve configuration def on_config(self, config): @@ -112,6 +113,13 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): # Retrieve color overrides self.color = { **self.color, **self.config.cards_color } + # Retrieve custom_dir path + for user_config in config.user_configs: + custom_dir = user_config.get("theme", {}).get("custom_dir") + if custom_dir: + self.custom_dir = custom_dir + break + # Retrieve logo and font self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config) self.font = self._load_font(config) @@ -344,8 +352,15 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): if "logo" in theme: _, extension = os.path.splitext(theme["logo"]) - # Load SVG and convert to PNG path = os.path.join(config.docs_dir, theme["logo"]) + + # Allow users to put the logo inside their custom_dir (theme["logo"] case) + if self.custom_dir: + custom_dir_logo = os.path.join(self.custom_dir, theme["logo"]) + if os.path.exists(custom_dir_logo): + path = custom_dir_logo + + # Load SVG and convert to PNG if extension == ".svg": return self._load_logo_svg(path) @@ -353,10 +368,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): return Image.open(path).convert("RGBA") # Handle icons - logo = "material/library" icon = theme["icon"] or {} if "logo" in icon and icon["logo"]: logo = icon["logo"] + else: + logo = "material/library" # Resolve path of package base = os.path.abspath(os.path.join( @@ -364,8 +380,15 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]): "../.." )) - # Load icon data and fill with color path = f"{base}/.icons/{logo}.svg" + + # Allow users to put the logo inside their custom_dir (theme["icon"]["logo"] case) + if self.custom_dir: + custom_dir_logo = os.path.join(self.custom_dir, ".icons", f"{logo}.svg") + if os.path.exists(custom_dir_logo): + path = custom_dir_logo + + # Load icon data and fill with color return self._load_logo_svg(path, self.color["text"]) # Load SVG file and convert to PNG From 9485b6fd118cad5a789553a2fb00da6c22b95475 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Krzy=C5=9Bk=C3=B3w?= Date: Thu, 27 Apr 2023 03:06:44 +0200 Subject: [PATCH 3/3] Updated social cards documentation page --- docs/setup/setting-up-social-cards.md | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/docs/setup/setting-up-social-cards.md b/docs/setup/setting-up-social-cards.md index 48ec14c59..4340c83ab 100644 --- a/docs/setup/setting-up-social-cards.md +++ b/docs/setup/setting-up-social-cards.md @@ -19,12 +19,11 @@ The social preview image for the page on [setting up site analytics]. [^1]: - Both types of logos, images (`theme.logo`) and icons (`theme.icon.logo`) - are supported. While an image logo is used as-is, icons are filled with the - color used in the header (white or black), which depends on the primary - color. Note that custom logos and icons must reside in the `docs_dir` for - the plugin to find them. For guidance, see #4920. This limitation will be - lifted in the future when the social plugin will receive its next update. + Both types of logos, images ([`theme.logo`](changing-the-logo-and-icons.md#image)) + and icons ([`theme.icon.logo`](changing-the-logo-and-icons.md#icon-bundled)) are supported. + While an image logo is used as-is, icons are filled with the + ([`social.cards_color.text`](#+social.cards_color)) color. Valid file paths inside the + [`custom_dir`](../customization.md#setup-and-theme-structure) will take priority. [colors]: changing-the-colors.md#primary-color [fonts]: changing-the-fonts.md#regular-font