Merge pull request #5447 from kamilkrzyskow/social-patch

Fix crash in social plugin and add support for logo in custom_dir
This commit is contained in:
Martin Donath 2023-05-02 08:49:33 +02:00 committed by GitHub
commit 5c57458256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 59 additions and 14 deletions

View File

@ -19,12 +19,11 @@ The social preview image for the page on [setting up site analytics].
</figure> </figure>
[^1]: [^1]:
Both types of logos, images (`theme.logo`) and icons (`theme.icon.logo`) Both types of logos, images ([`theme.logo`](changing-the-logo-and-icons.md#image))
are supported. While an image logo is used as-is, icons are filled with the and icons ([`theme.icon.logo`](changing-the-logo-and-icons.md#icon-bundled)) are supported.
color used in the header (white or black), which depends on the primary While an image logo is used as-is, icons are filled with the
color. Note that custom logos and icons must reside in the `docs_dir` for ([`social.cards_color.text`](#+social.cards_color)) color. Valid file paths inside the
the plugin to find them. For guidance, see #4920. This limitation will be [`custom_dir`](../customization.md#setup-and-theme-structure) will take priority.
lifted in the future when the social plugin will receive its next update.
[colors]: changing-the-colors.md#primary-color [colors]: changing-the-colors.md#primary-color
[fonts]: changing-the-fonts.md#regular-font [fonts]: changing-the-fonts.md#regular-font

View File

@ -68,6 +68,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
def __init__(self): def __init__(self):
self._executor = concurrent.futures.ThreadPoolExecutor(4) self._executor = concurrent.futures.ThreadPoolExecutor(4)
self.custom_dir = None
# Retrieve configuration # Retrieve configuration
def on_config(self, config): def on_config(self, config):
@ -112,6 +113,13 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve color overrides # Retrieve color overrides
self.color = { **self.color, **self.config.cards_color } 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 # Retrieve logo and font
self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config) self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config)
self.font = self._load_font(config) self.font = self._load_font(config)
@ -344,8 +352,15 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
if "logo" in theme: if "logo" in theme:
_, extension = os.path.splitext(theme["logo"]) _, extension = os.path.splitext(theme["logo"])
# Load SVG and convert to PNG
path = os.path.join(config.docs_dir, theme["logo"]) 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": if extension == ".svg":
return self._load_logo_svg(path) return self._load_logo_svg(path)
@ -353,10 +368,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
return Image.open(path).convert("RGBA") return Image.open(path).convert("RGBA")
# Handle icons # Handle icons
logo = "material/library"
icon = theme["icon"] or {} icon = theme["icon"] or {}
if "logo" in icon and icon["logo"]: if "logo" in icon and icon["logo"]:
logo = icon["logo"] logo = icon["logo"]
else:
logo = "material/library"
# Resolve path of package # Resolve path of package
base = os.path.abspath(os.path.join( 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" 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"]) return self._load_logo_svg(path, self.color["text"])
# Load SVG file and convert to PNG # Load SVG file and convert to PNG
@ -388,7 +411,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve from theme (default: Roboto) # Retrieve from theme (default: Roboto)
theme = config.theme theme = config.theme
if theme["font"]: if isinstance(theme["font"], dict) and "text" in theme["font"]:
name = theme["font"]["text"] name = theme["font"]["text"]
else: else:
name = "Roboto" name = "Roboto"

View File

@ -68,6 +68,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
def __init__(self): def __init__(self):
self._executor = concurrent.futures.ThreadPoolExecutor(4) self._executor = concurrent.futures.ThreadPoolExecutor(4)
self.custom_dir = None
# Retrieve configuration # Retrieve configuration
def on_config(self, config): def on_config(self, config):
@ -112,6 +113,13 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve color overrides # Retrieve color overrides
self.color = { **self.color, **self.config.cards_color } 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 # Retrieve logo and font
self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config) self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config)
self.font = self._load_font(config) self.font = self._load_font(config)
@ -344,8 +352,15 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
if "logo" in theme: if "logo" in theme:
_, extension = os.path.splitext(theme["logo"]) _, extension = os.path.splitext(theme["logo"])
# Load SVG and convert to PNG
path = os.path.join(config.docs_dir, theme["logo"]) 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": if extension == ".svg":
return self._load_logo_svg(path) return self._load_logo_svg(path)
@ -353,10 +368,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
return Image.open(path).convert("RGBA") return Image.open(path).convert("RGBA")
# Handle icons # Handle icons
logo = "material/library"
icon = theme["icon"] or {} icon = theme["icon"] or {}
if "logo" in icon and icon["logo"]: if "logo" in icon and icon["logo"]:
logo = icon["logo"] logo = icon["logo"]
else:
logo = "material/library"
# Resolve path of package # Resolve path of package
base = os.path.abspath(os.path.join( 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" 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"]) return self._load_logo_svg(path, self.color["text"])
# Load SVG file and convert to PNG # Load SVG file and convert to PNG
@ -388,7 +411,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve from theme (default: Roboto) # Retrieve from theme (default: Roboto)
theme = config.theme theme = config.theme
if theme["font"]: if isinstance(theme["font"], dict) and "text" in theme["font"]:
name = theme["font"]["text"] name = theme["font"]["text"]
else: else:
name = "Roboto" name = "Roboto"