Skip to content

Commit ba88bee

Browse files
authored
Update docs and add deprecation warnings
1 parent 2cdc786 commit ba88bee

File tree

9 files changed

+72
-93
lines changed

9 files changed

+72
-93
lines changed

CONTRIBUTING.md

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,18 @@ git tag <version>
4949
git push origin <version>
5050
```
5151

52-
Then manually create a github release to trigger publishing to pypi.
52+
Then manually create a github release to trigger publishing to pypi.
53+
54+
55+
TODO:
56+
57+
Changes:
58+
59+
- `macros` should now be defined _before_ `table-reader` (previously after)
60+
- table-reader will now always search docs_dir _and_ config_dir to find a table file. (deprecated option..)
61+
62+
63+
Upgrading guide
64+
65+
- Remove `base_path` and `search_page_directory` options from the config
66+

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ In your markdown files you can now use:
3838
{{ read_csv('path_to_table.csv') }}
3939
```
4040

41-
Where the path is relative to the location of your project's `mkdocs.yml` file (although you can [change that](https://timvink.github.io/mkdocs-table-reader-plugin/options) to be relative to your `docs/` directory).
41+
Where the path is relative to the location of your project's `mkdocs.yml` file, _or_ your project's `docs/` directory, _or_ the location of your markdown source file (all 3 possible locations will be searched, in that order).
4242

