Skip to content

Commit 333f19c

Browse files
committed
Exposing vhasdoc() and vpathto().
Exposing these two functions in the Jinja2 context for future use by banner messages.
1 parent 8ec3e88 commit 333f19c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

README.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ This project adheres to `Semantic Versioning <http://semver.org/>`_.
4747
Unreleased
4848
----------
4949

50+
Added
51+
* Jinja2 context functions: ``vhasdoc()`` ``vpathto()``
52+
5053
Changed
5154
* Root ref will also be built in its own directory like other versions. All URLs to root ref will point to the one
5255
in that directory instead of the root. More info: https://github.com/Robpol86/sphinxcontrib-versioning/issues/15

docs/context.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,36 @@ branches or just tags.
5454
{%- endfor %}
5555
</dl>
5656
57+
Functions
58+
=========
59+
60+
.. function:: vhasdoc(other_version)
61+
62+
Similar to Sphinx's `hasdoc() <sphinx_hasdoc_>`_ function. Returns True if the current document exists in another
63+
version.
64+
65+
.. code-block:: jinja
66+
67+
{% if vhasdoc('master') %}
68+
This doc is available in <a href="../master/index.html">master</a>.
69+
{% endif %}
70+
71+
.. function:: vpathto(other_version)
72+
73+
Similar to Sphinx's `pathto() <sphinx_pathto_>`_ function. Has two behaviors:
74+
75+
1. If the current document exists in the specified other version pathto() returns the relative URL to that document.
76+
2. If the current document does not exist in the other version the relative URL to that version's
77+
`master_doc <sphinx_master_doc_>`_ is returned instead.
78+
79+
.. code-block:: jinja
80+
81+
{% if vhasdoc('master') %}
82+
This doc is available in <a href="{{ vpathto('master') }}">master</a>.
83+
{% else %}
84+
Go to <a href="{{ vpathto('master') }}">master</a> for the latest docs.
85+
{% endif %}
86+
5787
Other Variables
5888
===============
5989

@@ -101,3 +131,6 @@ Other Variables
101131

102132
.. _Jinja2: http://jinja.pocoo.org/
103133
.. _sphinx_context: http://www.sphinx-doc.org/en/stable/config.html?highlight=context#confval-html_context
134+
.. _sphinx_hasdoc: http://www.sphinx-doc.org/en/stable/templating.html#hasdoc
135+
.. _sphinx_master_doc: http://www.sphinx-doc.org/en/stable/config.html#confval-master_doc
136+
.. _sphinx_pathto: http://www.sphinx-doc.org/en/stable/templating.html#pathto

sphinxcontrib/versioning/sphinx_.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,9 @@ def html_page_context(cls, app, pagename, templatename, context, doctree):
9393
context['scv_is_recent_tag'] = this_remote == versions.recent_tag_remote
9494
context['scv_is_root'] = cls.IS_ROOT
9595
context['scv_is_tag'] = this_remote['kind'] == 'tags'
96-
# context['scv_root_ref_is_branch'] = versions.root_remote['kind'] == 'heads'
97-
# context['scv_root_ref_is_tag'] = versions.root_remote['kind'] == 'tags'
9896
context['versions'] = versions
97+
context['vhasdoc'] = versions.vhasdoc
98+
context['vpathto'] = versions.vpathto
9999

100100

101101
def setup(app):

0 commit comments

Comments
 (0)