-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy paththeme_material_blog_plugin.py
More file actions
159 lines (130 loc) · 5.83 KB
/
theme_material_blog_plugin.py
File metadata and controls
159 lines (130 loc) · 5.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#! python3 # noqa: E265
# ############################################################################
# ########## Libraries #############
# ##################################
# standard library
from functools import lru_cache
from pathlib import Path
from typing import Union
# 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,
)
from mkdocs_rss_plugin.models import MkdocsPageSubset
# conditional
try:
from material import __version__ as material_version
from material.plugins.blog.plugin import BlogPlugin
from material.plugins.blog.structure import Post
except ImportError:
material_version = BlogPlugin = Post = 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: MkDocsConfig | None) -> 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 mkdocs_config.plugins.get(f"{self.THEME_NAME}/blog") is None:
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(
f"{self.THEME_NAME}/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
@lru_cache
def author_name_from_id(self, author_id: str) -> str:
"""Return author name from author_id used in Material blog plugin (.authors.yml).
Args:
author_id (str): author key in .authors.yml
Returns:
str: author name or passed author_id if not found within .authors.yml
"""
if (
self.blog_plugin_cfg.config.authors
and isinstance(self.blog_plugin_cfg, BlogPlugin)
and hasattr(self.blog_plugin_cfg, "authors")
and isinstance(self.blog_plugin_cfg.authors, dict)
):
if author_id in self.blog_plugin_cfg.authors:
author_metadata = self.blog_plugin_cfg.authors.get(author_id)
if "email" in self.blog_plugin_cfg.authors.get(author_id):
return f"{author_metadata.get('email')} ({author_metadata.get('name')})"
else:
return author_metadata.get("name")
else:
logger.error(
f"Author ID '{author_id}' is not part of known authors: "
f"{self.blog_plugin_cfg.authors}. Returning author_id."
)
return author_id
def is_page_a_blog_post(self, mkdocs_page: Union["Post", MkdocsPageSubset]) -> bool:
"""Identifies if the given page is part of Material Blog.
Args:
mkdocs_page: page to identify
Returns:
True if the given page is a Material Blog post.
"""
if self.IS_ENABLED and Post is not None and isinstance(mkdocs_page, Post):
logger.debug(
f"page '{mkdocs_page.file.src_uri}' identified as Material Blog post."
)
return True
elif isinstance(mkdocs_page, MkdocsPageSubset) and Path(
mkdocs_page.src_uri
).is_relative_to(self.blog_plugin_cfg.config.blog_dir):
logger.debug(
f"page '{mkdocs_page.src_uri}' identified as Material Blog post "
f"by src_uri matching."
)
return True
else:
return False