-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refacto(material_integrations): use POO to manage integrations with M…
- Loading branch information
Showing
7 changed files
with
368 additions
and
80 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
#! python3 # noqa: E265 | ||
|
||
# ############################################################################ | ||
# ########## Libraries ############# | ||
# ################################## | ||
|
||
# standard library | ||
from typing import Optional | ||
|
||
# 3rd party | ||
from mkdocs.config.defaults import MkDocsConfig | ||
from mkdocs.plugins import get_plugin_logger | ||
|
||
# package | ||
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME | ||
|
||
# conditional | ||
try: | ||
from material import __version__ as material_version | ||
from material.plugins.blog.plugin import BlogPlugin | ||
from pymdownx.slugs import slugify | ||
|
||
except ImportError: | ||
material_version = None | ||
|
||
|
||
# ############################################################################ | ||
# ########## Globals ############# | ||
# ################################ | ||
|
||
logger = get_plugin_logger(MKDOCS_LOGGER_NAME) | ||
|
||
# ############################################################################ | ||
# ########## Logic ############### | ||
# ################################ | ||
|
||
|
||
class IntegrationMaterialThemeBase: | ||
# attributes | ||
IS_THEME_MATERIAL: bool = False | ||
IS_INSIDERS: bool = False | ||
|
||
def __init__(self, mkdocs_config: MkDocsConfig) -> None: | ||
"""Integration instantiation. | ||
Args: | ||
mkdocs_config (MkDocsConfig): Mkdocs website configuration object. | ||
""" | ||
# store Mkdocs config as attribute | ||
self.mkdocs_config = mkdocs_config | ||
|
||
self.IS_THEME_MATERIAL = self.is_mkdocs_theme_material() | ||
self.IS_INSIDERS = self.is_mkdocs_theme_material_insiders() | ||
|
||
def is_mkdocs_theme_material( | ||
self, mkdocs_config: Optional[MkDocsConfig] = None | ||
) -> bool: | ||
"""Check if the theme set in mkdocs.yml is material or not. | ||
Args: | ||
mkdocs_config (Optional[MkDocsConfig]): Mkdocs website configuration object. | ||
Returns: | ||
bool: True if the theme's name is 'material'. False if not. | ||
""" | ||
if mkdocs_config is None and isinstance(self.mkdocs_config, MkDocsConfig): | ||
mkdocs_config: MkDocsConfig = self.mkdocs_config | ||
|
||
self.IS_THEME_MATERIAL = mkdocs_config.theme.name == "material" | ||
return self.IS_THEME_MATERIAL | ||
|
||
def is_mkdocs_theme_material_insiders(self) -> Optional[bool]: | ||
"""Check if the material theme is community or insiders edition. | ||
Returns: | ||
bool: True if the theme is Insiders edition. False if community. None if | ||
the Material theme is not installed. | ||
""" | ||
if not self.IS_THEME_MATERIAL: | ||
return None | ||
|
||
if material_version is not None and "insiders" in material_version: | ||
logger.debug("Material theme edition INSIDERS") | ||
self.IS_INSIDERS = True | ||
return True | ||
else: | ||
logger.debug("Material theme edition COMMUNITY") | ||
self.IS_INSIDERS = False | ||
return False |
103 changes: 103 additions & 0 deletions
103
mkdocs_rss_plugin/integrations/theme_material_blog_plugin.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#! python3 # noqa: E265 | ||
|
||
# ############################################################################ | ||
# ########## Libraries ############# | ||
# ################################## | ||
|
||
# standard library | ||
from typing import Optional | ||
|
||
# 3rd party | ||
from mkdocs.config.defaults import MkDocsConfig | ||
from mkdocs.plugins import get_plugin_logger | ||
|
||
# package | ||
from mkdocs_rss_plugin.constants import MKDOCS_LOGGER_NAME | ||
from mkdocs_rss_plugin.integrations.theme_material_base import ( | ||
IntegrationMaterialThemeBase, | ||
) | ||
|
||
# conditional | ||
try: | ||
from material import __version__ as material_version | ||
from material.plugins.blog.plugin import BlogPlugin | ||
|
||
except ImportError: | ||
material_version = None | ||
|
||
|
||
# ############################################################################ | ||
# ########## Globals ############# | ||
# ################################ | ||
|
||
logger = get_plugin_logger(MKDOCS_LOGGER_NAME) | ||
|
||
# ############################################################################ | ||
# ########## Logic ############### | ||
# ################################ | ||
|
||
|
||
class IntegrationMaterialBlog(IntegrationMaterialThemeBase): | ||
# attributes | ||
IS_ENABLED: bool = True | ||
IS_BLOG_PLUGIN_ENABLED: bool = True | ||
|
||
def __init__(self, mkdocs_config: MkDocsConfig, switch_force: bool = True) -> None: | ||
"""Integration instantiation. | ||
Args: | ||
mkdocs_config (MkDocsConfig): Mkdocs website configuration object. | ||
switch_force (bool, optional): option to force integration disabling. Set | ||
it to False to disable it even if Social Cards are enabled in Mkdocs | ||
configuration. Defaults to True. | ||
""" | ||
# check if the integration can be enabled or not | ||
self.IS_BLOG_PLUGIN_ENABLED = self.is_blog_plugin_enabled_mkdocs( | ||
mkdocs_config=mkdocs_config | ||
) | ||
# if every conditions are True, enable the integration | ||
self.IS_ENABLED = all([self.IS_THEME_MATERIAL, self.IS_BLOG_PLUGIN_ENABLED]) | ||
|
||
# except if the end-user wants to disable it | ||
if switch_force is False: | ||
self.IS_ENABLED = False | ||
logger.debug( | ||
"Integration with Blog (Material theme) is " | ||
"disabled in plugin's option in Mkdocs configuration." | ||
) | ||
|
||
def is_blog_plugin_enabled_mkdocs( | ||
self, mkdocs_config: Optional[MkDocsConfig] | ||
) -> bool: | ||
"""Check if blog plugin is installed and enabled. | ||
Args: | ||
mkdocs_config (Optional[MkDocsConfig]): Mkdocs website configuration object. | ||
Returns: | ||
bool: True if the theme material and the plugin blog is enabled. | ||
""" | ||
if mkdocs_config is None and isinstance(self.mkdocs_config, MkDocsConfig): | ||
mkdocs_config = self.mkdocs_config | ||
|
||
if not self.is_mkdocs_theme_material(mkdocs_config=mkdocs_config): | ||
logger.debug("Installed theme is not 'material'. Integration disabled.") | ||
return False | ||
|
||
if not mkdocs_config.plugins.get("material/blog"): | ||
logger.debug("Material blog plugin is not listed in configuration.") | ||
self.IS_BLOG_PLUGIN_ENABLED = False | ||
return False | ||
|
||
self.blog_plugin_cfg: BlogPlugin | None = mkdocs_config.plugins.get( | ||
"material/blog" | ||
) | ||
|
||
if not self.blog_plugin_cfg.config.enabled: | ||
logger.debug("Material blog plugin is installed but disabled.") | ||
self.IS_BLOG_PLUGIN_ENABLED = False | ||
return False | ||
|
||
logger.debug("Material blog plugin is enabled in Mkdocs configuration.") | ||
self.IS_BLOG_PLUGIN_ENABLED = True | ||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
site_name: Test RSS Plugin | ||
site_description: Test RSS with blog plugin also enabled | ||
site_url: https://guts.github.io/mkdocs-rss-plugin | ||
|
||
plugins: | ||
- blog: | ||
blog_dir: blog | ||
- rss | ||
|
||
theme: | ||
name: material |
Oops, something went wrong.