Skip to content

Commit bde0d25

Browse files
committed
Enhance documentation with new filter examples
1 parent f1eeb3d commit bde0d25

File tree

5 files changed

+70
-10
lines changed

5 files changed

+70
-10
lines changed

.github/workflows/publish.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212
- name: Set up Python
1313
uses: actions/setup-python@v5
1414
with:
15-
python-version: 3.12
15+
python-version: 3.13
1616
- name: Install dependencies
1717
run: |
18-
poetry install --only main
18+
poetry install --only main,docs
1919
- name: Build Documentation
2020
run: |
2121
poetry run python -m docs

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,34 @@ Example:
148148
{% pdoc pathlib:Path.open:code.dedent %}
149149
```
150150

151+
## Filter
152+
153+
Filter to use in `jinja2` template.
154+
155+
### include
156+
157+
`Environment.include` - returns the content of the file.
158+
159+
```jinja
160+
{{ "path/to/file" | include(enc="utf-8") }}
161+
```
162+
163+
### shell
164+
165+
`Environment.shell` - run shell command and return the selected result from `subprocess.CompletedProcess`.
166+
167+
```jinja
168+
{{ "python --version" | shell(promt=">>> %s\n") }}
169+
```
170+
171+
### strip
172+
173+
`Environment.strip` - remove leading and trailing whitespace and newlines from a string.
174+
175+
```jinja
176+
{{ "path/to/file" | include | strip }}
177+
```
178+
151179
## Command Line Interface
152180

153181
```cmd
@@ -192,8 +220,6 @@ $ jinja2pdoc .\examples\*.jinja2
192220

193221
## pre-commit-config
194222

195-
**Per default the hook is not registered to `files`!**
196-
197223
To render all template files from `docs` using `.pre-commit-config.yaml` add the following.
198224

199225
You may add a `frontmatter` section at the beginning of in your templates to specify output directory and filename, e.g. `<!--filename: example.md-->`. If no metadata are at the beginning of the template, the rendered file is written to the `output` directory which is default the current working direktory.
@@ -213,6 +239,8 @@ Use [`additional_dependencies`](https://pre-commit.com/#config-additional_depend
213239

214240
## pre-commit-hooks
215241

242+
**Per default the hook is not registered to `files`!**
243+
216244
```yaml
217245
- id: jinja2pdoc
218246
name: render jinja2pdoc

docs/README.md.jinja2

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ output of the [python code](#python) above.
4444

4545
{{ "docs/syntax.md" | include }}
4646

47+
## Filter
48+
49+
{{ "docs/filter.md" | include }}
50+
4751
## Command Line Interface
4852

4953
```cmd
@@ -56,8 +60,6 @@ output of the [python code](#python) above.
5660

5761
## pre-commit-config
5862

59-
**Per default the hook is not registered to `files`!**
60-
6163
To render all template files from `docs` using `.pre-commit-config.yaml` add the following.
6264

6365
You may add a `frontmatter` section at the beginning of in your templates to specify output directory and filename, e.g. `<!--filename: example.md-->`. If no metadata are at the beginning of the template, the rendered file is written to the `output` directory which is default the current working direktory.
@@ -72,6 +74,8 @@ Use [`additional_dependencies`](https://pre-commit.com/#config-additional_depend
7274

7375
## pre-commit-hooks
7476

77+
**Per default the hook is not registered to `files`!**
78+
7579
```yaml
7680
{{ ".pre-commit-hooks.yaml" | include }}
7781
```

docs/filter.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
Filter to use in `jinja2` template.
2+
3+
### include
4+
5+
`Environment.include` - returns the content of the file.
6+
7+
```jinja
8+
{{ "path/to/file" | include(enc="utf-8") }}
9+
```
10+
11+
### shell
12+
13+
`Environment.shell` - run shell command and return the selected result from `subprocess.CompletedProcess`.
14+
15+
```jinja
16+
{{ "python --version" | shell(promt=">>> %s\n") }}
17+
```
18+
19+
### strip
20+
21+
`Environment.strip` - remove leading and trailing whitespace and newlines from a string.
22+
23+
```jinja
24+
{{ "path/to/file" | include | strip }}
25+
```

jinja2_pdoc/environment.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -115,11 +115,9 @@ def include(
115115
str: The content of the file.
116116
117117
Example:
118-
````jinja2
119-
```yaml
118+
```jinja2
120119
{{ ".pre-commit-hool.yaml" | include }}
121120
```
122-
````
123121
"""
124122

125123
content = Path(file).read_text(encoding=enc)
@@ -137,9 +135,14 @@ def strip(text: str, chars: str = "\n ") -> str:
137135
Args:
138136
text (str): The text to be stripped.
139137
chars (str, optional): The characters to be stripped from the text.
140-
Defaults to `"\n "`.
138+
Defaults to `"\\n "`.
141139
142140
Returns:
143141
str: The stripped text.
142+
143+
Example:
144+
```jinja2
145+
{{ " hello world. \\n" | strip }}
146+
```
144147
"""
145148
return text.strip(chars)

0 commit comments

Comments
 (0)