Skip to content

Commit b521b2d

Browse files
committed
Upgraded pathing structure to handle multiple and better handling of default locations and better readme
1 parent abc1cdd commit b521b2d

File tree

3 files changed

+88
-42
lines changed

3 files changed

+88
-42
lines changed

‎README.md‎

Lines changed: 48 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,33 +35,19 @@ You are then able to use the mkdocs `extra: {}` hash to pass context data into y
3535
> **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.
3636

3737

38-
### Using external data files
39-
40-
If the `extra: {}` hash is not enough for your data then you are able to make use of external yaml files to provide that context data
41-
42-
```yaml
43-
plugins:
44-
- search
45-
- markdownextradata:
46-
data: path/to/datafiles
47-
```
48-
49-
The data path is optional; when absent, it will look for a `_data`
50-
folder adjacent to your `mkdocs.yml` and inside your `docs_dir`.
51-
If this path is found, the plugin will read all `.yml` and `.json`
52-
files inside it and add the data in them to the data available to the templates.
53-
The paths to these become their variable names, so if inside your data folder you have a file
54-
called `sections/captions.yml`, the data inside that file will be available in your
55-
templates as `sections.captions`.
56-
57-
5838
## Features
5939

6040
### Use Extra Variables in your markdown files
6141

6242
The variables you define in the mkdown.yml `extra:` slot will become available in your templates
6343

6444
```yaml
45+
site_name: My fantastic site
46+
47+
plugins:
48+
- search
49+
- markdownextradata
50+
6551
extra:
6652
customer:
6753
name: Your name here
@@ -76,6 +62,48 @@ and then in your `*.md` files
7662
<a href="{{ customer.web }}">{{ customer.web }}</a>
7763
```
7864

65+
### Using external data files
66+
67+
If the `extra: {}` hash is not enough for your data then you are able to make use of external yaml files to provide that context data
68+
69+
```yaml
70+
plugins:
71+
- search
72+
- markdownextradata:
73+
data: path/to/datafiles
74+
```
75+
76+
or if you have multiple locations provide a comma (,) separated list of locations
77+
78+
```yaml
79+
plugins:
80+
- search
81+
- markdownextradata:
82+
data: path/to/datafiles, another/path/to/datafiles
83+
```
84+
85+
if you leave `markdownextradata.data` empty
86+
87+
```yaml
88+
plugins:
89+
- search
90+
- markdownextradata
91+
```
92+
93+
by default it will search in the folder where your mkdocs.yml is kept
94+
and in the docs folder for another folder called `_data`
95+
96+
i.e. `./docs/_data/site.yaml` - available as '{{ site.whatever_variable_in_the_yaml}}'
97+
98+
If this path is found, the plugin will read all `.yml|.yaml` and `.json`
99+
files inside it and add the data in them, to the template context.
100+
101+
If inside your data folder you have a directory and a file file
102+
called `[path/to/datafiles/]sections/captions.yaml` - where `[path/to/datafiles/]` is the path in your configuration -
103+
the data inside that file will be available in your templates as `sections.captions.whatever_variable_in_the_yaml`.
104+
105+
106+
79107
## Testing
80108

81109
```

‎markdownextradata/plugin.py‎

Lines changed: 39 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import json
44
import yaml
55
import mkdocs
6+
from mkdocs.plugins import BasePlugin
67

78
from jinja2 import Template
89
from pathlib import Path
@@ -16,7 +17,7 @@
1617
str_type = mkdocs.utils.string_types
1718

1819

19-
class MarkdownExtraDataPlugin(mkdocs.plugins.BasePlugin):
20+
class MarkdownExtraDataPlugin(BasePlugin):
2021
"""
2122
Inject certain config variables into the markdown
2223
"""
@@ -37,36 +38,53 @@ def __add_data__(self, config, namespace, data):
3738
holder[namespace[0]] = data
3839

3940
def on_pre_build(self, config):
40-
# 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.
41+
# Loads all data from the supplied data directories
42+
# or, otherwise a _data directory next to mkdocs.yml and/or inside the docs_dir.
43+
# Does nothing if the dir does not exist.
4144

42-
data = self.config.get("data")
43-
for datadir in [
44-
os.path.dirname(config["config_file_path"]),
45-
config["docs_dir"],
46-
]:
47-
if not data:
48-
data = os.path.join(datadir, "_data")
49-
if not os.path.exists(data):
50-
data = None
45+
# assume an empty list if not defined
46+
data_source_folders = self.config.get("data")
47+
# cast as a list if is defined but is a string
48+
if isinstance(data_source_folders, str):
49+
data_source_folders = data_source_folders.split(',')
5150

52-
if data and os.path.exists(data):
53-
path = Path(data)
54-
for filename in chain(path.glob("**/*.yml"), path.glob("**/*.json")):
55-
with open(filename) as f:
56-
namespace = os.path.splitext(os.path.relpath(filename, data))[0]
51+
# if we have not value, then proceed to look in default folders
52+
# and assume a _data folder, add to list of folders to check
53+
if not data_source_folders:
54+
for datadir in [
55+
os.path.dirname(config["config_file_path"]),
56+
config["docs_dir"],
57+
]:
58+
ds_folder = os.path.join(datadir, "_data")
59+
if os.path.exists(ds_folder):
60+
data_source_folders.append(ds_folder)
61+
62+
if not data_source_folders:
63+
return
64+
65+
# iterate of a list of folders and look for data files
66+
for ds_folder in data_source_folders:
67+
if os.path.exists(ds_folder):
68+
path = Path(ds_folder)
69+
for filename in chain(
70+
path.glob("**/*.yaml"),
71+
path.glob("**/*.yml"),
72+
path.glob("**/*.json"),
73+
):
74+
namespace = os.path.splitext(os.path.relpath(filename, ds_folder))[0]
75+
# add data into dict based on its path as a namespace
5776
self.__add_data__(
5877
config,
5978
namespace,
6079
(
61-
yaml.load(f, Loader=yaml.FullLoader)
62-
if filename.suffix == ".yml"
63-
else json.load(f)
80+
yaml.load(filename.read_bytes(), Loader=yaml.FullLoader)
81+
if filename.suffix in [".yml", ".yaml"]
82+
else json.load(filename.read_bytes())
6483
),
6584
)
6685

6786
def on_page_markdown(self, markdown, config, **kwargs):
6887
context = {key: config.get(key) for key in CONFIG_KEYS if key in config}
6988
context.update(config.get("extra", {}))
70-
extra = config.get("extra")
7189
md_template = Template(markdown)
72-
return md_template.render(**extra)
90+
return md_template.render(**config.get("extra"))

‎setup.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def run_tests(self):
2424

2525
setup(
2626
name='mkdocs-markdownextradata-plugin',
27-
version='0.1.2',
27+
version='0.1.3',
2828
description='A MkDocs plugin that injects the mkdocs.yml extra variables into the markdown template',
2929
long_description=read('README.md'),
3030
long_description_content_type='text/markdown',

0 commit comments

Comments
 (0)