mkdocs-data-plugin 0.1.0__tar.gz → 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- mkdocs_data_plugin-0.3.0/LICENSE +21 -0
- {mkdocs_data_plugin-0.1.0 → mkdocs_data_plugin-0.3.0}/PKG-INFO +38 -7
- mkdocs_data_plugin-0.3.0/README.md +54 -0
- mkdocs_data_plugin-0.3.0/mkdocs_data_plugin/__init__.py +5 -0
- mkdocs_data_plugin-0.3.0/mkdocs_data_plugin/plugin.py +88 -0
- {mkdocs_data_plugin-0.1.0 → mkdocs_data_plugin-0.3.0}/pyproject.toml +14 -8
- mkdocs_data_plugin-0.3.0/tests/data/a.yml +2 -0
- mkdocs_data_plugin-0.3.0/tests/data/dir1/b.yml +2 -0
- mkdocs_data_plugin-0.3.0/tests/data/dir1.yml +2 -0
- mkdocs_data_plugin-0.3.0/tests/data/dir2/c.json +4 -0
- mkdocs_data_plugin-0.3.0/tests/docs/fruits.yml +3 -0
- mkdocs_data_plugin-0.3.0/tests/docs/test_dir_source.md +11 -0
- mkdocs_data_plugin-0.3.0/tests/docs/test_file_source.md +7 -0
- {mkdocs_data_plugin-0.1.0 → mkdocs_data_plugin-0.3.0}/tests/test_config.py +4 -4
- mkdocs_data_plugin-0.3.0/tests/test_data_template.py +52 -0
- mkdocs_data_plugin-0.3.0/tests/test_load_data.py +102 -0
- mkdocs_data_plugin-0.1.0/README.md +0 -25
- mkdocs_data_plugin-0.1.0/mkdocs_data_plugin/__init__.py +0 -2
- mkdocs_data_plugin-0.1.0/mkdocs_data_plugin/plugin.py +0 -78
- mkdocs_data_plugin-0.1.0/tests/data/a.yml +0 -2
- mkdocs_data_plugin-0.1.0/tests/data/dir1/b.yml +0 -2
- mkdocs_data_plugin-0.1.0/tests/data/dir2/c.json +0 -4
- mkdocs_data_plugin-0.1.0/tests/docs/test_data_template.md +0 -11
- mkdocs_data_plugin-0.1.0/tests/test_data_template.py +0 -31
- mkdocs_data_plugin-0.1.0/tests/test_load_data.py +0 -51
- {mkdocs_data_plugin-0.1.0 → mkdocs_data_plugin-0.3.0}/.gitignore +0 -0
- {mkdocs_data_plugin-0.1.0 → mkdocs_data_plugin-0.3.0}/tests/__init__.py +0 -0
- {mkdocs_data_plugin-0.1.0 → mkdocs_data_plugin-0.3.0}/tests/mkdocs.yml +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Joan Puigcerver
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: mkdocs-data-plugin
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.3.0
|
|
4
4
|
Summary: MkDocs plugin that allows to read data from markup files
|
|
5
5
|
Project-URL: Documentation, https://joapuiib.github.io/mkdocs-data-plugin
|
|
6
6
|
Project-URL: Download, https://github.com/joapuiib/mkdocs-data-plugin/releases
|
|
@@ -8,20 +8,23 @@ Project-URL: Homepage, https://joapuiib.github.io/mkdocs-data-plugin
|
|
|
8
8
|
Project-URL: Source, https://github.com/joapuiib/mkdocs-data-plugin
|
|
9
9
|
Project-URL: Tracker, https://github.com/joapuiib/mkdocs-data-plugin/issues
|
|
10
10
|
Author-email: Joan Puigcerver <joapuiib@gmail.com>
|
|
11
|
-
License: MIT
|
|
11
|
+
License-Expression: MIT
|
|
12
|
+
License-File: LICENSE
|
|
12
13
|
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
13
14
|
Requires-Python: >=3.8
|
|
14
15
|
Requires-Dist: mkdocs-macros-plugin>=1.2.0
|
|
15
|
-
Requires-Dist: mkdocs
|
|
16
|
+
Requires-Dist: mkdocs<2.0,>=1.5.2
|
|
17
|
+
Requires-Dist: properdocs>=1.6
|
|
16
18
|
Description-Content-Type: text/markdown
|
|
17
19
|
|
|
18
20
|
# mkdocs-data-plugin
|
|
19
|
-
|
|
21
|
+
__MkDocs Data Plugin__ is a plugin for [MkDocs](https://www.mkdocs.org/) that allows
|
|
22
|
+
reading data from markup files and use it in your Markdown pages.
|
|
20
23
|
|
|
21
24
|
Currently supported formats:
|
|
22
25
|
|
|
23
|
-
- JSON
|
|
24
|
-
- YAML
|
|
26
|
+
- JSON: `.json`
|
|
27
|
+
- YAML: `.yml`, `.yaml`
|
|
25
28
|
|
|
26
29
|
## Documentation
|
|
27
30
|
This plugin documentation can be found here: https://joapuiib.github.io/mkdocs-data-plugin/
|
|
@@ -38,5 +41,33 @@ Activate the plugin in your `mkdocs.yml`:
|
|
|
38
41
|
|
|
39
42
|
```yaml
|
|
40
43
|
plugins:
|
|
44
|
+
- macros
|
|
41
45
|
- data
|
|
42
46
|
```
|
|
47
|
+
|
|
48
|
+
## Overview
|
|
49
|
+
When using this plugin, you can define data in YAML or JSON files
|
|
50
|
+
in a separate directory and reference them in your Markdown files.
|
|
51
|
+
|
|
52
|
+
```txt
|
|
53
|
+
root/
|
|
54
|
+
├── docs/
|
|
55
|
+
│ └── ...
|
|
56
|
+
├── data/
|
|
57
|
+
│ └── fruits.yml
|
|
58
|
+
└── mkdocs.yml
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
```yaml title="fruits.yml"
|
|
62
|
+
- Apple
|
|
63
|
+
- Banana
|
|
64
|
+
- Strawberry
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
Files in this directory can be referenced in your Markdown files using the `data` variable.
|
|
68
|
+
|
|
69
|
+
```markdown
|
|
70
|
+
{% for fruit in data.fruits -%}
|
|
71
|
+
- {{ fruit }}
|
|
72
|
+
{% endfor %}
|
|
73
|
+
```
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
# mkdocs-data-plugin
|
|
2
|
+
__MkDocs Data Plugin__ is a plugin for [MkDocs](https://www.mkdocs.org/) that allows
|
|
3
|
+
reading data from markup files and use it in your Markdown pages.
|
|
4
|
+
|
|
5
|
+
Currently supported formats:
|
|
6
|
+
|
|
7
|
+
- JSON: `.json`
|
|
8
|
+
- YAML: `.yml`, `.yaml`
|
|
9
|
+
|
|
10
|
+
## Documentation
|
|
11
|
+
This plugin documentation can be found here: https://joapuiib.github.io/mkdocs-data-plugin/
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
This plugin can be installed via pip:
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
pip install mkdocs-data-plugin
|
|
18
|
+
```
|
|
19
|
+
|
|
20
|
+
## Configuration
|
|
21
|
+
Activate the plugin in your `mkdocs.yml`:
|
|
22
|
+
|
|
23
|
+
```yaml
|
|
24
|
+
plugins:
|
|
25
|
+
- macros
|
|
26
|
+
- data
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
## Overview
|
|
30
|
+
When using this plugin, you can define data in YAML or JSON files
|
|
31
|
+
in a separate directory and reference them in your Markdown files.
|
|
32
|
+
|
|
33
|
+
```txt
|
|
34
|
+
root/
|
|
35
|
+
├── docs/
|
|
36
|
+
│ └── ...
|
|
37
|
+
├── data/
|
|
38
|
+
│ └── fruits.yml
|
|
39
|
+
└── mkdocs.yml
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
```yaml title="fruits.yml"
|
|
43
|
+
- Apple
|
|
44
|
+
- Banana
|
|
45
|
+
- Strawberry
|
|
46
|
+
```
|
|
47
|
+
|
|
48
|
+
Files in this directory can be referenced in your Markdown files using the `data` variable.
|
|
49
|
+
|
|
50
|
+
```markdown
|
|
51
|
+
{% for fruit in data.fruits -%}
|
|
52
|
+
- {{ fruit }}
|
|
53
|
+
{% endfor %}
|
|
54
|
+
```
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import os
|
|
3
|
+
|
|
4
|
+
import yaml
|
|
5
|
+
from mkdocs.config import base
|
|
6
|
+
from mkdocs.config import config_options as c
|
|
7
|
+
from mkdocs.config.defaults import MkDocsConfig
|
|
8
|
+
from mkdocs.plugins import BasePlugin, get_plugin_logger
|
|
9
|
+
|
|
10
|
+
log = get_plugin_logger(__name__)
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class DataPluginConfig(base.Config):
|
|
14
|
+
sources = c.Type(dict, default={'data': 'data'})
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class DataPlugin(BasePlugin[DataPluginConfig]):
|
|
18
|
+
def __init__(self):
|
|
19
|
+
self.sources = {}
|
|
20
|
+
self.processors = {'.yml': yaml.safe_load, '.yaml': yaml.safe_load, '.json': json.load}
|
|
21
|
+
|
|
22
|
+
def load_sources(self):
|
|
23
|
+
"""
|
|
24
|
+
Load all sources from the config file and load the data from the files.
|
|
25
|
+
"""
|
|
26
|
+
for source, path in self.config['sources'].items():
|
|
27
|
+
if not os.path.exists(path):
|
|
28
|
+
log.warning(f"Mapping path '{path}' not found. Skipping.")
|
|
29
|
+
elif os.path.isdir(path):
|
|
30
|
+
self.load_folder(source, path)
|
|
31
|
+
else:
|
|
32
|
+
value = self.load_file(path)
|
|
33
|
+
self.update_data(source, [], value)
|
|
34
|
+
|
|
35
|
+
def update_data(self, source: str, keys: list, value: any):
|
|
36
|
+
"""
|
|
37
|
+
Update the sources data with the given value.
|
|
38
|
+
"""
|
|
39
|
+
if len(keys) == 0:
|
|
40
|
+
self.sources[source] = value
|
|
41
|
+
else:
|
|
42
|
+
data = self.sources.setdefault(source, {})
|
|
43
|
+
for key in keys[:-1]:
|
|
44
|
+
data = data.setdefault(key, {})
|
|
45
|
+
data[keys[-1]] = value
|
|
46
|
+
|
|
47
|
+
def load_folder(self, source: str, path: str):
|
|
48
|
+
"""
|
|
49
|
+
Iterate over all files in the data directory
|
|
50
|
+
and load them into the data attribute.
|
|
51
|
+
"""
|
|
52
|
+
for root, _, files in os.walk(path):
|
|
53
|
+
|
|
54
|
+
keys = []
|
|
55
|
+
if root != path:
|
|
56
|
+
directory = os.path.relpath(root, path)
|
|
57
|
+
keys = directory.split(os.sep)
|
|
58
|
+
|
|
59
|
+
for file in files:
|
|
60
|
+
value = self.load_file(os.path.join(root, file))
|
|
61
|
+
|
|
62
|
+
filename, _ = os.path.splitext(file)
|
|
63
|
+
self.update_data(source, keys + [filename], value)
|
|
64
|
+
|
|
65
|
+
def load_file(self, path: str):
|
|
66
|
+
"""
|
|
67
|
+
Loads a file and processes it with the appropriate processor.
|
|
68
|
+
"""
|
|
69
|
+
_, extension = os.path.splitext(path)
|
|
70
|
+
with open(path, 'r') as file:
|
|
71
|
+
return self.processors[extension](file)
|
|
72
|
+
|
|
73
|
+
def on_config(self, config: MkDocsConfig):
|
|
74
|
+
self.load_sources()
|
|
75
|
+
|
|
76
|
+
macros_plugin = config.plugins.get('macros')
|
|
77
|
+
if macros_plugin:
|
|
78
|
+
for source, data in self.sources.items():
|
|
79
|
+
macros_plugin.register_variables({source: data})
|
|
80
|
+
else:
|
|
81
|
+
log.warning(
|
|
82
|
+
"The macros plugin is not installed. The `data` variable won't be available in pages."
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
def on_page_context(self, context, page, config, nav):
|
|
86
|
+
for source, data in self.sources.items():
|
|
87
|
+
context[source] = data
|
|
88
|
+
return context
|
|
@@ -16,7 +16,8 @@ classifiers = [
|
|
|
16
16
|
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
17
17
|
]
|
|
18
18
|
dependencies = [
|
|
19
|
-
"mkdocs>=1.5.2",
|
|
19
|
+
"mkdocs>=1.5.2,<2.0",
|
|
20
|
+
"properdocs>=1.6",
|
|
20
21
|
"mkdocs-macros-plugin>=1.2.0",
|
|
21
22
|
]
|
|
22
23
|
|
|
@@ -63,12 +64,17 @@ type = ["default"]
|
|
|
63
64
|
|
|
64
65
|
[tool.hatch.envs.doc]
|
|
65
66
|
dependencies = [
|
|
66
|
-
"mkdocs-
|
|
67
|
+
"mkdocs-materialx[imaging]~=10.1",
|
|
68
|
+
"mkdocs-alias-plugin~=0.8",
|
|
69
|
+
"mkdocs-git-revision-date-localized-plugin~=1.2",
|
|
67
70
|
]
|
|
68
71
|
|
|
69
72
|
[tool.hatch.envs.doc.scripts]
|
|
70
73
|
serve = [
|
|
71
|
-
"
|
|
74
|
+
"properdocs serve",
|
|
75
|
+
]
|
|
76
|
+
build = [
|
|
77
|
+
"properdocs build",
|
|
72
78
|
]
|
|
73
79
|
|
|
74
80
|
[tool.hatch.envs.style]
|
|
@@ -81,13 +87,13 @@ dependencies = [
|
|
|
81
87
|
|
|
82
88
|
[tool.hatch.envs.style.scripts]
|
|
83
89
|
check = [
|
|
84
|
-
"isort --check-only --diff
|
|
85
|
-
"black -q --check --diff
|
|
86
|
-
"ruff check
|
|
90
|
+
"isort --check-only --diff mkdocs_data_plugin",
|
|
91
|
+
"black -q --check --diff mkdocs_data_plugin",
|
|
92
|
+
"ruff check mkdocs_data_plugin",
|
|
87
93
|
]
|
|
88
94
|
format = [
|
|
89
|
-
"isort -q
|
|
90
|
-
"black -q
|
|
95
|
+
"isort -q mkdocs_data_plugin",
|
|
96
|
+
"black -q mkdocs_data_plugin",
|
|
91
97
|
]
|
|
92
98
|
|
|
93
99
|
[tool.black]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
---
|
|
2
|
+
title: "Test Directory Source"
|
|
3
|
+
---
|
|
4
|
+
|
|
5
|
+
- data.a: `{{ data.a }}`
|
|
6
|
+
- data.a.number: `{{ data.a.number }}`
|
|
7
|
+
- data.a.string: `{{ data.a.string }}`
|
|
8
|
+
- data.dir1.b.number: `{{ data.dir1.b.number }}`
|
|
9
|
+
- data.dir1.b.string: `{{ data.dir1.b.string }}`
|
|
10
|
+
- data.dir2.c.number: `{{ data.dir2.c.number }}`
|
|
11
|
+
- data.dir2.c.string: `{{ data.dir2.c.string }}`
|
|
@@ -6,10 +6,10 @@ from mkdocs_data_plugin.plugin import DataPlugin
|
|
|
6
6
|
def test_config_default_values():
|
|
7
7
|
plugin = DataPlugin()
|
|
8
8
|
plugin.load_config({})
|
|
9
|
-
assert plugin.config.
|
|
9
|
+
assert plugin.config.sources == {'data': 'data'}
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
def
|
|
12
|
+
def test_config_sources():
|
|
13
13
|
plugin = DataPlugin()
|
|
14
|
-
plugin.load_config({'
|
|
15
|
-
assert plugin.config.
|
|
14
|
+
plugin.load_config({'sources': {'data': 'other_data'}})
|
|
15
|
+
assert plugin.config.sources == {'data': 'other_data'}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from mkdocs.commands.build import build
|
|
4
|
+
from mkdocs.config.base import load_config
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_dir_source_in_markdown_file():
|
|
8
|
+
mkdocs_config = load_config(
|
|
9
|
+
"tests/mkdocs.yml",
|
|
10
|
+
plugins={
|
|
11
|
+
"macros": {},
|
|
12
|
+
"data": {'sources': {'data': 'tests/data'}},
|
|
13
|
+
},
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
build(mkdocs_config)
|
|
17
|
+
site_dir = mkdocs_config["site_dir"]
|
|
18
|
+
|
|
19
|
+
with open(site_dir+'/test_dir_source/index.html') as f:
|
|
20
|
+
data_loaded = re.findall(r"<code>([^<]*)", f.read())
|
|
21
|
+
print(data_loaded)
|
|
22
|
+
assert(data_loaded == [
|
|
23
|
+
"{'number': 1, 'string': 'text'}", # data/a.yml
|
|
24
|
+
"1", # data/a.yml -> number
|
|
25
|
+
"text", # data/a.yml -> string
|
|
26
|
+
"2", # data/dir1/b.yml -> number
|
|
27
|
+
"text", # data/dir1/b.yml -> string
|
|
28
|
+
"3", # data/dir2/c.yml -> number
|
|
29
|
+
"text", # data/dir2/c.yml -> string
|
|
30
|
+
])
|
|
31
|
+
|
|
32
|
+
def test_file_source_in_markdown_file():
|
|
33
|
+
mkdocs_config = load_config(
|
|
34
|
+
"tests/mkdocs.yml",
|
|
35
|
+
plugins={
|
|
36
|
+
"macros": {},
|
|
37
|
+
"data": {'sources': {'fruits': 'tests/docs/fruits.yml'}},
|
|
38
|
+
},
|
|
39
|
+
)
|
|
40
|
+
|
|
41
|
+
build(mkdocs_config)
|
|
42
|
+
site_dir = mkdocs_config["site_dir"]
|
|
43
|
+
|
|
44
|
+
with open(site_dir+'/test_file_source/index.html') as f:
|
|
45
|
+
data_loaded = re.findall(r"<code>([^<]*)", f.read())
|
|
46
|
+
print(data_loaded)
|
|
47
|
+
assert(data_loaded == [
|
|
48
|
+
"Apple",
|
|
49
|
+
"Banana",
|
|
50
|
+
"Strawberry",
|
|
51
|
+
])
|
|
52
|
+
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
from mkdocs.commands.build import build
|
|
2
|
+
from mkdocs.config.base import load_config
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def test_inexistent_source_is_skipped():
|
|
6
|
+
mkdocs_config = load_config(
|
|
7
|
+
"tests/mkdocs.yml",
|
|
8
|
+
plugins={
|
|
9
|
+
"data": {'sources': {'data': 'tests/inexistent'}}
|
|
10
|
+
},
|
|
11
|
+
)
|
|
12
|
+
|
|
13
|
+
build(mkdocs_config)
|
|
14
|
+
|
|
15
|
+
dataPlugin = mkdocs_config["plugins"]["data"]
|
|
16
|
+
|
|
17
|
+
assert "data" not in dataPlugin.sources
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_folder_source():
|
|
21
|
+
mkdocs_config = load_config(
|
|
22
|
+
"tests/mkdocs.yml",
|
|
23
|
+
plugins={
|
|
24
|
+
"data": {'sources': {'data': 'tests/data'}}
|
|
25
|
+
},
|
|
26
|
+
)
|
|
27
|
+
|
|
28
|
+
build(mkdocs_config)
|
|
29
|
+
|
|
30
|
+
dataPlugin = mkdocs_config["plugins"]["data"]
|
|
31
|
+
data = dataPlugin.sources["data"]
|
|
32
|
+
|
|
33
|
+
assert data == {
|
|
34
|
+
"a": {
|
|
35
|
+
"number": 1,
|
|
36
|
+
"string": "text",
|
|
37
|
+
},
|
|
38
|
+
"dir1": {
|
|
39
|
+
"number": 1,
|
|
40
|
+
"string": "text",
|
|
41
|
+
"b": {
|
|
42
|
+
"number": 2,
|
|
43
|
+
"string": "text",
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
"dir2": {
|
|
47
|
+
"c": {
|
|
48
|
+
"number": 3,
|
|
49
|
+
"string": "text",
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
def test_folder_source_slash():
|
|
55
|
+
mkdocs_config = load_config(
|
|
56
|
+
"tests/mkdocs.yml",
|
|
57
|
+
plugins={
|
|
58
|
+
"data": {'sources': {'data': 'tests/data/'}}
|
|
59
|
+
},
|
|
60
|
+
)
|
|
61
|
+
|
|
62
|
+
build(mkdocs_config)
|
|
63
|
+
|
|
64
|
+
dataPlugin = mkdocs_config["plugins"]["data"]
|
|
65
|
+
data = dataPlugin.sources["data"]
|
|
66
|
+
|
|
67
|
+
assert data == {
|
|
68
|
+
"a": {
|
|
69
|
+
"number": 1,
|
|
70
|
+
"string": "text",
|
|
71
|
+
},
|
|
72
|
+
"dir1": {
|
|
73
|
+
"number": 1,
|
|
74
|
+
"string": "text",
|
|
75
|
+
"b": {
|
|
76
|
+
"number": 2,
|
|
77
|
+
"string": "text",
|
|
78
|
+
},
|
|
79
|
+
},
|
|
80
|
+
"dir2": {
|
|
81
|
+
"c": {
|
|
82
|
+
"number": 3,
|
|
83
|
+
"string": "text",
|
|
84
|
+
},
|
|
85
|
+
},
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
def test_file_source():
|
|
89
|
+
mkdocs_config = load_config(
|
|
90
|
+
"tests/mkdocs.yml",
|
|
91
|
+
plugins={
|
|
92
|
+
"data": {'sources': {'fruits': 'tests/docs/fruits.yml'}}
|
|
93
|
+
},
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
build(mkdocs_config)
|
|
97
|
+
|
|
98
|
+
dataPlugin = mkdocs_config["plugins"]["data"]
|
|
99
|
+
data = dataPlugin.sources["fruits"]
|
|
100
|
+
print(dataPlugin.sources)
|
|
101
|
+
|
|
102
|
+
assert data == ['Apple', 'Banana', 'Strawberry']
|
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
# mkdocs-data-plugin
|
|
2
|
-
MkDocs plugin that allows to read data from markup files
|
|
3
|
-
|
|
4
|
-
Currently supported formats:
|
|
5
|
-
|
|
6
|
-
- JSON
|
|
7
|
-
- YAML
|
|
8
|
-
|
|
9
|
-
## Documentation
|
|
10
|
-
This plugin documentation can be found here: https://joapuiib.github.io/mkdocs-data-plugin/
|
|
11
|
-
|
|
12
|
-
## Installation
|
|
13
|
-
This plugin can be installed via pip:
|
|
14
|
-
|
|
15
|
-
```bash
|
|
16
|
-
pip install mkdocs-data-plugin
|
|
17
|
-
```
|
|
18
|
-
|
|
19
|
-
## Configuration
|
|
20
|
-
Activate the plugin in your `mkdocs.yml`:
|
|
21
|
-
|
|
22
|
-
```yaml
|
|
23
|
-
plugins:
|
|
24
|
-
- data
|
|
25
|
-
```
|
|
@@ -1,78 +0,0 @@
|
|
|
1
|
-
import os
|
|
2
|
-
import yaml
|
|
3
|
-
import json
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
from mkdocs.config.defaults import MkDocsConfig
|
|
7
|
-
from mkdocs.config import base, config_options as c
|
|
8
|
-
from mkdocs.plugins import BasePlugin, get_plugin_logger
|
|
9
|
-
|
|
10
|
-
log = get_plugin_logger(__name__)
|
|
11
|
-
|
|
12
|
-
class DataPluginConfig(base.Config):
|
|
13
|
-
data_dir = c.Type(str, default='data')
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
class DataPlugin(BasePlugin[DataPluginConfig]):
|
|
17
|
-
def __init__(self):
|
|
18
|
-
self.data = {}
|
|
19
|
-
self.processors = {
|
|
20
|
-
'.yml': yaml.safe_load,
|
|
21
|
-
'.yaml': yaml.safe_load,
|
|
22
|
-
'.json': json.load
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
def set_data(self, keys, value):
|
|
27
|
-
"""
|
|
28
|
-
Set a value in the data attribute.
|
|
29
|
-
"""
|
|
30
|
-
data = self.data
|
|
31
|
-
for key in keys[:-1]:
|
|
32
|
-
data = data.setdefault(key, {})
|
|
33
|
-
data[keys[-1]] = value
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
def on_config(self, config: MkDocsConfig):
|
|
37
|
-
self.load_data(self.config.data_dir)
|
|
38
|
-
|
|
39
|
-
macros_plugin = config.plugins.get('macros')
|
|
40
|
-
if macros_plugin:
|
|
41
|
-
macros_plugin.register_variables({'data': self.data})
|
|
42
|
-
else:
|
|
43
|
-
log.warning("The macros plugin is not installed. The `data` variable won't be available in pages.")
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
def load_data(self, path: str):
|
|
47
|
-
"""
|
|
48
|
-
Iterate over all files in the data directory
|
|
49
|
-
and load them into the data attribute.
|
|
50
|
-
"""
|
|
51
|
-
for root, _, files in os.walk(path):
|
|
52
|
-
|
|
53
|
-
keys = []
|
|
54
|
-
if root != self.config.data_dir:
|
|
55
|
-
directory = os.path.relpath(root, self.config.data_dir)
|
|
56
|
-
keys = directory.split(os.sep)
|
|
57
|
-
|
|
58
|
-
for file in files:
|
|
59
|
-
value = self.load_file(os.path.join(root, file))
|
|
60
|
-
|
|
61
|
-
filename, _ = os.path.splitext(file)
|
|
62
|
-
self.set_data(keys + [filename], value)
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
def load_file(self, path: str):
|
|
66
|
-
"""
|
|
67
|
-
Load a file and return its content.
|
|
68
|
-
"""
|
|
69
|
-
_, extension = os.path.splitext(path)
|
|
70
|
-
with open(path, 'r') as file:
|
|
71
|
-
return self.processors[extension](file)
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
def on_page_context(self, context, page, config, nav):
|
|
76
|
-
context['data'] = self.data
|
|
77
|
-
return context
|
|
78
|
-
|
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
---
|
|
2
|
-
title: "Test Data Template"
|
|
3
|
-
---
|
|
4
|
-
|
|
5
|
-
- data.a: `{{ data.a }}`
|
|
6
|
-
- data.a.a1: `{{ data.a.a1 }}`
|
|
7
|
-
- data.a.a2: `{{ data.a.a2 }}`
|
|
8
|
-
- data.dir1.b.b1: `{{ data.dir1.b.b1 }}`
|
|
9
|
-
- data.dir1.b.b2: `{{ data.dir1.b.b2 }}`
|
|
10
|
-
- data.dir2.c.c1: `{{ data.dir2.c.c1 }}`
|
|
11
|
-
- data.dir2.c.c2: `{{ data.dir2.c.c2 }}`
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
import re
|
|
2
|
-
|
|
3
|
-
from mkdocs.commands.build import build
|
|
4
|
-
from mkdocs.config.base import load_config
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
def test_loads_files():
|
|
8
|
-
mkdocs_config = load_config(
|
|
9
|
-
"tests/mkdocs.yml",
|
|
10
|
-
plugins={
|
|
11
|
-
"macros": {},
|
|
12
|
-
"data": {'data_dir': 'tests/data'},
|
|
13
|
-
},
|
|
14
|
-
)
|
|
15
|
-
|
|
16
|
-
build(mkdocs_config)
|
|
17
|
-
site_dir = mkdocs_config["site_dir"]
|
|
18
|
-
|
|
19
|
-
with open(site_dir+'/test_data_template/index.html') as f:
|
|
20
|
-
data_loaded = re.findall(r"<code>([^<]*)", f.read())
|
|
21
|
-
print(data_loaded)
|
|
22
|
-
assert(data_loaded == [
|
|
23
|
-
"{'a1': 1, 'a2': 'text'}", # data/a.yml
|
|
24
|
-
"1", # data/a.yml -> a1
|
|
25
|
-
"text", # data/a.yml -> a2
|
|
26
|
-
"2", # data/dir1/b.yml -> b1
|
|
27
|
-
"text", # data/dir1/b.yml -> b2
|
|
28
|
-
"3", # data/dir2/c.yml -> c1
|
|
29
|
-
"text", # data/dir2/c.yml -> c2
|
|
30
|
-
])
|
|
31
|
-
|
|
@@ -1,51 +0,0 @@
|
|
|
1
|
-
from mkdocs.commands.build import build
|
|
2
|
-
from mkdocs.config.base import load_config
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
def test_empty_data_is_empty_dict():
|
|
6
|
-
mkdocs_config = load_config(
|
|
7
|
-
"tests/mkdocs.yml",
|
|
8
|
-
plugins={
|
|
9
|
-
"data": {'data_dir': 'tests/inexistent'}
|
|
10
|
-
},
|
|
11
|
-
)
|
|
12
|
-
|
|
13
|
-
build(mkdocs_config)
|
|
14
|
-
|
|
15
|
-
dataPlugin = mkdocs_config["plugins"]["data"]
|
|
16
|
-
data = dataPlugin.data
|
|
17
|
-
|
|
18
|
-
assert data == {}
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
def test_loads_files():
|
|
22
|
-
mkdocs_config = load_config(
|
|
23
|
-
"tests/mkdocs.yml",
|
|
24
|
-
plugins={
|
|
25
|
-
"data": {'data_dir': 'tests/data'}
|
|
26
|
-
},
|
|
27
|
-
)
|
|
28
|
-
|
|
29
|
-
build(mkdocs_config)
|
|
30
|
-
|
|
31
|
-
dataPlugin = mkdocs_config["plugins"]["data"]
|
|
32
|
-
data = dataPlugin.data
|
|
33
|
-
|
|
34
|
-
assert data == {
|
|
35
|
-
"a": {
|
|
36
|
-
"a1": 1,
|
|
37
|
-
"a2": "text",
|
|
38
|
-
},
|
|
39
|
-
"dir1": {
|
|
40
|
-
"b": {
|
|
41
|
-
"b1": 2,
|
|
42
|
-
"b2": "text",
|
|
43
|
-
},
|
|
44
|
-
},
|
|
45
|
-
"dir2": {
|
|
46
|
-
"c": {
|
|
47
|
-
"c1": 3,
|
|
48
|
-
"c2": "text",
|
|
49
|
-
},
|
|
50
|
-
},
|
|
51
|
-
}
|
|
File without changes
|
|
File without changes
|
|
File without changes
|