Skip to content

Instantly share code, notes, and snippets.

@shimizukawa
Last active December 11, 2015 04:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shimizukawa/4544948 to your computer and use it in GitHub Desktop.
Save shimizukawa/4544948 to your computer and use it in GitHub Desktop.
sphinx: doc reference by using substitution and custome 'doc' inline directive. inspired by https://bitbucket.org/birkenfeld/sphinx/issue/1077
############################################
# inline doc directive for substitution
# rst: .. |sub| doc:: docname
# html: <a href="docname.html">sub</a>
from sphinx.util.compat import Directive
from sphinx.roles import XRefRole
class DocDirective(Directive):
required_arguments = 1
def run(self):
docname = self.arguments[0]
text = self.state.parent['names'][0]
rawtext = "%s <%s>" % (text, docname)
return XRefRole(warn_dangling=True)(
'doc', rawtext, rawtext, self.state.parent.line, self.state)[0]
def setup(app):
app.add_directive('doc', DocDirective)
<p>This is link to <a class="reference internal" href="foo.html"><em>The Foo</em></a> by using substitution.</p>
This is link to |The Foo| by using substitution.
.. |The Foo| doc:: foo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment