Skip to content

Error when returning a nodes.section in a custom directive #13294

Answered by AA-Turner
loichuder asked this question in Q&A
Discussion options

You must be logged in to vote

I would suggest the following:

from docutils import nodes
from sphinx.util.docutils import SphinxDirective


class TestDirective(SphinxDirective):
    def run(self):
        new_section = nodes.section()
        new_section += nodes.title('Title', 'Title')
        self.state.document.note_implicit_target(new_section, new_section)
        return [new_section]


def setup(app):
    app.add_directive('test', TestDirective)

Note note_implicit_target, which both creates an internal reference target to your section, and sets the node ID.

You may find the relevant Docutils code useful here, as this is what happens for the Docutils parser to create a section:

https://github.com/docutils/docutils/…

Replies: 2 comments 4 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
4 replies
@loichuder
Comment options

@AA-Turner
Comment options

Answer selected by loichuder
@chrisjsewell
Comment options

@loichuder
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants