1

On a readthedocs / Sphinx project, I need to display current_version in rst file and to generate link with the current_version.

Screenshot:

screenshot

I've seen it in templates (versions.html):

v: {{ current_version }}

Is it possible to access {{ current_version }} in rst ? I've tried

|current_version|

But the result is

WARNING: Undefined substitution referenced: "current_version".

Precisions :

it work with |version| which is declared in conf.py. but I don't know how to display current_version.

Edit 2 :

My point was not clear sorry. In a readthedocs project, I've got several branch of documentation. In my rst files, I need to know the branch to build special links http://xxx.xx/**branch**/ because I've to store data in another repository. In the conf.py, there is the version field but, I want to get the branch automatically. And I saw that in the RTD template, in file version.html, there is {{ current_version }} which is displayed in the screenshot.

bad_coder
  • 11,289
  • 20
  • 44
  • 72
CyrilJ
  • 11
  • 2
  • thx, it answers a part of the question : how to use a variable in rst. But it doesn't work with current_version – CyrilJ Feb 16 '22 at 13:33
  • Do you have any reference for the `current_version` you mention? Because it's not in the Sphinx nor in the RTD documentation! – bad_coder Feb 16 '22 at 13:56
  • I've put a screenshot. I didn't found documenation. It's in the RTD theme in version.html. – CyrilJ Feb 16 '22 at 14:51
  • If you can't provide a link to the source code there's no way for us to know what that `current_version` is because it's not included in any of the official documentation, please edit the question to include a [*"Minimal, Reproducible, Example."*](https://stackoverflow.com/help/minimal-reproducible-example) – bad_coder Feb 16 '22 at 14:54
  • 1
    @CyrilJ good job editing the post, I'm voting to reopen. – bad_coder Feb 17 '22 at 12:08

1 Answers1

0

Read the Docs injects extra html_context when building (see https://github.com/readthedocs/readthedocs.org/blob/725e21813313ce67ca0048d72fcfdce019ee8182/readthedocs/doc_builder/templates/doc_builder/conf.py.tmpl#L75-L125). That extra context contains the current_version variable you are looking for.

This context is passed to the template engine. So, from jinja2 template engine, you could use {{ current_version }}.

However, take into account that this does not work locally because your local build of Sphinx won't be injecting this extra context. If you want to emulate what Read the Docs docs to test this locally, you can add the following to your conf.py:

html_context = {
 'current_version': 'my-current-version',
}

and build the documentation locally to check its behavior.

Also, take a look at this FAQ entry in the Read the Docs' official documentation: https://docs.readthedocs.io/en/stable/faq.html#how-do-i-change-behavior-when-building-with-read-the-docs

Manuel Kaufmann
  • 346
  • 1
  • 7