mirror of
https://github.com/squidfunk/mkdocs-material.git
synced 2024-06-14 11:52:32 +03:00
Optimize social plugin: reuse resized logo
This commit is contained in:
parent
5f8269b78f
commit
c681be2f7f
@ -18,6 +18,7 @@
|
|||||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
# IN THE SOFTWARE.
|
# IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import concurrent.futures
|
||||||
import functools
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@ -64,6 +65,9 @@ class SocialPluginConfig(Config):
|
|||||||
# Social plugin
|
# Social plugin
|
||||||
class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._executor = concurrent.futures.ThreadPoolExecutor(4)
|
||||||
|
|
||||||
# Retrieve configuration
|
# Retrieve configuration
|
||||||
def on_config(self, config):
|
def on_config(self, config):
|
||||||
self.color = colors.get("indigo")
|
self.color = colors.get("indigo")
|
||||||
@ -108,7 +112,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
|||||||
self.color = { **self.color, **self.config.cards_color }
|
self.color = { **self.color, **self.config.cards_color }
|
||||||
|
|
||||||
# Retrieve logo and font
|
# Retrieve logo and font
|
||||||
self.logo = self._load_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)
|
||||||
|
|
||||||
# Create social cards
|
# Create social cards
|
||||||
@ -167,12 +171,10 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
|||||||
|
|
||||||
# Render social card
|
# Render social card
|
||||||
def _render_card(self, site_name, title, description):
|
def _render_card(self, site_name, title, description):
|
||||||
logo = self.logo
|
|
||||||
|
|
||||||
# Render background and logo
|
# Render background and logo
|
||||||
image = self._render_card_background((1200, 630), self.color["fill"])
|
image = self._render_card_background((1200, 630), self.color["fill"])
|
||||||
image.alpha_composite(
|
image.alpha_composite(
|
||||||
logo.resize((144, int(144 * logo.height / logo.width))),
|
self._resized_logo_promise.result(),
|
||||||
(1200 - 228, 64 - 4)
|
(1200 - 228, 64 - 4)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -304,6 +306,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
|||||||
{ "name": "twitter:image", "content": url }
|
{ "name": "twitter:image", "content": url }
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def _load_resized_logo(self, config, width = 144):
|
||||||
|
logo = self._load_logo(config)
|
||||||
|
height = int(width * logo.height / logo.width)
|
||||||
|
return logo.resize((width, height))
|
||||||
|
|
||||||
# Retrieve logo image or icon
|
# Retrieve logo image or icon
|
||||||
def _load_logo(self, config):
|
def _load_logo(self, config):
|
||||||
theme = config.theme
|
theme = config.theme
|
||||||
|
@ -18,6 +18,7 @@
|
|||||||
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||||
# IN THE SOFTWARE.
|
# IN THE SOFTWARE.
|
||||||
|
|
||||||
|
import concurrent.futures
|
||||||
import functools
|
import functools
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
@ -64,6 +65,9 @@ class SocialPluginConfig(Config):
|
|||||||
# Social plugin
|
# Social plugin
|
||||||
class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
||||||
|
|
||||||
|
def __init__(self):
|
||||||
|
self._executor = concurrent.futures.ThreadPoolExecutor(4)
|
||||||
|
|
||||||
# Retrieve configuration
|
# Retrieve configuration
|
||||||
def on_config(self, config):
|
def on_config(self, config):
|
||||||
self.color = colors.get("indigo")
|
self.color = colors.get("indigo")
|
||||||
@ -108,7 +112,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
|||||||
self.color = { **self.color, **self.config.cards_color }
|
self.color = { **self.color, **self.config.cards_color }
|
||||||
|
|
||||||
# Retrieve logo and font
|
# Retrieve logo and font
|
||||||
self.logo = self._load_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)
|
||||||
|
|
||||||
# Create social cards
|
# Create social cards
|
||||||
@ -167,12 +171,10 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
|||||||
|
|
||||||
# Render social card
|
# Render social card
|
||||||
def _render_card(self, site_name, title, description):
|
def _render_card(self, site_name, title, description):
|
||||||
logo = self.logo
|
|
||||||
|
|
||||||
# Render background and logo
|
# Render background and logo
|
||||||
image = self._render_card_background((1200, 630), self.color["fill"])
|
image = self._render_card_background((1200, 630), self.color["fill"])
|
||||||
image.alpha_composite(
|
image.alpha_composite(
|
||||||
logo.resize((144, int(144 * logo.height / logo.width))),
|
self._resized_logo_promise.result(),
|
||||||
(1200 - 228, 64 - 4)
|
(1200 - 228, 64 - 4)
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -304,6 +306,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
|
|||||||
{ "name": "twitter:image", "content": url }
|
{ "name": "twitter:image", "content": url }
|
||||||
]
|
]
|
||||||
|
|
||||||
|
def _load_resized_logo(self, config, width = 144):
|
||||||
|
logo = self._load_logo(config)
|
||||||
|
height = int(width * logo.height / logo.width)
|
||||||
|
return logo.resize((width, height))
|
||||||
|
|
||||||
# Retrieve logo image or icon
|
# Retrieve logo image or icon
|
||||||
def _load_logo(self, config):
|
def _load_logo(self, config):
|
||||||
theme = config.theme
|
theme = config.theme
|
||||||
|
Loading…
Reference in New Issue
Block a user