Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom/New filters #15

Open
vancheese opened this issue Sep 28, 2020 · 4 comments
Open

Custom/New filters #15

vancheese opened this issue Sep 28, 2020 · 4 comments

Comments

@vancheese
Copy link

Is it possible to add custom/new filters? If so, where/how would you define them in conf.py

@vancheese
Copy link
Author

https://stackoverflow.com/questions/10632232/adding-a-custom-filter-to-jinja2-under-pyramid suggests


from jinja2 import Environment

#Define a new filter
def GetBitValue(num,place):
    y = (num >> (place-1)) & 1
    return y

env = Environment()
env.filters['getbitvalue'] = GetBitValue

This approach seems to be quite valid but where should this be placed so that sphinx know about it...
(the conf.py doesn't work)

@flying-sheep
Copy link

Not really, but you can hack it in:

from functools import wraps
import sphinx.ext.autosummary.generate as gen
import sphinxcontrib.jinja
from jinja2 import Environment

class AutodocEnv(Environment):
    @wraps(Environment.__init__)
    def __init__(self, *args, **kw):
        super().__init__(*args, **kw)
        from sphinx.util import rst
        self.filters['escape'] = rst.escape
        self.filters['e'] = rst.escape
        self.filters['underline'] = gen._underline

sphinxcontrib.jinja.Environment = AutodocEnv

@vancheese
Copy link
Author

vancheese commented Oct 1, 2020

Wooh, awesome reply
I solved the problem by adding a custom filter dictionary in the conf file and then adding it during the templating process
See it on my fork - https://github.com/vancheese/sphinx-jinja

@tardyp
Copy link
Owner

tardyp commented Oct 1, 2020

@vancheese I'd be happy to include in the main repo and release a new version. Please submit a PR! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants