Skip to content

Instantly share code, notes, and snippets.

@tk0miya
Created November 8, 2012 02:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save tk0miya/4036201 to your computer and use it in GitHub Desktop.
Save tk0miya/4036201 to your computer and use it in GitHub Desktop.
sphinxcontrib_section_autoref
===================
Example of autoref
===================
Section 1
==========
You can write reference to section title without any definitions:
:ref:`Section 1`
:ref:`Section 2`
:ref:`index/Section 1`
Section 2
==========
Hello world
# -*- coding: utf-8 -*-
"""
sphinxcontrib_section_autoref
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Add link-target labels to every sections automatically.
You can make reference :ref:`section-name` without label defintions.
And more, labels which are similar to wiki-names (cf. index/secition-name) are
added in same time.
:copyright: Copyright 2012 by Takeshi Komiya.
:license: BSDL.
"""
import os.path
from docutils import nodes, transforms
from docutils.nodes import fully_normalize_name as normalize_name
class AutoReferenceTransform(transforms.Transform):
application = None
default_priority = 500
def apply(self):
filename = os.path.relpath(self.document['source'], self.application.env.srcdir)
docname = os.path.splitext(filename)[0]
for target in self.document.traverse(nodes.section):
# append global link-target including docname (cf. index/section-name)
name = docname + '/' + normalize_name(target[0][0])
target['names'].insert(0, name)
self.document.note_explicit_target(target)
def setup(app):
AutoReferenceTransform.application = app
app.add_transform(AutoReferenceTransform)
@TBBle
Copy link

TBBle commented Nov 15, 2017

If you're looking at this, sphinx.ext.autosectionlabel is included with Sphinx 1.4 onwards. The version here has issues with duplicate IDs when the same section name is used more than once in a document; the Sphinx version also sees issues with duplicate labels in this case.

@donatelloOo
Copy link

Well.. so ?
Can we resolve this issue by putting the full file path instead of just the name, and add a incremented suffix to the labels that already exists ?
Or do we want to get stuck forever ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment