Skip to content

Commit 0fefcac

Browse files
committed
feat: Add a "roles" template filter
1 parent d372472 commit 0fefcac

1 file changed

Lines changed: 24 additions & 2 deletions

File tree

src/sphinxnotes/data/render/template.py

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@
1010
from ..utils import Report
1111

1212
if TYPE_CHECKING:
13-
from typing import Any
14-
from sphinx.builders import Builder
13+
from typing import Any, Iterable
1514
from sphinx.application import Sphinx
15+
from sphinx.environment import BuildEnvironment
16+
from sphinx.builders import Builder
1617

1718

1819
@dataclass
@@ -108,6 +109,27 @@ def is_safe_attribute(self, obj, attr, value=None):
108109
return super().is_safe_attribute(obj, attr, value)
109110

110111

112+
def _roles_filter(env: BuildEnvironment):
113+
"""
114+
Fetch artwork picture by ID and install theme to Sphinx's source directory,
115+
return the relative URI of current doc root.
116+
"""
117+
def _filter(value: Iterable[str], role: str) -> Iterable[str]:
118+
"""
119+
A heplfer filter for converting list of string to list of role.
120+
121+
For example::
122+
123+
{{ ["foo", "bar"] | roles("doc") }}
124+
125+
Produces ``[":doc:`foo`", ":doc:`bar`"]``.
126+
"""
127+
return map(lambda x: ':%s:`%s`' % (role, x), value)
128+
return _filter
129+
130+
111131
def setup(app: Sphinx):
112132
app.connect('builder-inited', _JinjaEnv._on_builder_inited)
113133
app.connect('build-finished', _JinjaEnv._on_build_finished)
134+
135+
_JinjaEnv.add_filter('roles', _roles_filter)

0 commit comments

Comments
 (0)