2024-01-06 05:27:06 +03:00
|
|
|
# Copyright (c) 2016-2024 Martin Donath <martin.donath@squidfunk.com>
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
# of this software and associated documentation files (the "Software"), to
|
|
|
|
# deal in the Software without restriction, including without limitation the
|
|
|
|
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
|
|
|
# sell copies of the Software, and to permit persons to whom the Software is
|
|
|
|
# furnished to do so, subject to the following conditions:
|
|
|
|
|
|
|
|
# The above copyright notice and this permission notice shall be included in
|
|
|
|
# all copies or substantial portions of the Software.
|
|
|
|
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
|
|
|
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
|
|
|
# IN THE SOFTWARE.
|
|
|
|
|
2023-08-23 16:15:11 +03:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Disclaimer
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Please note: this version of the social plugin is not actively development
|
|
|
|
# anymore. Instead, Material for MkDocs Insiders ships a complete rewrite of
|
|
|
|
# the plugin which is much more powerful and addresses all shortcomings of
|
|
|
|
# this implementation. Additionally, the new social plugin allows to create
|
|
|
|
# entirely custom social cards. You can probably imagine, that this was a lot
|
|
|
|
# of work to pull off. If you run into problems, or want to have additional
|
|
|
|
# functionality, please consider sponsoring the project. You can then use the
|
|
|
|
# new version of the plugin immediately.
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
2022-10-17 16:51:11 +03:00
|
|
|
import concurrent.futures
|
2022-10-17 15:48:38 +03:00
|
|
|
import functools
|
2022-09-15 19:51:35 +03:00
|
|
|
import logging
|
2022-09-13 13:35:14 +03:00
|
|
|
import os
|
2022-10-01 16:31:23 +03:00
|
|
|
import posixpath
|
2022-09-13 13:35:14 +03:00
|
|
|
import re
|
|
|
|
import requests
|
2023-08-21 18:45:32 +03:00
|
|
|
import sys
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
from collections import defaultdict
|
|
|
|
from hashlib import md5
|
|
|
|
from io import BytesIO
|
2022-09-15 19:51:35 +03:00
|
|
|
from mkdocs.commands.build import DuplicateFilter
|
2023-08-30 17:28:59 +03:00
|
|
|
from mkdocs.exceptions import PluginError
|
2022-09-13 13:35:14 +03:00
|
|
|
from mkdocs.plugins import BasePlugin
|
2024-03-31 10:23:22 +03:00
|
|
|
from mkdocs.utils import copy_file
|
2022-09-13 13:35:14 +03:00
|
|
|
from shutil import copyfile
|
2024-03-31 10:23:22 +03:00
|
|
|
from tempfile import NamedTemporaryFile
|
2024-03-31 12:00:27 +03:00
|
|
|
|
|
|
|
from .config import SocialConfig
|
|
|
|
|
2023-08-30 17:28:59 +03:00
|
|
|
try:
|
|
|
|
from PIL import Image, ImageDraw, ImageFont
|
2024-03-31 12:00:27 +03:00
|
|
|
except ImportError as e:
|
|
|
|
import_errors = {repr(e)}
|
|
|
|
else:
|
|
|
|
import_errors = set()
|
2022-09-13 13:35:14 +03:00
|
|
|
|
2024-03-31 12:00:27 +03:00
|
|
|
cairosvg_error: str = ""
|
|
|
|
|
|
|
|
try:
|
|
|
|
from cairosvg import svg2png
|
|
|
|
except ImportError as e:
|
|
|
|
import_errors.add(repr(e))
|
|
|
|
except OSError as e:
|
|
|
|
cairosvg_error = str(e)
|
2022-09-15 19:51:35 +03:00
|
|
|
|
2023-08-30 17:28:59 +03:00
|
|
|
|
2022-09-13 13:35:14 +03:00
|
|
|
# -----------------------------------------------------------------------------
|
2023-08-21 18:45:32 +03:00
|
|
|
# Classes
|
2022-09-13 13:35:14 +03:00
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
2022-09-24 14:07:09 +03:00
|
|
|
# Social plugin
|
2023-07-05 18:39:33 +03:00
|
|
|
class SocialPlugin(BasePlugin[SocialConfig]):
|
2022-09-13 13:35:14 +03:00
|
|
|
|
2022-10-17 16:51:11 +03:00
|
|
|
def __init__(self):
|
|
|
|
self._executor = concurrent.futures.ThreadPoolExecutor(4)
|
|
|
|
|
2022-09-13 13:35:14 +03:00
|
|
|
# Retrieve configuration
|
|
|
|
def on_config(self, config):
|
|
|
|
self.color = colors.get("indigo")
|
2023-08-30 17:28:59 +03:00
|
|
|
self.config.cards = self.config.enabled
|
2022-09-16 21:03:07 +03:00
|
|
|
if not self.config.cards:
|
2022-09-13 13:35:14 +03:00
|
|
|
return
|
|
|
|
|
2023-08-30 17:28:59 +03:00
|
|
|
# Check dependencies
|
2024-03-31 12:00:27 +03:00
|
|
|
if import_errors:
|
|
|
|
raise PluginError(
|
|
|
|
"Required dependencies of \"social\" plugin not found:\n"
|
|
|
|
+ str("\n".join(map(lambda x: "- " + x, import_errors)))
|
|
|
|
+ "\n\n--> Install with: pip install \"mkdocs-material[imaging]\""
|
|
|
|
)
|
|
|
|
|
|
|
|
if cairosvg_error:
|
2023-08-30 17:28:59 +03:00
|
|
|
raise PluginError(
|
2024-03-31 12:00:27 +03:00
|
|
|
"\"cairosvg\" Python module is installed, but it crashed with:\n"
|
|
|
|
+ cairosvg_error
|
|
|
|
+ "\n\n--> Check out the troubleshooting guide: https://t.ly/MfX6u"
|
2023-08-30 17:28:59 +03:00
|
|
|
)
|
|
|
|
|
2023-05-08 17:59:16 +03:00
|
|
|
# Move color options
|
2023-06-23 12:53:07 +03:00
|
|
|
if self.config.cards_color:
|
2023-05-08 17:59:16 +03:00
|
|
|
|
|
|
|
# Move background color to new option
|
|
|
|
value = self.config.cards_color.get("fill")
|
|
|
|
if value:
|
|
|
|
self.config.cards_layout_options["background_color"] = value
|
|
|
|
|
|
|
|
# Move color to new option
|
|
|
|
value = self.config.cards_color.get("text")
|
|
|
|
if value:
|
|
|
|
self.config.cards_layout_options["color"] = value
|
|
|
|
|
|
|
|
# Move font family to new option
|
2023-06-23 12:53:07 +03:00
|
|
|
if self.config.cards_font:
|
2023-05-08 17:59:16 +03:00
|
|
|
value = self.config.cards_font
|
|
|
|
self.config.cards_layout_options["font_family"] = value
|
|
|
|
|
2022-10-18 08:40:25 +03:00
|
|
|
# Check if site URL is defined
|
|
|
|
if not config.site_url:
|
|
|
|
log.warning(
|
2023-08-19 15:38:58 +03:00
|
|
|
"The \"site_url\" option is not set. The cards are generated, "
|
|
|
|
"but not linked, so they won't be visible on social media."
|
2022-10-18 08:40:25 +03:00
|
|
|
)
|
|
|
|
|
2022-09-13 13:35:14 +03:00
|
|
|
# Ensure presence of cache directory
|
2022-09-16 21:03:07 +03:00
|
|
|
self.cache = self.config.cache_dir
|
2022-09-13 13:35:14 +03:00
|
|
|
if not os.path.isdir(self.cache):
|
|
|
|
os.makedirs(self.cache)
|
|
|
|
|
|
|
|
# Retrieve palette from theme configuration
|
2022-09-16 21:03:07 +03:00
|
|
|
theme = config.theme
|
2022-09-13 13:35:14 +03:00
|
|
|
if "palette" in theme:
|
|
|
|
palette = theme["palette"]
|
|
|
|
|
2024-03-19 06:11:08 +03:00
|
|
|
# Find first palette that includes primary color definition
|
2022-09-13 13:35:14 +03:00
|
|
|
if isinstance(palette, list):
|
2024-03-19 06:11:08 +03:00
|
|
|
for p in palette:
|
|
|
|
if "primary" in p and p["primary"]:
|
|
|
|
palette = p
|
|
|
|
break
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Set colors according to palette
|
|
|
|
if "primary" in palette and palette["primary"]:
|
|
|
|
primary = palette["primary"].replace(" ", "-")
|
|
|
|
self.color = colors.get(primary, self.color)
|
|
|
|
|
|
|
|
# Retrieve color overrides
|
2023-05-08 22:01:01 +03:00
|
|
|
options = self.config.cards_layout_options
|
2023-05-08 17:59:16 +03:00
|
|
|
self.color = {
|
2023-05-08 22:01:01 +03:00
|
|
|
"fill": options.get("background_color", self.color["fill"]),
|
|
|
|
"text": options.get("color", self.color["text"])
|
2023-05-08 17:59:16 +03:00
|
|
|
}
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Retrieve logo and font
|
2022-10-17 16:51:11 +03:00
|
|
|
self._resized_logo_promise = self._executor.submit(self._load_resized_logo, config)
|
2022-09-13 13:35:14 +03:00
|
|
|
self.font = self._load_font(config)
|
|
|
|
|
2022-10-17 16:13:12 +03:00
|
|
|
self._image_promises = []
|
|
|
|
|
2022-09-13 13:35:14 +03:00
|
|
|
# Create social cards
|
|
|
|
def on_page_markdown(self, markdown, page, config, files):
|
2022-09-16 21:03:07 +03:00
|
|
|
if not self.config.cards:
|
2022-09-13 13:35:14 +03:00
|
|
|
return
|
|
|
|
|
|
|
|
# Resolve image directory
|
2022-09-16 21:03:07 +03:00
|
|
|
directory = self.config.cards_dir
|
2022-09-13 13:35:14 +03:00
|
|
|
file, _ = os.path.splitext(page.file.src_path)
|
|
|
|
|
|
|
|
# Resolve path of image
|
|
|
|
path = "{}.png".format(os.path.join(
|
2022-09-16 21:03:07 +03:00
|
|
|
config.site_dir,
|
2022-09-13 13:35:14 +03:00
|
|
|
directory,
|
|
|
|
file
|
|
|
|
))
|
|
|
|
|
|
|
|
# Resolve path of image directory
|
|
|
|
directory = os.path.dirname(path)
|
|
|
|
if not os.path.isdir(directory):
|
|
|
|
os.makedirs(directory)
|
|
|
|
|
|
|
|
# Compute site name
|
2022-09-16 21:03:07 +03:00
|
|
|
site_name = config.site_name
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Compute page title and description
|
|
|
|
title = page.meta.get("title", page.title)
|
2022-09-16 21:03:07 +03:00
|
|
|
description = config.site_description or ""
|
2022-09-13 13:35:14 +03:00
|
|
|
if "description" in page.meta:
|
|
|
|
description = page.meta["description"]
|
|
|
|
|
2023-07-10 19:53:51 +03:00
|
|
|
# Check type of meta title - see https://t.ly/m1Us
|
|
|
|
if not isinstance(title, str):
|
|
|
|
log.error(
|
|
|
|
f"Page meta title of page '{page.file.src_uri}' must be a "
|
|
|
|
f"string, but is of type \"{type(title)}\"."
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
|
|
|
# Check type of meta description - see https://t.ly/m1Us
|
|
|
|
if not isinstance(description, str):
|
|
|
|
log.error(
|
|
|
|
f"Page meta description of '{page.file.src_uri}' must be a "
|
|
|
|
f"string, but is of type \"{type(description)}\"."
|
|
|
|
)
|
|
|
|
sys.exit(1)
|
|
|
|
|
2023-08-23 16:15:11 +03:00
|
|
|
# Generate social card if not in cache
|
2022-09-13 13:35:14 +03:00
|
|
|
hash = md5("".join([
|
|
|
|
site_name,
|
|
|
|
str(title),
|
|
|
|
description
|
|
|
|
]).encode("utf-8"))
|
|
|
|
file = os.path.join(self.cache, f"{hash.hexdigest()}.png")
|
2022-10-17 16:13:12 +03:00
|
|
|
self._image_promises.append(self._executor.submit(
|
|
|
|
self._cache_image,
|
|
|
|
cache_path = file, dest_path = path,
|
|
|
|
render_function = lambda: self._render_card(site_name, title, description)
|
|
|
|
))
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Inject meta tags into page
|
|
|
|
meta = page.meta.get("meta", [])
|
|
|
|
page.meta["meta"] = meta + self._generate_meta(page, config)
|
|
|
|
|
2022-10-17 16:13:12 +03:00
|
|
|
def on_post_build(self, config):
|
2023-01-03 13:13:40 +03:00
|
|
|
if not self.config.cards:
|
|
|
|
return
|
|
|
|
|
2022-10-17 16:13:12 +03:00
|
|
|
# Check for exceptions
|
|
|
|
for promise in self._image_promises:
|
|
|
|
promise.result()
|
|
|
|
|
2022-09-13 13:35:14 +03:00
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
2022-10-17 16:13:12 +03:00
|
|
|
# Render image to cache (if not present), then copy from cache to site
|
|
|
|
def _cache_image(self, cache_path, dest_path, render_function):
|
|
|
|
if not os.path.isfile(cache_path):
|
|
|
|
image = render_function()
|
|
|
|
image.save(cache_path)
|
|
|
|
|
|
|
|
# Copy file from cache
|
|
|
|
copyfile(cache_path, dest_path)
|
|
|
|
|
2022-10-17 15:48:38 +03:00
|
|
|
@functools.lru_cache(maxsize=None)
|
|
|
|
def _get_font(self, kind, size):
|
|
|
|
return ImageFont.truetype(self.font[kind], size)
|
|
|
|
|
2022-09-13 13:35:14 +03:00
|
|
|
# Render social card
|
|
|
|
def _render_card(self, site_name, title, description):
|
|
|
|
# Render background and logo
|
|
|
|
image = self._render_card_background((1200, 630), self.color["fill"])
|
|
|
|
image.alpha_composite(
|
2022-10-17 16:51:11 +03:00
|
|
|
self._resized_logo_promise.result(),
|
2022-09-13 13:35:14 +03:00
|
|
|
(1200 - 228, 64 - 4)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Render site name
|
2022-10-17 15:48:38 +03:00
|
|
|
font = self._get_font("Bold", 36)
|
2022-09-13 13:35:14 +03:00
|
|
|
image.alpha_composite(
|
|
|
|
self._render_text((826, 48), font, site_name, 1, 20),
|
|
|
|
(64 + 4, 64)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Render page title
|
2022-10-17 15:48:38 +03:00
|
|
|
font = self._get_font("Bold", 92)
|
2022-09-13 13:35:14 +03:00
|
|
|
image.alpha_composite(
|
|
|
|
self._render_text((826, 328), font, title, 3, 30),
|
|
|
|
(64, 160)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Render page description
|
2022-10-17 15:48:38 +03:00
|
|
|
font = self._get_font("Regular", 28)
|
2022-09-13 13:35:14 +03:00
|
|
|
image.alpha_composite(
|
|
|
|
self._render_text((826, 80), font, description, 2, 14),
|
|
|
|
(64 + 4, 512)
|
|
|
|
)
|
|
|
|
|
|
|
|
# Return social card image
|
|
|
|
return image
|
|
|
|
|
|
|
|
# Render social card background
|
|
|
|
def _render_card_background(self, size, fill):
|
|
|
|
return Image.new(mode = "RGBA", size = size, color = fill)
|
|
|
|
|
2022-10-17 14:39:12 +03:00
|
|
|
@functools.lru_cache(maxsize=None)
|
|
|
|
def _tmp_context(self):
|
|
|
|
image = Image.new(mode = "RGBA", size = (50, 50))
|
|
|
|
return ImageDraw.Draw(image)
|
|
|
|
|
|
|
|
@functools.lru_cache(maxsize=None)
|
|
|
|
def _text_bounding_box(self, text, font):
|
|
|
|
return self._tmp_context().textbbox((0, 0), text, font = font)
|
|
|
|
|
2022-09-13 13:35:14 +03:00
|
|
|
# Render social card text
|
|
|
|
def _render_text(self, size, font, text, lmax, spacing = 0):
|
2022-10-17 14:39:12 +03:00
|
|
|
width = size[0]
|
2022-09-13 13:35:14 +03:00
|
|
|
lines, words = [], []
|
|
|
|
|
|
|
|
# Remove remnant HTML tags
|
|
|
|
text = re.sub(r"(<[^>]+>)", "", text)
|
|
|
|
|
|
|
|
# Retrieve y-offset of textbox to correct for spacing
|
|
|
|
yoffset = 0
|
|
|
|
|
|
|
|
# Create drawing context and split text into lines
|
|
|
|
for word in text.split(" "):
|
|
|
|
combine = " ".join(words + [word])
|
2022-10-17 14:39:12 +03:00
|
|
|
textbox = self._text_bounding_box(combine, font = font)
|
2022-09-13 13:35:14 +03:00
|
|
|
yoffset = textbox[1]
|
2022-10-17 14:39:12 +03:00
|
|
|
if not words or textbox[2] <= width:
|
2022-09-13 13:35:14 +03:00
|
|
|
words.append(word)
|
|
|
|
else:
|
|
|
|
lines.append(words)
|
|
|
|
words = [word]
|
|
|
|
|
|
|
|
# Join words for each line and create image
|
|
|
|
lines.append(words)
|
|
|
|
lines = [" ".join(line) for line in lines]
|
|
|
|
image = Image.new(mode = "RGBA", size = size)
|
|
|
|
|
|
|
|
# Create drawing context and split text into lines
|
|
|
|
context = ImageDraw.Draw(image)
|
|
|
|
context.text(
|
|
|
|
(0, spacing / 2 - yoffset), "\n".join(lines[:lmax]),
|
|
|
|
font = font, fill = self.color["text"], spacing = spacing - yoffset
|
|
|
|
)
|
|
|
|
|
|
|
|
# Return text image
|
|
|
|
return image
|
|
|
|
|
|
|
|
# -------------------------------------------------------------------------
|
|
|
|
|
|
|
|
# Generate meta tags
|
|
|
|
def _generate_meta(self, page, config):
|
2022-09-16 21:03:07 +03:00
|
|
|
directory = self.config.cards_dir
|
2022-09-19 00:18:11 +03:00
|
|
|
file, _ = os.path.splitext(page.file.src_uri)
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Compute page title
|
|
|
|
title = page.meta.get("title", page.title)
|
|
|
|
if not page.is_homepage:
|
2022-09-16 21:03:07 +03:00
|
|
|
title = f"{title} - {config.site_name}"
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Compute page description
|
2022-09-16 21:03:07 +03:00
|
|
|
description = config.site_description
|
2022-09-13 13:35:14 +03:00
|
|
|
if "description" in page.meta:
|
|
|
|
description = page.meta["description"]
|
|
|
|
|
|
|
|
# Resolve image URL
|
2022-09-19 00:18:11 +03:00
|
|
|
url = "{}.png".format(posixpath.join(
|
2022-10-18 08:40:25 +03:00
|
|
|
config.site_url or ".",
|
2022-09-13 13:35:14 +03:00
|
|
|
directory,
|
|
|
|
file
|
|
|
|
))
|
|
|
|
|
|
|
|
# Ensure forward slashes
|
|
|
|
url = url.replace(os.path.sep, "/")
|
|
|
|
|
|
|
|
# Return meta tags
|
|
|
|
return [
|
|
|
|
|
|
|
|
# Meta tags for Open Graph
|
|
|
|
{ "property": "og:type", "content": "website" },
|
|
|
|
{ "property": "og:title", "content": title },
|
|
|
|
{ "property": "og:description", "content": description },
|
|
|
|
{ "property": "og:image", "content": url },
|
|
|
|
{ "property": "og:image:type", "content": "image/png" },
|
|
|
|
{ "property": "og:image:width", "content": "1200" },
|
|
|
|
{ "property": "og:image:height", "content": "630" },
|
|
|
|
{ "property": "og:url", "content": page.canonical_url },
|
|
|
|
|
|
|
|
# Meta tags for Twitter
|
|
|
|
{ "name": "twitter:card", "content": "summary_large_image" },
|
|
|
|
# { "name": "twitter:site", "content": user },
|
|
|
|
# { "name": "twitter:creator", "content": user },
|
|
|
|
{ "name": "twitter:title", "content": title },
|
|
|
|
{ "name": "twitter:description", "content": description },
|
|
|
|
{ "name": "twitter:image", "content": url }
|
|
|
|
]
|
|
|
|
|
2022-10-17 16:51:11 +03:00
|
|
|
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))
|
|
|
|
|
2022-09-13 13:35:14 +03:00
|
|
|
# Retrieve logo image or icon
|
|
|
|
def _load_logo(self, config):
|
2022-09-16 21:03:07 +03:00
|
|
|
theme = config.theme
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Handle images (precedence over icons)
|
|
|
|
if "logo" in theme:
|
|
|
|
_, extension = os.path.splitext(theme["logo"])
|
|
|
|
|
2022-09-16 21:03:07 +03:00
|
|
|
path = os.path.join(config.docs_dir, theme["logo"])
|
2023-04-27 03:38:46 +03:00
|
|
|
|
|
|
|
# Allow users to put the logo inside their custom_dir (theme["logo"] case)
|
2023-07-25 21:25:45 +03:00
|
|
|
if theme.custom_dir:
|
|
|
|
custom_dir_logo = os.path.join(theme.custom_dir, theme["logo"])
|
2023-04-27 03:38:46 +03:00
|
|
|
if os.path.exists(custom_dir_logo):
|
|
|
|
path = custom_dir_logo
|
|
|
|
|
|
|
|
# Load SVG and convert to PNG
|
2022-09-13 13:35:14 +03:00
|
|
|
if extension == ".svg":
|
|
|
|
return self._load_logo_svg(path)
|
|
|
|
|
|
|
|
# Load PNG, JPEG, etc.
|
|
|
|
return Image.open(path).convert("RGBA")
|
|
|
|
|
|
|
|
# Handle icons
|
|
|
|
icon = theme["icon"] or {}
|
|
|
|
if "logo" in icon and icon["logo"]:
|
|
|
|
logo = icon["logo"]
|
2023-04-27 03:38:46 +03:00
|
|
|
else:
|
|
|
|
logo = "material/library"
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Resolve path of package
|
|
|
|
base = os.path.abspath(os.path.join(
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
"../.."
|
|
|
|
))
|
|
|
|
|
2023-09-20 16:01:13 +03:00
|
|
|
path = f"{base}/templates/.icons/{logo}.svg"
|
2023-04-27 03:38:46 +03:00
|
|
|
|
|
|
|
# Allow users to put the logo inside their custom_dir (theme["icon"]["logo"] case)
|
2023-07-25 21:25:45 +03:00
|
|
|
if theme.custom_dir:
|
|
|
|
custom_dir_logo = os.path.join(theme.custom_dir, ".icons", f"{logo}.svg")
|
2023-04-27 03:38:46 +03:00
|
|
|
if os.path.exists(custom_dir_logo):
|
|
|
|
path = custom_dir_logo
|
|
|
|
|
|
|
|
# Load icon data and fill with color
|
2022-09-13 13:35:14 +03:00
|
|
|
return self._load_logo_svg(path, self.color["text"])
|
|
|
|
|
|
|
|
# Load SVG file and convert to PNG
|
|
|
|
def _load_logo_svg(self, path, fill = None):
|
|
|
|
file = BytesIO()
|
|
|
|
data = open(path).read()
|
|
|
|
|
|
|
|
# Fill with color, if given
|
|
|
|
if fill:
|
|
|
|
data = data.replace("<svg", f"<svg fill=\"{fill}\"")
|
|
|
|
|
|
|
|
# Convert to PNG and return image
|
|
|
|
svg2png(bytestring = data, write_to = file, scale = 10)
|
|
|
|
return Image.open(file)
|
|
|
|
|
|
|
|
# Retrieve font
|
|
|
|
def _load_font(self, config):
|
2023-05-08 17:59:16 +03:00
|
|
|
name = self.config.cards_layout_options.get("font_family")
|
2022-09-13 13:35:14 +03:00
|
|
|
if not name:
|
|
|
|
|
|
|
|
# Retrieve from theme (default: Roboto)
|
2022-09-16 21:03:07 +03:00
|
|
|
theme = config.theme
|
2023-04-27 01:05:02 +03:00
|
|
|
if isinstance(theme["font"], dict) and "text" in theme["font"]:
|
2022-09-13 13:35:14 +03:00
|
|
|
name = theme["font"]["text"]
|
|
|
|
else:
|
|
|
|
name = "Roboto"
|
|
|
|
|
2024-03-31 10:23:22 +03:00
|
|
|
# Resolve relevant fonts
|
2023-08-23 16:15:11 +03:00
|
|
|
font = {}
|
2024-03-31 10:23:22 +03:00
|
|
|
for style in ["Regular", "Bold"]:
|
|
|
|
font[style] = self._resolve_font(name, style)
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# Return available font weights with fallback
|
|
|
|
return defaultdict(lambda: font["Regular"], font)
|
|
|
|
|
2024-03-31 10:23:22 +03:00
|
|
|
# Resolve font family with specific style - if we haven't already done it,
|
|
|
|
# the font family is first downloaded from Google Fonts and the styles are
|
|
|
|
# saved to the cache directory. If the font cannot be resolved, the plugin
|
|
|
|
# must abort with an error.
|
|
|
|
def _resolve_font(self, family: str, style: str):
|
|
|
|
path = os.path.join(self.config.cache_dir, "fonts", family)
|
|
|
|
|
|
|
|
# Fetch font family, if it hasn't been fetched yet
|
|
|
|
if not os.path.isdir(path):
|
|
|
|
self._fetch_font_from_google_fonts(family)
|
|
|
|
|
|
|
|
# Check for availability of font style
|
|
|
|
list = sorted(os.listdir(path))
|
|
|
|
for file in list:
|
|
|
|
name, _ = os.path.splitext(file)
|
|
|
|
if name == style:
|
|
|
|
return os.path.join(path, file)
|
|
|
|
|
|
|
|
# Find regular variant of font family - we cannot rely on the fact that
|
|
|
|
# fonts always have a single regular variant - some of them have several
|
|
|
|
# of them, potentially prefixed with "Condensed" etc. For this reason we
|
|
|
|
# use the first font we find if we find no regular one.
|
|
|
|
fallback = ""
|
|
|
|
for file in list:
|
|
|
|
name, _ = os.path.splitext(file)
|
|
|
|
|
|
|
|
# 1. Fallback: use first font
|
|
|
|
if not fallback:
|
|
|
|
fallback = name
|
|
|
|
|
|
|
|
# 2. Fallback: use regular font - use the shortest one, i.e., prefer
|
|
|
|
# "10pt Regular" over "10pt Condensed Regular". This is a heuristic.
|
|
|
|
if "Regular" in name:
|
|
|
|
if not fallback or len(name) < len(fallback):
|
|
|
|
fallback = name
|
|
|
|
|
|
|
|
# Print warning in debug mode, since the font could not be resolved
|
|
|
|
if self.config.debug:
|
|
|
|
log.warning(
|
|
|
|
f"Couldn't find style '{style}' for font family '{family}'. " +
|
|
|
|
f"Styles available:\n\n" +
|
|
|
|
f"\n".join([os.path.splitext(file)[0] for file in list]) +
|
|
|
|
f"\n\n"
|
|
|
|
f"Falling back to: {fallback}\n"
|
|
|
|
f"\n"
|
|
|
|
)
|
|
|
|
|
|
|
|
# Fall back to regular font (guess if there are multiple)
|
|
|
|
return self._resolve_font(family, fallback)
|
2022-09-13 13:35:14 +03:00
|
|
|
|
2024-03-31 10:23:22 +03:00
|
|
|
# Fetch font family from Google Fonts
|
|
|
|
def _fetch_font_from_google_fonts(self, family: str):
|
|
|
|
path = os.path.join(self.config.cache_dir, "fonts")
|
2022-09-13 13:35:14 +03:00
|
|
|
|
2024-03-31 10:23:22 +03:00
|
|
|
# Download manifest from Google Fonts - Google returns JSON with syntax
|
|
|
|
# errors, so we just treat the response as plain text and parse out all
|
|
|
|
# URLs to font files, as we're going to rename them anyway. This should
|
|
|
|
# be more resilient than trying to correct the JSON syntax.
|
|
|
|
url = f"https://fonts.google.com/download/list?family={family}"
|
|
|
|
res = requests.get(url)
|
|
|
|
|
|
|
|
# Ensure that the download succeeded
|
|
|
|
if res.status_code != 200:
|
|
|
|
raise PluginError(
|
|
|
|
f"Couldn't find font family '{family}' on Google Fonts "
|
|
|
|
f"({res.status_code}: {res.reason})"
|
|
|
|
)
|
2022-09-13 13:35:14 +03:00
|
|
|
|
2024-03-31 10:23:22 +03:00
|
|
|
# Extract font URLs from manifest
|
|
|
|
for match in re.findall(
|
|
|
|
r"\"(https:(?:.*?)\.[ot]tf)\"", str(res.content)
|
|
|
|
):
|
|
|
|
with requests.get(match) as res:
|
|
|
|
res.raise_for_status()
|
|
|
|
|
|
|
|
# Create a temporary file to download the font
|
|
|
|
with NamedTemporaryFile() as temp:
|
|
|
|
temp.write(res.content)
|
|
|
|
temp.flush()
|
|
|
|
|
|
|
|
# Extract font family name and style
|
|
|
|
font = ImageFont.truetype(temp.name)
|
|
|
|
name, style = font.getname()
|
|
|
|
name = " ".join([name.replace(family, ""), style]).strip()
|
|
|
|
|
|
|
|
# Move fonts to cache directory
|
|
|
|
target = os.path.join(path, family, f"{name}.ttf")
|
|
|
|
copy_file(temp.name, target)
|
2022-09-13 13:35:14 +03:00
|
|
|
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
# Data
|
|
|
|
# -----------------------------------------------------------------------------
|
|
|
|
|
2022-09-15 19:51:35 +03:00
|
|
|
# Set up logging
|
|
|
|
log = logging.getLogger("mkdocs")
|
|
|
|
log.addFilter(DuplicateFilter())
|
|
|
|
|
2022-09-13 13:35:14 +03:00
|
|
|
# Color palette
|
2023-12-18 18:08:14 +03:00
|
|
|
colors = {
|
2022-09-13 13:35:14 +03:00
|
|
|
"red": { "fill": "#ef5552", "text": "#ffffff" },
|
|
|
|
"pink": { "fill": "#e92063", "text": "#ffffff" },
|
|
|
|
"purple": { "fill": "#ab47bd", "text": "#ffffff" },
|
|
|
|
"deep-purple": { "fill": "#7e56c2", "text": "#ffffff" },
|
|
|
|
"indigo": { "fill": "#4051b5", "text": "#ffffff" },
|
|
|
|
"blue": { "fill": "#2094f3", "text": "#ffffff" },
|
|
|
|
"light-blue": { "fill": "#02a6f2", "text": "#ffffff" },
|
|
|
|
"cyan": { "fill": "#00bdd6", "text": "#ffffff" },
|
|
|
|
"teal": { "fill": "#009485", "text": "#ffffff" },
|
|
|
|
"green": { "fill": "#4cae4f", "text": "#ffffff" },
|
|
|
|
"light-green": { "fill": "#8bc34b", "text": "#ffffff" },
|
|
|
|
"lime": { "fill": "#cbdc38", "text": "#000000" },
|
|
|
|
"yellow": { "fill": "#ffec3d", "text": "#000000" },
|
|
|
|
"amber": { "fill": "#ffc105", "text": "#000000" },
|
|
|
|
"orange": { "fill": "#ffa724", "text": "#000000" },
|
|
|
|
"deep-orange": { "fill": "#ff6e42", "text": "#ffffff" },
|
|
|
|
"brown": { "fill": "#795649", "text": "#ffffff" },
|
|
|
|
"grey": { "fill": "#757575", "text": "#ffffff" },
|
|
|
|
"blue-grey": { "fill": "#546d78", "text": "#ffffff" },
|
|
|
|
"black": { "fill": "#000000", "text": "#ffffff" },
|
|
|
|
"white": { "fill": "#ffffff", "text": "#000000" }
|
2023-12-18 18:08:14 +03:00
|
|
|
}
|