MkDocs upgrade: use attribute-based access

This commit is contained in:
Oleh Prypin 2022-09-16 20:03:07 +02:00 committed by Martin Donath
parent b6d2eb19fd
commit 02bcffa6a8
4 changed files with 42 additions and 42 deletions

View File

@ -63,7 +63,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve configuration # Retrieve configuration
def on_config(self, config): def on_config(self, config):
self.color = colors.get("indigo") self.color = colors.get("indigo")
if not self.config["cards"]: if not self.config.cards:
return return
# Check if required dependencies are installed # Check if required dependencies are installed
@ -75,12 +75,12 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
sys.exit() sys.exit()
# Ensure presence of cache directory # Ensure presence of cache directory
self.cache = self.config["cache_dir"] self.cache = self.config.cache_dir
if not os.path.isdir(self.cache): if not os.path.isdir(self.cache):
os.makedirs(self.cache) os.makedirs(self.cache)
# Retrieve palette from theme configuration # Retrieve palette from theme configuration
theme = config["theme"] theme = config.theme
if "palette" in theme: if "palette" in theme:
palette = theme["palette"] palette = theme["palette"]
@ -94,7 +94,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
self.color = colors.get(primary, self.color) self.color = colors.get(primary, self.color)
# Retrieve color overrides # Retrieve color overrides
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.logo = self._load_logo(config)
@ -102,16 +102,16 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Create social cards # Create social cards
def on_page_markdown(self, markdown, page, config, files): def on_page_markdown(self, markdown, page, config, files):
if not self.config["cards"]: if not self.config.cards:
return return
# Resolve image directory # Resolve image directory
directory = self.config["cards_dir"] directory = self.config.cards_dir
file, _ = os.path.splitext(page.file.src_path) file, _ = os.path.splitext(page.file.src_path)
# Resolve path of image # Resolve path of image
path = "{}.png".format(os.path.join( path = "{}.png".format(os.path.join(
config["site_dir"], config.site_dir,
directory, directory,
file file
)) ))
@ -122,11 +122,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
os.makedirs(directory) os.makedirs(directory)
# Compute site name # Compute site name
site_name = config.get("site_name") site_name = config.site_name
# Compute page title and description # Compute page title and description
title = page.meta.get("title", page.title) title = page.meta.get("title", page.title)
description = config.get("site_description") or "" description = config.site_description or ""
if "description" in page.meta: if "description" in page.meta:
description = page.meta["description"] description = page.meta["description"]
@ -244,22 +244,22 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Generate meta tags # Generate meta tags
def _generate_meta(self, page, config): def _generate_meta(self, page, config):
directory = self.config["cards_dir"] directory = self.config.cards_dir
file, _ = os.path.splitext(page.file.src_path) file, _ = os.path.splitext(page.file.src_path)
# Compute page title # Compute page title
title = page.meta.get("title", page.title) title = page.meta.get("title", page.title)
if not page.is_homepage: if not page.is_homepage:
title = f"{title} - {config.get('site_name')}" title = f"{title} - {config.site_name}"
# Compute page description # Compute page description
description = config.get("site_description") description = config.site_description
if "description" in page.meta: if "description" in page.meta:
description = page.meta["description"] description = page.meta["description"]
# Resolve image URL # Resolve image URL
url = "{}.png".format(os.path.join( url = "{}.png".format(os.path.join(
config.get("site_url"), config.site_url,
directory, directory,
file file
)) ))
@ -291,14 +291,14 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve logo image or icon # Retrieve logo image or icon
def _load_logo(self, config): def _load_logo(self, config):
theme = config.get("theme") theme = config.theme
# Handle images (precedence over icons) # Handle images (precedence over icons)
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 # Load SVG and convert to PNG
path = os.path.join(config["docs_dir"], theme["logo"]) path = os.path.join(config.docs_dir, theme["logo"])
if extension == ".svg": if extension == ".svg":
return self._load_logo_svg(path) return self._load_logo_svg(path)
@ -336,11 +336,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve font # Retrieve font
def _load_font(self, config): def _load_font(self, config):
name = self.config.get("cards_font") name = self.config.cards_font
if not name: if not name:
# Retrieve from theme (default: Roboto) # Retrieve from theme (default: Roboto)
theme = config["theme"] theme = config.theme
if theme["font"]: if theme["font"]:
name = theme["font"]["text"] name = theme["font"]["text"]
else: else:

View File

