public
Last active

sphinx: doc reference by using substitution and custome 'doc' inline directive. inspired by https://bitbucket.org/birkenfeld/sphinx/issue/1077

  • Download Gist
conf.py
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
############################################
# 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)
index.html
HTML
1
<p>This is link to <a class="reference internal" href="foo.html"><em>The Foo</em></a> by using substitution.</p>
index.txt
1 2 3
This is link to |The Foo| by using substitution.
 
.. |The Foo| doc:: foo

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.