File tree Expand file tree Collapse file tree
roots/test-filter-example Expand file tree Collapse file tree Original file line number Diff line number Diff 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 )
Original file line number Diff line number Diff 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
100100Template 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 :
Original file line number Diff line number Diff line change 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 ): ...
Original file line number Diff line number Diff line change 1+ test filter
2+ ============
3+
4+ .. data.render::
5+
6+ {{ "I love you" | catify }}
Original file line number Diff line number Diff line change 33import 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' )
715def test_strict_data_define_directive (app , status , warning ):
816 app .build ()
You can’t perform that action at this time.
0 commit comments