Skip to content

Commit 6d9ea52

Browse files
committed
docs: Add custom filter example
1 parent 22c06b5 commit 6d9ea52

5 files changed

Lines changed: 55 additions & 2 deletions

File tree

docs/conf.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,4 @@ def setup(app):
164164
__import__('test-base-context-directive-example.conf').conf.setup(app)
165165
__import__('test-base-data-define-directive-example.conf').conf.setup(app)
166166
__import__('test-strict-data-define-directive-example.conf').conf.setup(app)
167+
__import__('test-filter-example.conf').conf.setup(app)

docs/ext.rst

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,31 @@ The decorated class must be one of the following classes:
9494
9595
.. _ext-filters:
9696

97-
Extending Filters
97+
Extending ilters
9898
=================
9999

100100
Template filters are registered by a
101101
:py:deco:`sphinxnotes.render.filter` function decorator.
102102

103-
TODO
103+
The decorated function takes a :py:class:`sphinx.environment.BuildEnvironment`
104+
as argument and returns a filter function.
105+
106+
.. note::
107+
108+
The decorator is used to **decorate the filter function factory, NOT
109+
the filter function itself**.
110+
111+
.. literalinclude:: ../tests/roots/test-filter-example/conf.py
112+
:language: python
113+
:start-after: [literalinclude start]
114+
:end-before: [literalinclude end]
115+
116+
.. example::
117+
:style: grid
118+
119+
.. data.render::
120+
121+
{{ "Hello world" | catify }}
104122
105123
.. _ext-directives:
106124
.. _ext-roles:
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
from sphinxnotes.render import filter
2+
from sphinx.environment import BuildEnvironment
3+
4+
5+
# [literalinclude start]
6+
@filter('catify')
7+
def catify(_: BuildEnvironment):
8+
"""Speak in a cat-like tone"""
9+
10+
def _filter(value: str) -> str:
11+
return value + ", meow~"
12+
13+
return _filter
14+
# [literalinclude end]
15+
16+
17+
extensions = ['sphinxnotes.render.ext']
18+
19+
20+
def setup(app): ...
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
test filter
2+
============
3+
4+
.. data.render::
5+
6+
{{ "I love you" | catify }}

tests/test_e2e.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@
33
import pytest
44

55

6+
@pytest.mark.sphinx('html', testroot='filter-example')
7+
def test_custom_filter(app, status, warning):
8+
app.build()
9+
10+
html = (app.outdir / 'index.html').read_text(encoding='utf-8')
11+
12+
assert 'I love you, meow~' in html
13+
614
@pytest.mark.sphinx('html', testroot='strict-data-define-directive-example')
715
def test_strict_data_define_directive(app, status, warning):
816
app.build()

0 commit comments

Comments
 (0)