18
18
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
19
# IN THE SOFTWARE.
20
20
21
+ import glob
21
22
import json
22
23
import logging
23
24
import os
30
31
from importlib .metadata import distributions , version
31
32
from io import BytesIO
32
33
from markdown .extensions .toc import slugify
34
+ from mkdocs .config .defaults import MkDocsConfig
33
35
from mkdocs .plugins import BasePlugin , event_priority
34
36
from mkdocs .utils import get_theme_dir
35
37
import regex
@@ -107,6 +109,27 @@ def on_config(self, config):
107
109
log .error ("Please remove 'hooks' setting." )
108
110
self ._help_on_customizations_and_exit ()
109
111
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
+
110
133
# Create in-memory archive and prompt author for a short descriptive
111
134
# name for the archive, which is also used as the directory name. Note
112
135
# that the name is slugified for better readability and stripped of any
@@ -128,6 +151,18 @@ def on_config(self, config):
128
151
if path .startswith (os .getcwd ()):
129
152
self .exclusion_patterns .append (_resolve_pattern (path ))
130
153
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
+
131
166
# Create self-contained example from project
132
167
files : list [str ] = []
133
168
with ZipFile (archive , "a" , ZIP_DEFLATED , False ) as f :
@@ -311,6 +346,17 @@ def _resolve_pattern(abspath: str):
311
346
312
347
return path
313
348
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
+
314
360
# -----------------------------------------------------------------------------
315
361
# Data
316
362
# -----------------------------------------------------------------------------
0 commit comments