mkdocs-data-plugin 0.1.0__py3-none-any.whl

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.
@@ -0,0 +1,2 @@
1
+ version = "0.1.0"
2
+ __version__ = "0.1.0"
@@ -0,0 +1,78 @@
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
+
@@ -0,0 +1,42 @@
1
+ Metadata-Version: 2.3
2
+ Name: mkdocs-data-plugin
3
+ Version: 0.1.0
4
+ Summary: MkDocs plugin that allows to read data from markup files
5
+ Project-URL: Documentation, https://joapuiib.github.io/mkdocs-data-plugin
6
+ Project-URL: Download, https://github.com/joapuiib/mkdocs-data-plugin/releases
7
+ Project-URL: Homepage, https://joapuiib.github.io/mkdocs-data-plugin
8
+ Project-URL: Source, https://github.com/joapuiib/mkdocs-data-plugin
9
+ Project-URL: Tracker, https://github.com/joapuiib/mkdocs-data-plugin/issues
10
+ Author-email: Joan Puigcerver <joapuiib@gmail.com>
11
+ License: MIT
12
+ Classifier: Topic :: Software Development :: Libraries :: Python Modules
13
+ Requires-Python: >=3.8
14
+ Requires-Dist: mkdocs-macros-plugin>=1.2.0
15
+ Requires-Dist: mkdocs>=1.5.2
16
+ Description-Content-Type: text/markdown
17
+
18
+ # mkdocs-data-plugin
19
+ MkDocs plugin that allows to read data from markup files
20
+
21
+ Currently supported formats:
22
+
23
+ - JSON
24
+ - YAML
25
+
26
+ ## Documentation
27
+ This plugin documentation can be found here: https://joapuiib.github.io/mkdocs-data-plugin/
28
+
29
+ ## Installation
30
+ This plugin can be installed via pip:
31
+
32
+ ```bash
33
+ pip install mkdocs-data-plugin
34
+ ```
35
+
36
+ ## Configuration
37
+ Activate the plugin in your `mkdocs.yml`:
38
+
39
+ ```yaml
40
+ plugins:
41
+ - data
42
+ ```
@@ -0,0 +1,6 @@
1
+ mkdocs_data_plugin/__init__.py,sha256=cN0zX9gYNljh3D9btS9CoJ9ot7pJI9L19IzFTtAtLd4,40
2
+ mkdocs_data_plugin/plugin.py,sha256=X6h6QmGPUfjzy-dF_aAv1eK3E-DpoCU6hMBuPj9VGr4,2115
3
+ mkdocs_data_plugin-0.1.0.dist-info/METADATA,sha256=X4ohyRjPLxp-T5WGDsZ2lfxx6Cvf40deI-JDUZOhs3U,1190
4
+ mkdocs_data_plugin-0.1.0.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
5
+ mkdocs_data_plugin-0.1.0.dist-info/entry_points.txt,sha256=OPCZHj_0qGtoX7jiNvaejvGQYPK_b7CmhSbLKAsxzl4,61
6
+ mkdocs_data_plugin-0.1.0.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.26.3
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,2 @@
1
+ [mkdocs.plugins]
2
+ data = mkdocs_data_plugin.plugin:DataPlugin