@ -48,12 +48,12 @@ class TagsPlugin(BasePlugin[TagsPluginConfig]):
self.tags_file = None self.tags_file = None
# Retrieve tags mapping from configuration # Retrieve tags mapping from configuration
self.tags_map = config["extra"].get("tags") self.tags_map = config.extra.get("tags")
# Use override of slugify function # Use override of slugify function
toc = { "slugify": slugify, "separator": "-" } toc = { "slugify": slugify, "separator": "-" }
if "toc" in config["mdx_configs"]: if "toc" in config.mdx_configs:
toc = { **toc, **config["mdx_configs"]["toc"] } toc = { **toc, **config.mdx_configs["toc"] }
# Partially apply slugify function # Partially apply slugify function
self.slugify = lambda value: ( self.slugify = lambda value: (
@ -62,7 +62,7 @@ class TagsPlugin(BasePlugin[TagsPluginConfig]):
# Hack: 2nd pass for tags index page(s) # Hack: 2nd pass for tags index page(s)
def on_nav(self, nav, config, files): def on_nav(self, nav, config, files):
file = self.config.get("tags_file") file = self.config.tags_file
if file: if file:
self.tags_file = self._get_tags_file(files, file) self.tags_file = self._get_tags_file(files, file)

View File

@ -63,7 +63,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve configuration # Retrieve configuration
def on_config(self, config): def on_config(self, config):
self.color = colors.get("indigo") self.color = colors.get("indigo")
if not self.config["cards"]: if not self.config.cards:
return return
# Check if required dependencies are installed # Check if required dependencies are installed
@ -75,12 +75,12 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
sys.exit() sys.exit()
# Ensure presence of cache directory # Ensure presence of cache directory
self.cache = self.config["cache_dir"] self.cache = self.config.cache_dir
if not os.path.isdir(self.cache): if not os.path.isdir(self.cache):
os.makedirs(self.cache) os.makedirs(self.cache)
# Retrieve palette from theme configuration # Retrieve palette from theme configuration
theme = config["theme"] theme = config.theme
if "palette" in theme: if "palette" in theme:
palette = theme["palette"] palette = theme["palette"]
@ -94,7 +94,7 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
self.color = colors.get(primary, self.color) self.color = colors.get(primary, self.color)
# Retrieve color overrides # Retrieve color overrides
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.logo = self._load_logo(config)
@ -102,16 +102,16 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Create social cards # Create social cards
def on_page_markdown(self, markdown, page, config, files): def on_page_markdown(self, markdown, page, config, files):
if not self.config["cards"]: if not self.config.cards:
return return
# Resolve image directory # Resolve image directory
directory = self.config["cards_dir"] directory = self.config.cards_dir
file, _ = os.path.splitext(page.file.src_path) file, _ = os.path.splitext(page.file.src_path)
# Resolve path of image # Resolve path of image
path = "{}.png".format(os.path.join( path = "{}.png".format(os.path.join(
config["site_dir"], config.site_dir,
directory, directory,
file file
)) ))
@ -122,11 +122,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
os.makedirs(directory) os.makedirs(directory)
# Compute site name # Compute site name
site_name = config.get("site_name") site_name = config.site_name
# Compute page title and description # Compute page title and description
title = page.meta.get("title", page.title) title = page.meta.get("title", page.title)
description = config.get("site_description") or "" description = config.site_description or ""
if "description" in page.meta: if "description" in page.meta:
description = page.meta["description"] description = page.meta["description"]
@ -244,22 +244,22 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Generate meta tags # Generate meta tags
def _generate_meta(self, page, config): def _generate_meta(self, page, config):
directory = self.config["cards_dir"] directory = self.config.cards_dir
file, _ = os.path.splitext(page.file.src_path) file, _ = os.path.splitext(page.file.src_path)
# Compute page title # Compute page title
title = page.meta.get("title", page.title) title = page.meta.get("title", page.title)
if not page.is_homepage: if not page.is_homepage:
title = f"{title} - {config.get('site_name')}" title = f"{title} - {config.site_name}"
# Compute page description # Compute page description
description = config.get("site_description") description = config.site_description
if "description" in page.meta: if "description" in page.meta:
description = page.meta["description"] description = page.meta["description"]
# Resolve image URL # Resolve image URL
url = "{}.png".format(os.path.join( url = "{}.png".format(os.path.join(
config.get("site_url"), config.site_url,
directory, directory,
file file
)) ))
@ -291,14 +291,14 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve logo image or icon # Retrieve logo image or icon
def _load_logo(self, config): def _load_logo(self, config):
theme = config.get("theme") theme = config.theme
# Handle images (precedence over icons) # Handle images (precedence over icons)
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 # Load SVG and convert to PNG
path = os.path.join(config["docs_dir"], theme["logo"]) path = os.path.join(config.docs_dir, theme["logo"])
if extension == ".svg": if extension == ".svg":
return self._load_logo_svg(path) return self._load_logo_svg(path)
@ -336,11 +336,11 @@ class SocialPlugin(BasePlugin[SocialPluginConfig]):
# Retrieve font # Retrieve font
def _load_font(self, config): def _load_font(self, config):
name = self.config.get("cards_font") name = self.config.cards_font
if not name: if not name:
# Retrieve from theme (default: Roboto) # Retrieve from theme (default: Roboto)
theme = config["theme"] theme = config.theme
if theme["font"]: if theme["font"]:
name = theme["font"]["text"] name = theme["font"]["text"]
else: else:

View File

@ -48,12 +48,12 @@ class TagsPlugin(BasePlugin[TagsPluginConfig]):
self.tags_file = None self.tags_file = None
# Retrieve tags mapping from configuration # Retrieve tags mapping from configuration
self.tags_map = config["extra"].get("tags") self.tags_map = config.extra.get("tags")
# Use override of slugify function # Use override of slugify function
toc = { "slugify": slugify, "separator": "-" } toc = { "slugify": slugify, "separator": "-" }
if "toc" in config["mdx_configs"]: if "toc" in config.mdx_configs:
toc = { **toc, **config["mdx_configs"]["toc"] } toc = { **toc, **config.mdx_configs["toc"] }
# Partially apply slugify function # Partially apply slugify function
self.slugify = lambda value: ( self.slugify = lambda value: (
@ -62,7 +62,7 @@ class TagsPlugin(BasePlugin[TagsPluginConfig]):
# Hack: 2nd pass for tags index page(s) # Hack: 2nd pass for tags index page(s)
def on_nav(self, nav, config, files): def on_nav(self, nav, config, files):
file = self.config.get("tags_file") file = self.config.tags_file
if file: if file:
self.tags_file = self._get_tags_file(files, file) self.tags_file = self._get_tags_file(files, file)