Skip to content

Commit 819e209

Browse files
kamilkrzyskowsquidfunk
authored andcommitted
Added projects plugin handling in the info plugin
1 parent 79129d5 commit 819e209

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

material/plugins/info/plugin.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919
# IN THE SOFTWARE.
2020

21+
import glob
2122
import json
2223
import logging
2324
import os
@@ -30,6 +31,7 @@
3031
from importlib.metadata import distributions, version
3132
from io import BytesIO
3233
from markdown.extensions.toc import slugify
34+
from mkdocs.config.defaults import MkDocsConfig
3335
from mkdocs.plugins import BasePlugin, event_priority
3436
from mkdocs.utils import get_theme_dir
3537
import regex
@@ -107,6 +109,27 @@ def on_config(self, config):
107109
log.error("Please remove 'hooks' setting.")
108110
self._help_on_customizations_and_exit()
109111

112+
# Assure that config_file_path is absolute.
113+
# If the --config-file option is used then the path is
114+
# used as provided, so it is likely relative.
115+
if not os.path.isabs(config.config_file_path):
116+
config.config_file_path = os.path.normpath(os.path.join(
117+
os.getcwd(),
118+
config.config_file_path
119+
))
120+
121+
# Support projects plugin
122+
projects_plugin = config.plugins.get("material/projects")
123+
if projects_plugin:
124+
abs_projects_dir = os.path.normpath(
125+
os.path.join(
126+
os.path.dirname(config.config_file_path),
127+
projects_plugin.config.projects_dir
128+
)
129+
)
130+
else:
131+
abs_projects_dir = ""
132+
110133
# Create in-memory archive and prompt author for a short descriptive
111134
# name for the archive, which is also used as the directory name. Note
112135
# that the name is slugified for better readability and stripped of any
@@ -128,6 +151,18 @@ def on_config(self, config):
128151
if path.startswith(os.getcwd()):
129152
self.exclusion_patterns.append(_resolve_pattern(path))
130153

154+
# Exclude site_dir for projects
155+
if projects_plugin:
156+
for path in glob.iglob(
157+
pathname = projects_plugin.config.projects_config_files,
158+
root_dir = abs_projects_dir,
159+
recursive = True
160+
):
161+
current_config_file = os.path.join(abs_projects_dir, path)
162+
project_config = _get_project_config(current_config_file)
163+
pattern = _resolve_pattern(project_config.site_dir)
164+
self.exclusion_patterns.append(pattern)
165+
131166
# Create self-contained example from project
132167
files: list[str] = []
133168
with ZipFile(archive, "a", ZIP_DEFLATED, False) as f:
@@ -311,6 +346,17 @@ def _resolve_pattern(abspath: str):
311346

312347
return path
313348

349+
# Get project configuration
350+
def _get_project_config(project_config_file: str):
351+
with open(project_config_file, encoding="utf-8") as file:
352+
config = MkDocsConfig(config_file_path = project_config_file)
353+
config.load_file(file)
354+
355+
# MkDocs transforms site_dir to absolute path during validation
356+
config.validate()
357+
358+
return config
359+
314360
# -----------------------------------------------------------------------------
315361
# Data
316362
# -----------------------------------------------------------------------------

src/plugins/info/plugin.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
1919
# IN THE SOFTWARE.
2020

21+
import glob
2122
import json
2223
import logging
2324
import os
@@ -30,6 +31,7 @@
3031
from importlib.metadata import distributions, version
3132
from io import BytesIO
3233
from markdown.extensions.toc import slugify
34+
from mkdocs.config.defaults import MkDocsConfig
3335
from mkdocs.plugins import BasePlugin, event_priority
3436
from mkdocs.utils import get_theme_dir
3537
import regex
@@ -107,6 +109,27 @@ def on_config(self, config):
107109
log.error("Please remove 'hooks' setting.")
108110
self._help_on_customizations_and_exit()
109111

112+
# Assure that config_file_path is absolute.
113+
# If the --config-file option is used then the path is
114+
# used as provided, so it is likely relative.
115+
if not os.path.isabs(config.config_file_path):
116+
config.config_file_path = os.path.normpath(os.path.join(
117+
os.getcwd(),
118+
config.config_file_path
119+
))
120+
121+
# Support projects plugin
122+
projects_plugin = config.plugins.get("material/projects")
123+
if projects_plugin:
124+
abs_projects_dir = os.path.normpath(
125+
os.path.join(
126+
os.path.dirname(config.config_file_path),
127+
projects_plugin.config.projects_dir
128+
)
129+
)
130+
else:
131+
abs_projects_dir = ""
132+
110133
# Create in-memory archive and prompt author for a short descriptive
111134
# name for the archive, which is also used as the directory name. Note
112135
# that the name is slugified for better readability and stripped of any
@@ -128,6 +151,18 @@ def on_config(self, config):
128151
if path.startswith(os.getcwd()):
129152
self.exclusion_patterns.append(_resolve_pattern(path))
130153

154+
# Exclude site_dir for projects
155+
if projects_plugin:
156+
for path in glob.iglob(
157+
pathname = projects_plugin.config.projects_config_files,
158+
root_dir = abs_projects_dir,
159+
recursive = True
160+
):
161+
current_config_file = os.path.join(abs_projects_dir, path)
162+
project_config = _get_project_config(current_config_file)
163+
pattern = _resolve_pattern(project_config.site_dir)
164+
self.exclusion_patterns.append(pattern)
165+
131166
# Create self-contained example from project
132167
files: list[str] = []
133168
with ZipFile(archive, "a", ZIP_DEFLATED, False) as f:
@@ -311,6 +346,17 @@ def _resolve_pattern(abspath: str):
311346

312347
return path
313348

349+
# Get project configuration
350+
def _get_project_config(project_config_file: str):
351+
with open(project_config_file, encoding="utf-8") as file:
352+
config = MkDocsConfig(config_file_path = project_config_file)
353+
config.load_file(file)
354+
355+
# MkDocs transforms site_dir to absolute path during validation
356+
config.validate()
357+
358+
return config
359+
314360
# -----------------------------------------------------------------------------
315361
# Data
316362
# -----------------------------------------------------------------------------

0 commit comments

Comments
 (0)