Skip to content

Commit 5dcaeca

Browse files
authored
Merge pull request #69 from timvink/add_support_for_indentation_in_macros
Add support for fixing indentation in jinja
2 parents 9f6b419 + 572b702 commit 5dcaeca

File tree

6 files changed

+106
-5
lines changed

6 files changed

+106
-5
lines changed

docs/howto/use_jinja2.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,48 @@ Now you can do cool things like dynamically load a list of tables:
2424
{% endfor %}
2525

2626
```
27+
28+
## Indented content like content tabs
29+
30+
If you inserted content has multiple lines, then indentation will be not be retained beyond the first line. This means things like [content tabs](https://squidfunk.github.io/mkdocs-material/reference/content-tabs/#usage) will not work as expected.
31+
32+
To fix that, you can use the custom _filter_ `add_indendation` (a filter add to `macros` by `table-reader` plugin). For example:
33+
34+
=== "index.md"
35+
36+
```jinja
37+
{% set table_names = ["basic_table.csv","basic_table2.csv"] %}
38+
{% for table_name in table_names %}
39+
40+
=== "{{ table_name }}"
41+
42+
{ { read_csv(table_name) | add_indentation(spaces=4) }}
43+
44+
{% endfor %}
45+
```
46+
47+
=== "mkdocs.yml"
48+
49+
```yaml
50+
site_name: test git_table_reader site
51+
use_directory_urls: true
52+
53+
theme:
54+
name: material
55+
56+
plugins:
57+
- search
58+
- macros
59+
- table-reader
60+
61+
markdown_extensions:
62+
- pymdownx.superfences
63+
- pymdownx.tabbed:
64+
alternate_style: true
65+
```
66+
67+
!!! note "Note the space in { {"
68+
69+
To avoid the tables being inserted into the code example, we replaced `{{` with `{ {`.
70+
If you copy this example, make sure to fix.
71+

mkdocs_table_reader_plugin/markdown.py

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,41 @@ def convert_to_md_table(df: pd.DataFrame, markdown_kwargs: Dict) -> str:
4646
return df.to_markdown(**markdown_kwargs)
4747

4848

49-
def fix_indentation(leading_spaces: str, text: str) -> str:
49+
def add_indentation(text: str, *, spaces: int = 0, tabs: int = 0) -> str:
5050
"""
5151
Adds indentation to a text.
5252
5353
Args:
54-
leading_spaces (str): Indentation to add
54+
spaces (int): Indentation to add in spaces
55+
tabs (int): Indentation to add in tabs
56+
text (str): input text
57+
58+
Returns:
59+
str: fixed text
60+
"""
61+
if spaces and tabs:
62+
raise ValueError("You can only specify either spaces or tabs, not both.")
63+
if spaces:
64+
indentation = " " * spaces
65+
elif tabs:
66+
indentation = "\t" * tabs
67+
else:
68+
indentation = ""
69+
70+
fixed_lines = []
71+
for line in text.split("\n"):
72+
fixed_lines.append(textwrap.indent(line, indentation))
73+
text = "\n" + "\n".join(fixed_lines) + "\n"
74+
75+
return text
76+
77+
78+
def fix_indentation(text: str, *, leading_spaces: str) -> str:
79+
"""
80+
Adds indentation to a text.
81+
82+
Args:
83+
leading_spaces (str): Indentation to add in actual spaces, e.g. " " for 4 spaces
5584
text (str): input text
5685
5786
Returns:

mkdocs_table_reader_plugin/plugin.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from mkdocs_table_reader_plugin.safe_eval import parse_argkwarg
88
from mkdocs_table_reader_plugin.readers import READERS
9-
from mkdocs_table_reader_plugin.markdown import fix_indentation
9+
from mkdocs_table_reader_plugin.markdown import fix_indentation, add_indentation
1010

1111
logger = get_plugin_logger("table-reader")
1212

@@ -73,6 +73,11 @@ def on_config(self, config, **kwargs):
7373
config.plugins["macros"].macros.update(self.readers)
7474
config.plugins["macros"].variables["macros"].update(self.readers)
7575
config.plugins["macros"].env.globals.update(self.readers)
76+
77+
config.plugins["macros"].filters.update({"add_indentation": add_indentation})
78+
config.plugins["macros"].variables["filters"].update({"add_indentation": add_indentation})
79+
config.plugins["macros"].env.filters.update({"add_indentation": add_indentation})
80+
7681
self.external_jinja_engine = True
7782
else:
7883
self.external_jinja_engine = False
@@ -144,7 +149,7 @@ def on_page_markdown(self, markdown, page, config, files, **kwargs):
144149
# You might insert multiple CSVs with a single reader like read_csv
145150
# Because of the replacement, the next occurrence will be the first match for .sub() again.
146151
# This is always why when allow_missing_files=True we replaced the input tag.
147-
markdown_table = fix_indentation(leading_spaces, markdown_table)
152+
markdown_table = fix_indentation(leading_spaces=leading_spaces, text=markdown_table)
148153
markdown = tag_pattern.sub(markdown_table, markdown, count=1)
149154

150155
return markdown

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
setup(
77
name="mkdocs-table-reader-plugin",
8-
version="3.0.0",
8+
version="3.0.1",
99
description="MkDocs plugin to directly insert tables from files into markdown.",
1010
long_description=long_description,
1111
long_description_content_type="text/markdown",

tests/fixtures/jinja/docs/index.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,17 @@ This is a table that we load from the docs folder, because we set `data_path` to
1313
{{ read_csv(table_name) }}
1414

1515
{% endfor %}
16+
17+
18+
## Now with tabs
19+
20+
21+
{% for table_name in table_names %}
22+
23+
=== "{{ table_name }}"
24+
25+
{{ read_csv(table_name) | add_indentation(spaces=4) }}
26+
27+
28+
{% endfor %}
29+

tests/fixtures/jinja/mkdocs.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
site_name: test git_table_reader site
22
use_directory_urls: true
33

4+
theme:
5+
name: material
6+
47
plugins:
58
- search
69
- macros
710
- table-reader
11+
12+
markdown_extensions:
13+
- pymdownx.superfences
14+
- pymdownx.tabbed:
15+
alternate_style: true

0 commit comments

Comments
 (0)