Skip to content
This repository was archived by the owner on Nov 6, 2025. It is now read-only.

Commit af42f39

Browse files
committed
Updated archive creation hook
1 parent ac1837a commit af42f39

File tree

5 files changed

+76
-77
lines changed

5 files changed

+76
-77
lines changed

examples/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,3 @@ def transform(project: MkDocsConfig, config: MkDocsConfig):
4747
# Inherit settings for theme
4848
merge(project.theme["icon"], config.theme["icon"])
4949
project.theme["features"].extend(config.theme["features"])
50-
51-
root = os.path.dirname(config.config_file_path)
52-
project.hooks = [
53-
os.path.join(root, "hooks", "zip.py")
54-
]

examples/tags/docs/README.md

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,16 +12,3 @@ This example includes the following pages:
1212
[HTML]: demo/tags.md#html
1313
[JavaScript]: demo/tags.md#javascript
1414
[CSS]: demo/tags.md#css
15-
16-
Download example as archive:
17-
18-
[Download .zip][Download]{ .md-button .md-button--primary }
19-
20-
Unzip archive, change to the folder and enter:
21-
22-
``` bash
23-
pip install -r requirements.txt
24-
mkdocs serve
25-
```
26-
27-
[Download]: ../../download/tags.zip

hooks/archive.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
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")

hooks/zip.py

Lines changed: 0 additions & 59 deletions
This file was deleted.

mkdocs.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ plugins:
5252
projects_dir: examples
5353
projects_config_transform: !!python/name:examples.transform
5454

55+
# Hooks
56+
hooks:
57+
- hooks/archive.py
58+
5559
# Customization
5660
extra:
5761
analytics:

0 commit comments

Comments
 (0)