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

Support enabling substitutions by default #976

Open
neongreen-sc opened this issue Oct 15, 2024 · 2 comments
Open

Support enabling substitutions by default #976

neongreen-sc opened this issue Oct 15, 2024 · 2 comments

Comments

@neongreen-sc
Copy link

I'm working on a document where many code blocks need substitutions -- is there any way I could enable them in code blocks by default, and use smth like :nosubstitutions: when I want to turn them off?

@adamtheturtle
Copy link
Owner

This is not currently possible. I support this as an idea though, perhaps with a conf.py setting.

@adamtheturtle
Copy link
Owner

I haven't tested this, but could you do something like this in your conf.py:

from sphinx.directives.code import CodeBlock
from docutils.parsers.rst import directives

class SubstitutionsCodeBlock(CodeBlock):
    """
    Custom CodeBlock that automatically applies the :substitutions: option.
    """
    option_spec = CodeBlock.option_spec.copy()
    option_spec['substitutions'] = directives.flag

    def run(self):
        self.options['substitutions'] = True
        return super().run()

def setup(app):
    app.add_directive('substitutions-code-block', SubstitutedCodeBlock)

Then you can use substitutions-code-block almost everywhere, and code-block when you don't want substitutions.

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

2 participants