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

Move project-specific extensions into a dedicated directory / package #16

Closed
5 tasks done
Mischback opened this issue Dec 6, 2022 · 0 comments · Fixed by #9
Closed
5 tasks done

Move project-specific extensions into a dedicated directory / package #16

Mischback opened this issue Dec 6, 2022 · 0 comments · Fixed by #9
Assignees
Labels
area/repository Affects the repository structure area/sphinx Related to sphinx lang/python type/improvement Improvement of an existing feature
Milestone

Comments

@Mischback
Copy link
Owner

Mischback commented Dec 6, 2022

Currently, Sphinx's configuration file conf.py acts like one of the project's extensions.

While this works reasonably well for small use-cases (see #5), it is not the desired result.

To Do:

Sphinx's most basic tutorial places the (custom) extension in a folder _ext in the source directory.

From my understanding, the _ext directory may be placed anywhere. It has to be added to Python's PATH. See the section on extending sys.path.

Namespacing might be an issue, so make sure that the project's dedicated extensions don't conflict with other existing extensions (or other packages).

Steps

  • create [REPO_ROOT]/extensions/mischback
  • add [REPO_ROOT]/extensions to Python path
    • in conf.py:
      import sys
      
      # add the project-specific extensions directory to Python's path
      sys.path.append(join(REPO_ROOT, "extensions"))
  • create [REPO_ROOT]/extensions/mischback/sphinx_jinja2_debug.py:
    def activate_jinja2_debug_ext(app):
        """Activate Jinja2 debug extension.
    
        This function is intended to be connected to ``Sphinx``'s
        ``builder-inited`` event (see
        https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx-core-events)
        and will then navigate from the app to the ``jinja.Environment`` and call
        its ``add_extension()`` method.
        """
        if hasattr(app.builder, "templates"):
            app.builder.templates.environment.add_extension("jinja2.ext.debug")
    
    def setup(app):
        """Register the extension with Sphinx.
    
        This function is required by ``Sphinx``'s extension interface, see
        https://www.sphinx-doc.org/en/master/development/tutorials/helloworld.html#writing-the-extension
        for reference.
    
        It connects this plugins (only) function with the ``"builder-inited"`` event, see
        https://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx-core-events
        for reference.
        """
        app.connect("builder-inited", activate_jinja2_debug_ext)
  • use the new extension
    • in conf.py:
      extensions = [
          # ...
          "mischback.sphinx_jinja2_debug",
      ]
  • make the extensions setup() function return the (desired) dictionary (see dry-coding here)
@Mischback Mischback added area/repository Affects the repository structure lang/python area/sphinx Related to sphinx type/improvement Improvement of an existing feature labels Dec 6, 2022
@Mischback Mischback added this to the Crawl milestone Dec 6, 2022
@Mischback Mischback self-assigned this Dec 6, 2022
Mischback added a commit that referenced this issue Dec 9, 2022
- close #3
- close #4
- close #5
- close #6
- close #14
- close #16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/repository Affects the repository structure area/sphinx Related to sphinx lang/python type/improvement Improvement of an existing feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant