File size: 12,024 Bytes
c3b2812
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Copyright (c) 2016-2025 Martin Donath <martin.donath@squidfunk.com>

# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:

# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.

# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.

from __future__ import annotations

import json
import os
import posixpath

from jinja2 import pass_context
from jinja2.runtime import Context
from mkdocs.config.defaults import MkDocsConfig
from mkdocs.exceptions import PluginError
from mkdocs import utils
from mkdocs.plugins import BasePlugin, event_priority
from mkdocs.structure import StructureItem
from mkdocs.structure.files import Files
from mkdocs.structure.nav import Link, Section
from mkdocs.utils import get_theme_dir
from urllib.parse import ParseResult as URL, urlparse

from .builder import ProjectsBuilder
from .config import ProjectsConfig
from .structure import Project, ProjectLink

# -----------------------------------------------------------------------------
# Classes
# -----------------------------------------------------------------------------

# Projects plugin
class ProjectsPlugin(BasePlugin[ProjectsConfig]):

    # Projects builder
    builder: ProjectsBuilder = None

    # Initialize plugin
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        # Initialize incremental builds
        self.is_serve = False
        self.is_dirty = False

        # Hack: Since we're building in topological order, we cannot let MkDocs
        # clean the directory, because it means that nested projects are always
        # deleted before a project is built. We also don't need to restore this
        # functionality, because it's only used once in the process.
        utils.clean_directory = lambda _: _

    # Determine whether we're serving the site
    def on_startup(self, *, command, dirty):
        self.is_serve = command == "serve"
        self.is_dirty = dirty

    # Resolve projects – compared to our other concurrent plugins, this plugin
    # is forced to use a process pool in order to guarantee proper isolation, as
    # MkDocs itself is not thread-safe. Additionally, all project configurations
    # are resolved and written to the cache (if enabled), as it's sufficient to
    # resolve them once on the top-level before projects are built. We might
    # need adjacent project configurations for interlinking projects.
    def on_config(self, config):
        if not self.config.enabled:
            return

        # Skip if projects should not be built - we can only exit here if we're
        # at the top-level, but not when building a nested project
        root = self.config.projects_root_dir is None
        if root and not self.config.projects:
            return

        # Set projects root directory to the top-level project
        if not self.config.projects_root_dir:
            self.config.projects_root_dir = os.path.dirname(
                config.config_file_path
            )

        # Initialize manifest
        self.manifest: dict[str, str] = {}
        self.manifest_file = os.path.join(
            self.config.projects_root_dir,
            self.config.cache_dir,
            "manifest.json"
        )

        # Load manifest if it exists and the cache should be used
        if os.path.isfile(self.manifest_file):
            try:
                with open(self.manifest_file) as f:
                    self.manifest = json.load(f)
            except:
                pass

        # Building the top-level project, we must resolve and load all project
        # configurations, as we need all information upfront to build them in
        # the correct order, and to resolve links between projects. Furthermore,
        # the author might influence a project's path by setting the site URL.
        if root:
            if not self.builder:
                self.builder = ProjectsBuilder(config, self.config)

            # @todo: detach project resolution from build
            self.manifest = { ".": os.path.relpath(config.config_file_path) }
            for job in self.builder.root.jobs():
                path = os.path.relpath(job.project.config.config_file_path)
                self.manifest[job.project.slug] = path

            # Save manifest, a we need it in nested projects
            os.makedirs(os.path.dirname(self.manifest_file), exist_ok = True)
            with open(self.manifest_file, "w") as f:
                f.write(json.dumps(self.manifest, indent = 2, sort_keys = True))

    # Schedule projects for building - the general case is that all projects
    # can be considered independent of each other, so we build them in parallel
    def on_pre_build(self, config):
        if not self.config.enabled:
            return

        # Skip if projects should not be built or we're not at the top-level
        if not self.config.projects or not self.builder:
            return

        # Build projects
        self.builder.build(self.is_serve, self.is_dirty)

    # Patch environment to allow for hoisting of media files provided by the
    # theme itself, which will also work for other themes, not only this one
    def on_env(self, env, *, config, files):
        if not self.config.enabled:
            return

        # Skip if projects should not be built or we're at the top-level
        if not self.config.projects or self.builder:
            return

        # If hoisting is enabled and we're building a project, remove all media
        # files that are provided by the theme and hoist them to the top
        if self.config.hoisting:
            theme = get_theme_dir(config.theme.name)
            hoist = Files([])

            # Retrieve top-level project and check if the current project uses
            # the same theme as the top-level project - if not, don't hoist
            root = Project("mkdocs.yml", self.config)
            if config.theme.name != root.config.theme["name"]:
                return

            # Remove all media files that are provided by the theme
            for file in files.media_files():
                if file.abs_src_path.startswith(theme):
                    files.remove(file)
                    hoist.append(file)

            # Resolve source and target project
            source: Project | None = None
            target: Project | None = None
            for ref, file in self.manifest.items():
                base = os.path.join(self.config.projects_root_dir, file)
                if file == os.path.relpath(
                    config.config_file_path, self.config.projects_root_dir
                ):
                    source = Project(base, self.config, ref)
                if "." == ref:
                    target = Project(base, self.config, ref)

            # Compute path for slug from source and target project
            path = target.path(source)

            # Fetch URL template filter from environment - the filter might
            # be overridden by other plugins, so we must retrieve and wrap it
            url_filter = env.filters["url"]

            # Patch URL template filter to add support for correctly resolving
            # media files that were hoisted to the top-level project
            @pass_context
            def url_filter_with_hoisting(context: Context, url: str | None):
                if url and hoist.get_file_from_path(url):
                    return posixpath.join(path, url_filter(context, url))
                else:
                    return url_filter(context, url)

            # Register custom template filters
            env.filters["url"] = url_filter_with_hoisting

    # Adjust project navigation in page (run latest) - as always, allow
    # other plugins to alter the navigation before we process it here
    @event_priority(-100)
    def on_page_context(self, context, *, page, config, nav):
        if not self.config.enabled:
            return

        # Skip if projects should not be built
        if not self.config.projects:
            return

        # Replace project URLs in navigation
        self._replace(nav.items, config)

    # Adjust project navigation in template (run latest) - as always, allow
    # other plugins to alter the navigation before we process it here
    @event_priority(-100)
    def on_template_context(self, context, *, template_name, config):
        if not self.config.enabled:
            return

        # Skip if projects should not be built
        if not self.config.projects:
            return

        # Replace project URLs in navigation
        self._replace(context["nav"].items, config)

    # Serve projects
    def on_serve(self, server, *, config, builder):
        if self.config.enabled:
            self.builder.serve(server, self.is_dirty)

    # -------------------------------------------------------------------------

    # Replace project links in the given list of navigation items
    def _replace(self, items: list[StructureItem], config: MkDocsConfig):
        for index, item in enumerate(items):

            # Handle section
            if isinstance(item, Section):
                self._replace(item.children, config)

            # Handle link
            if isinstance(item, Link):
                url = urlparse(item.url)
                if url.scheme == "project":
                    project, url = self._resolve_project_url(url, config)

                    # Append file name if directory URLs are disabled
                    if not project.config.use_directory_urls:
                        url += "index.html"

                    # Replace link with project link
                    items[index] = ProjectLink(
                        item.title or project.config.site_name,
                        url
                    )

    # Resolve project URL and slug
    def _resolve_project_url(self, url: URL, config: MkDocsConfig):

        # Abort if the project URL contains a path, as we first need to collect
        # use cases for when, how and whether we need and want to support this
        if url.path != "":
            raise PluginError(
                f"Couldn't resolve project URL: paths currently not supported\n"
                f"Please only use 'project://{url.hostname}'"
            )

        # Compute slug from host name and convert to dot notation
        slug = url.hostname
        slug = slug if slug.startswith(".") else f".{slug}"

        # Resolve source and target project
        source: Project | None = None
        target: Project | None = None
        for ref, file in self.manifest.items():
            base = os.path.join(self.config.projects_root_dir, file)
            if file == os.path.relpath(
                config.config_file_path, self.config.projects_root_dir
            ):
                source = Project(base, self.config, ref)
            if slug == ref:
                target = Project(base, self.config, ref)

        # Abort if slug doesn't match a known project
        if not target:
            raise PluginError(f"Couldn't find project '{slug}'")

        # Return project slug and path
        return target, target.path(source)