4343
- There are [readers](https://timvink.github.io/mkdocs-table-reader-plugin/readers/) for `.csv`, `.fwf`, `.json`, `.xlsx`, `.yaml` and `.tsv` files. There is also the `read_raw()` reader that will allow you to insert tables (or other content) already in markdown format.
4444

docs/howto/project_structure.md

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,15 @@ If you only want to include an occasional table in a specific markdown file, jus
2626
\{\{ read_csv("another_table.csv") \}\}
2727
```
2828

29-
This works because the [option](../options.md) `search_page_directory` defaults to `True`.
29+
In `page.md`, to read `another_table.csv`, you can choose to use:
30+
31+
- <code>\{\{ read_csv("docs/folder/another_table.csv") \}\}</code> (Path relative to mkdocs.yml)
32+
- <code>\{\{ read_csv("folder/another_table.csv") \}\}</code> (Path relative to docs/ directory)
33+
- <code>\{\{ read_csv("another_table.csv") \}\}</code> (Path relative to page source file)
3034

3135
## Re-using tables across markdown files
3236

33-
If you want to reuse tables in multiple markdown files, you'll want to store them in a central directory, like `docs/assets/tables`.
37+
If you want to reuse tables in multiple markdown files, or have many tables, you'll want to store them in a central directory, like `docs/assets/tables`.
3438
That way, if you restructure your navigation, the links to the tables won't break either.
3539
It's also great if you generate tables because the output directory will be the same.
3640

@@ -45,44 +49,12 @@ Given the following project structure:
4549
│ └── assets/
4650
│ └── tables/
4751
│ └── another_table.csv
48-
│ └── page.md
4952
└── mkdocs.yml
5053
```
5154

52-
In `page.md`, to read `basic_table.csv`, you can choose to use:
53-
54-
- <code>\{\{ read_csv("docs/assets/tables/another_table.csv") \}\}</code> when `base_path` [option](../options.md) is set to `config_dir` (default)
55-
- <code>\{\{ read_csv("assets/tables/another_table.csv") \}\}</code> when `base_path` [option](../options.md) is set to `docs_dir`
56-
- <code>\{\{ read_csv("../another_table.csv") \}\}</code> if you want to use a relative path and `search_page_directory` [option](../options.md) is enabled (default). Note that `..` stands for "one directory up".
57-
58-
## A central table directory combined with same-directory tables
59-
60-
If you have some central tables that you want to reuse, and some tables that are specific to a page, you could use the following project structure:
61-
62-
```nohighlight
63-
.
64-
├── docs/
65-
│ ├── tables/
66-
│ | └── basic_table.csv
67-
│ └── folder/
68-
│ └── another_table.csv
69-
│ └── page.md
70-
└── mkdocs.yml
71-
```
72-
73-
In `page.md`, to read `basic_table.csv`, you can choose to use:
74-
75-
- <code>\{\{ read_csv("docs/tables/basic_table.csv") \}\}</code> when `base_path` [option](../options.md) is set to `config_dir` (default)
76-
- <code>\{\{ read_csv("tables/basic_table.csv") \}\}</code> when `base_path` [option](../options.md) is set to `docs_dir`
77-
- <code>\{\{ read_csv("basic_table.csv") \}\}</code> when:
78-
- `bash_path` [option](../options.md) is set to `config_dir` and `data_path` is set to `docs/tables`, OR
79-
- `bash_path` [option](../options.md) is set to `docs_dir` and `data_path` is set to `tables`
80-
8155
In `page.md`, to read `another_table.csv`, you can choose to use:
8256

83-
- <code>\{\{ read_csv("docs/folder/another_table.csv") \}\}</code> when `base_path` is set to `config_dir` (default)
84-
- <code>\{\{ read_csv("folder/another_table.csv") \}\}</code> when `base_path` is set to `docs_dir`
85-
- <code>\{\{ read_csv("another_table.csv") \}\}</code> when:
86-
- `search_page_directory` [option](../options.md) is enabled (default), OR
87-
- `bash_path` [option](../options.md) is set to `config_dir` and `data_path` is set to `docs/folder`, OR
88-
- `bash_path` [option](../options.md) is set to `docs_dir` and `data_path` is set to `folder`
57+
- <code>\{\{ read_csv("docs/assets/tables/another_table.csv") \}\}</code> (Path relative to mkdocs.yml)
58+
- <code>\{\{ read_csv("assets/tables/another_table.csv") \}\}</code> (Path relative to docs/ directory)
59+
- <code>\{\{ read_csv("../assets/tables/another_table.csv") \}\}</code> (Path relative to page source file _(note that `..` stands for "one directory up")_)
60+

docs/howto/use_jinja2.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Using jinja2
2+
3+
`table-reader` supports [`mkdocs-macros-plugin`](https://mkdocs-macros-plugin.readthedocs.io/en/latest/), which enables you to use jinja2 syntax inside markdown files (among other things).
4+
5+
To enable `macros`, specify the plugin _before_ `table-reader` in your `mkdocs.yml` file:
6+
7+
```yaml
8+
plugins:
9+
- macros
10+
- table-reader
11+
```
12+
13+
Now you can do cool things like dynamically load a list of tables:
14+
15+
16+
```markdown
17+
# index.md
18+
19+
{% set table_names = ["basic_table.csv","basic_table2.csv"] %}
20+
{% for table_name in table_names %}
21+
22+
{{ read_csv(table_name) }}
23+
24+
{% endfor %}
25+
26+
```

docs/options.md

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,63 +10,35 @@ You can customize the plugin by setting options in `mkdocs.yml`. For example:
1010
```yml
1111
plugins:
1212
- table-reader:
13-
base_path: "config_dir"
1413
data_path: "."
15-
search_page_directory: True
1614
allow_missing_files: False
1715
select_readers:
1816
- read_csv
1917
- read_json
2018
enabled: True
2119
```
2220
23-
## `base_path`
24-
25-
The base path where `mkdocs-table-reader-plugin` will search for input files. The path to your table files should be relative to the `base_path`. Allowed values:
26-
27-
- `config_dir` (default): the directory where your project's `mkdocs.yml` file is located.
28-
- `docs_dir`: the directory where your projects' `docs/` folder is located.
29-
30-
If you store your table in `docs/assets/table.csv`, you can insert it in any markdown page using <code>\{\{ read_csv("docs/assets/table.csv") \}\}</code>, or when `base_path` is `docs_dir`, with <code>\{\{ read_csv("assets/table.csv") \}\}</code>.
31-
32-
!!! info
33-
34-
Note that by default the plugin will _also_ search the page's directory but only when a table is not found.
35-
36-
For more examples see the how to guide on [project structure](howto/project_structure.md).
37-
3821
## `data_path`
3922

40-
The path to your table files should be relative to the `base_path`. If you use a folder for all your table files you can shorten the path specification by setting the `data_path`.
23+
Default is `.`. Set a default path to the searched directories in order to shorten table filename specifications.
4124

42-
For example, if your table is located in `docs/tables/basic_table.csv`, you can set `data_path` to `docs/tables/` and leave `base_path` to the default `config_dir`. Then you will be able to use <code>\{\{ read_csv("basic_table.csv") \}\}</code> instead of <code>\{\{ read_csv("docs/tables/basic_table.csv") \}\}</code> inside any markdown page.
25+
Given a file path, `table-reader` will search for that file relative to your your project's `mkdocs.yml` and relative to your `docs/` folder. If you use a folder for all your table files you can shorten the path specification by setting the `data_path`.
4326

44-
Default is `.`, which means you need to specify the path to your table files relative to the `base_path`.
27+
For example, if your table is located in `docs/assets/tables/basic_table.csv`, you can set `data_path` to `docs/assets/tables/`. Then you will be able to use <code>\{\{ read_csv("basic_table.csv") \}\}</code> instead of <code>\{\{ read_csv("docs/assets/tables/basic_table.csv") \}\}</code> inside any markdown page.
4528

4629
!!! info
4730

4831
Note that by default the plugin will _also_ search the page's directory but only when a table is not found.
4932

5033
For more examples see the how to guide on [project structure](howto/project_structure.md).
5134

52-
## `search_page_directory`
53-
54-
Default: `True`. When enabled, if a filepath is not found, the plugin will also attempt to find the file relative to the current page's directory.
55-
56-
!!! info
57-
58-
Note that even when `True`, the path relative to `data_path` is searched first,
59-
and if a file is not found there, then the page's directory is searched.
60-
61-
For more examples see the how to guide on [project structure](howto/project_structure.md).
62-
6335
## `allow_missing_files`
6436

6537
Default: `False`. When enabled, if a filepath is not found, the plugin will raise a warning instead of an error.
6638

6739
## `select_readers`
6840

69-
Default: Selects all available readers. Specify a list of reader to improve documentation build times for large sites.
41+
Default: Selects all available readers. Specify a list of readers to improve documentation build times for very large sites.
7042

7143
## `enabled`
7244

docs/schema.json

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,12 @@
1313
"markdownDescription": "https://timvink.github.io/mkdocs-table-reader-plugin/options/",
1414
"type": "object",
1515
"properties": {
16-
"base_path": {
17-
"title": "The base path where mkdocs-table-reader-plugin will search for input files. The path to your table files should be relative to the base_path.",
18-
"markdownDescription": "https://timvink.github.io/mkdocs-table-reader-plugin/options/#base_path",
19-
"type": "string",
20-
"enum": ["config_dir", "docs_dir"],
21-
"default": "config_dir"
22-
},
2316
"data_path": {
2417
"title": "The path to your table files should be relative to the base_path. If you use a folder for all your table files you can shorten the path specification by setting the data_path.",
2518
"markdownDescription": "https://timvink.github.io/mkdocs-table-reader-plugin/options/#data_path",
2619
"type": "string",
2720
"default": "."
2821
},
29-
"search_page_directory": {
30-
"title": "When enabled, if a filepath is not found, the plugin will also attempt to find the file relative to the current page's directory.",
31-
"markdownDescription": "https://timvink.github.io/mkdocs-table-reader-plugin/options/#search_page_directory",
32-
"type": "boolean",
33-
"default": true
34-
},
3522
"allow_missing_files": {
3623
"title": "When enabled, if a filepath is not found, the plugin will raise a warning instead of an error.",
3724
"markdownDescription": "https://timvink.github.io/mkdocs-table-reader-plugin/options/#allow_missing_files",

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ nav:
1010
- How to:
1111
- howto/customize_tables.md
1212
- howto/preprocess_tables.md
13+
- howto/use_jinja2.md
1314
- howto/project_structure.md
1415
- howto/docker.md
1516
- howto/alternatives.md

mkdocs_table_reader_plugin/plugin.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,21 @@
22
import re
33
import logging
44

5-
from mkdocs.plugins import BasePlugin
5+
from mkdocs.plugins import BasePlugin, get_plugin_logger
66
from mkdocs.config import config_options
77
from mkdocs.exceptions import ConfigurationError
88

99
from mkdocs_table_reader_plugin.safe_eval import parse_argkwarg
1010
from mkdocs_table_reader_plugin.readers import READERS
1111
from mkdocs_table_reader_plugin.markdown import fix_indentation
1212

13-
logger = logging.getLogger("mkdocs.plugins")
13+
logger = get_plugin_logger("table-reader")
14+
1415

1516
class TableReaderPlugin(BasePlugin):
1617

1718
config_scheme = (
18-
("base_path", config_options.Choice(['docs_dir','config_dir'], default="config_dir")),
1919
("data_path", config_options.Type(str, default=".")),
20-
("search_page_directory", config_options.Type(bool, default=True)),
2120
("allow_missing_files", config_options.Type(bool, default=False)),
2221
("select_readers", config_options.ListOfItems(config_options.Choice(list(READERS.keys())), default = list(READERS.keys()))),
2322
)
@@ -32,6 +31,11 @@ def on_config(self, config, **kwargs):
3231
Returns:
3332
Config
3433
"""
34+
if "search_page_directory" in self.config:
35+
logger.warning(f"[table-reader]: The 'search_page_directory' configuration option is deprecated, it will always be searched. Please remove it from your mkdocs.yml.")
36+
if "base_path" in self.config:
37+
logger.warning(f"[table-reader]: The 'base_path' configuration option is deprecated. Both the config_dir and docs_dir will be searched. Please remove it from your mkdocs.yml.")
38+
3539
self.readers = {reader: READERS[reader].set_config_context(mkdocs_config=config, plugin_config=self.config) for reader in self.config.get('select_readers') if reader in self.config.get('select_readers',[])}
3640

3741
plugins = [p for p in config.get("plugins")]
@@ -118,16 +122,14 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
118122
pd_args, pd_kwargs = parse_argkwarg(result[1])
119123

120124
# Load the table
121-
# note we use the first valid file paths,
122-
# where we first search the 'data_path' and then the page's directory.
123125
markdown_table = function(*pd_args, **pd_kwargs)
124-
markdown_table = fix_indentation(leading_spaces, markdown_table)
125-
126+
126127
# Insert markdown table
127128
# By replacing only the first occurrence of the regex pattern
128129
# You might insert multiple CSVs with a single reader like read_csv
129130
# Because of the replacement, the next occurrence will be the first match for .sub() again.
130131
# This is always why when allow_missing_files=True we replaced the input tag.
132+
markdown_table = fix_indentation(leading_spaces, markdown_table)
131133
markdown = tag_pattern.sub(markdown_table, markdown, count=1)
132134

133135
return markdown
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"a","b"
2+
40,73
3+
50,52
4+
531456,80
5+
"name","table2"

0 commit comments

Comments
 (0)