Skip to content

Commit 76c0183

Browse files
authored
Merge pull request #8 from retorquere/external-data
allow loading of external data
2 parents 870dbd8 + ceeb769 commit 76c0183

File tree

3 files changed

+48
-3
lines changed

3 files changed

+48
-3
lines changed

‎README.md‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@ Enable the plugin in your `mkdocs.yml`:
2727
```yaml
2828
plugins:
2929
- search
30-
- markdownextradata
30+
- markdownextradata:
31+
data: path/to/datafiles
3132
```
3233
3334
> **Note:** If you have no `plugins` entry in your config file yet, you'll likely also want to add the `search` plugin. MkDocs enables it by default if there is no `plugins` entry set, but now you have to enable it explicitly.
3435

3536
More information about plugins in the [MkDocs documentation][mkdocs-plugins]
3637

38+
The data path is optional; when absent, it will look for a `_data`
39+
folder adjacent to your `mkdocs.yml` and inside your `docs_dir`.
40+
If this path is found, the plugin will read all `.yml` and `.json`
41+
files inside it and add the data in them to the data available to the templates.
42+
The paths to these become their variable names, so if inside your data folder you have a file
43+
called `sections/captions.yml`, the data inside that file will be available in your
44+
templates as `sections.captions`.
45+
3746
<br/>
3847

3948
## Features

‎markdownextradata/plugin.py‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
from mkdocs.plugins import BasePlugin
22

33
from jinja2 import Template
4-
4+
import os
5+
from pathlib import Path
6+
import mkdocs
7+
import yaml
8+
from itertools import chain
59

610
CONFIG_KEYS = [
711
'site_name',
@@ -16,6 +20,37 @@ class MarkdownExtraDataPlugin(BasePlugin):
1620
"""
1721
Inject certain config variables into the markdown
1822
"""
23+
24+
config_scheme = (
25+
('data', mkdocs.config.config_options.Type(mkdocs.utils.string_types, default=None)),
26+
)
27+
28+
def __add_data__(self, config, namespace, data):
29+
# creates the namespace and adds the data there
30+
namespace = ['extra'] + namespace.split(os.sep)
31+
holder = config
32+
while len(namespace) > 1:
33+
if not namespace[0] in holder: holder[namespace[0]] = {}
34+
holder = holder[namespace[0]]
35+
del namespace[0]
36+
holder[namespace[0]] = data
37+
38+
def on_pre_build(self, config):
39+
# this loads all data from the supplied data directory, or otherwise a _data directory next to mkdocs.yml or inside the docs_dir. Does nothing if the dir does not exist.
40+
41+
data = self.config.get('data')
42+
for datadir in [ os.path.dirname(config['config_file_path']), config['docs_dir'] ]:
43+
if not data:
44+
data = os.path.join(datadir, '_data')
45+
if not os.path.exists(data): data = None
46+
47+
if data and os.path.exists(data):
48+
path = Path(data)
49+
for filename in chain(path.glob('**/*.yml'), path.glob('**/*.json')):
50+
with open(filename) as f:
51+
namespace = os.path.splitext(os.path.relpath(filename, data))[0]
52+
self.__add_data__(config, namespace, (yaml.load(f) if filename.suffix == '.yml' else json.load(f)))
53+
1954
def on_page_markdown(self, markdown, config, **kwargs):
2055
context = {key: config.get(key) for key in CONFIG_KEYS if key in config}
2156
context.update(config.get('extra', {}))

‎setup.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ def read(fname):
1818
license='MIT',
1919
python_requires='>=2.7.9,!=3.0.*,!=3.1.*,!=3.2.*,!=3.3.*',
2020
install_requires=[
21-
'mkdocs>=0.17'
21+
'mkdocs>=0.17',
22+
'pyyaml',
2223
],
2324
classifiers=[
2425
'Development Status :: 5 - Production/Stable',

0 commit comments

Comments
 (0)