|
| 1 | +# Copyright (c) 2016-2023 Martin Donath <[email protected]> |
| 2 | + |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to |
| 5 | +# deal in the Software without restriction, including without limitation the |
| 6 | +# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 7 | +# sell copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | + |
| 10 | +# The above copyright notice and this permission notice shall be included in |
| 11 | +# all copies or substantial portions of the Software. |
| 12 | + |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 18 | +# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 19 | +# IN THE SOFTWARE. |
| 20 | + |
| 21 | +import logging |
| 22 | +import os |
| 23 | + |
| 24 | +from glob import iglob |
| 25 | +from mkdocs.config.defaults import MkDocsConfig |
| 26 | +from pathspec.gitignore import GitIgnoreSpec |
| 27 | +from zipfile import ZipFile, ZIP_DEFLATED |
| 28 | + |
| 29 | +# ----------------------------------------------------------------------------- |
| 30 | +# Hooks |
| 31 | +# ----------------------------------------------------------------------------- |
| 32 | + |
| 33 | +# Create archives for all examples |
| 34 | +def on_post_build(config: MkDocsConfig): |
| 35 | + |
| 36 | + # Read files to ignore from .gitignore |
| 37 | + with open(".gitignore") as f: |
| 38 | + spec = GitIgnoreSpec.from_lines([ |
| 39 | + line for line in f.read().split("\n") |
| 40 | + if line and not line.startswith("#") |
| 41 | + ]) |
| 42 | + |
| 43 | + # Create archives for each example |
| 44 | + for file in iglob("examples/*/mkdocs.yml", recursive = True): |
| 45 | + base = os.path.dirname(file) |
| 46 | + |
| 47 | + # Compute archive name and path |
| 48 | + example = os.path.basename(base) |
| 49 | + archive = os.path.join(config.site_dir, f"{example}.zip") |
| 50 | + |
| 51 | + # Start archive creation |
| 52 | + log.info(f"Creating archive '{example}.zip'") |
| 53 | + with ZipFile(archive, "w", ZIP_DEFLATED, False) as f: |
| 54 | + for name in spec.match_files(os.listdir(base), negate = True): |
| 55 | + path = os.path.join(base, name) |
| 56 | + if os.path.isdir(path): |
| 57 | + path = os.path.join(path, "**") |
| 58 | + |
| 59 | + # Find all files recursively and add them to the archive |
| 60 | + for file in iglob(path, recursive = True): |
| 61 | + location = os.path.join(name, os.path.relpath(file, base)) |
| 62 | + |
| 63 | + # Add file to archive |
| 64 | + log.debug(f"+ '{file}'") |
| 65 | + f.write(file, location) |
| 66 | + |
| 67 | +# ----------------------------------------------------------------------------- |
| 68 | +# Data |
| 69 | +# ----------------------------------------------------------------------------- |
| 70 | + |
| 71 | +# Set up logging |
| 72 | +log = logging.getLogger("mkdocs.material.examples") |
0 commit comments