Skip to content

Commit d5f5154

Browse files
committed
Exposing even more variables in Jinja context.
Adding more boolean variables to the context. They'll be needed for future features (e.g. development banner).
1 parent 9768e3e commit d5f5154

5 files changed

Lines changed: 62 additions & 2 deletions

File tree

README.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ Unreleased
4747

4848
Added
4949
* Exposing Jinja2 context variables: ``scv_is_branch`` ``scv_is_root_ref`` ``scv_is_tag`` ``scv_root_ref_is_branch``
50-
``scv_root_ref_is_tag``
50+
``scv_root_ref_is_tag`` ``scv_is_greatest_tag`` ``scv_is_recent_branch`` ``scv_is_recent_ref``
51+
``scv_is_recent_tag``
5152

5253
Changed
5354
* Version links point to that version of the current page if it exists there.

docs/themes.rst

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@ variables are exposed:
2525

2626
A boolean set to True if the current version being built is from a git branch.
2727

28+
.. attribute:: scv_is_greatest_tag
29+
30+
A boolean set to True if the current version being built is:
31+
32+
* From a git tag.
33+
* A valid semver-formatted name (e.g. v1.2.3).
34+
* The highest version number.
35+
36+
.. attribute:: scv_is_recent_branch
37+
38+
A boolean set to True if the current version being built is a git branch and is the most recent commit out of just
39+
git branches.
40+
41+
.. attribute:: scv_is_recent_ref
42+
43+
A boolean set to True if the current version being built is the most recent git commit (branch or tag).
44+
45+
.. attribute:: scv_is_recent_tag
46+
47+
A boolean set to True if the current version being built is a git tag and is the most recent commit out of just git
48+
tags.
49+
2850
.. attribute:: scv_is_root_ref
2951

3052
A boolean set to True if the current version being built is the :option:`--root-ref`.

sphinxcontrib/versioning/sphinx_.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ def html_page_context(cls, app, pagename, templatename, context, doctree):
8585
context['github_version'] = cls.CURRENT_VERSION
8686
context['html_theme'] = app.config.html_theme
8787
context['scv_is_branch'] = this_remote['kind'] == 'heads'
88+
context['scv_is_greatest_tag'] = this_remote == versions.greatest_tag_remote
89+
context['scv_is_recent_branch'] = this_remote == versions.recent_branch_remote
90+
context['scv_is_recent_ref'] = this_remote == versions.recent_remote
91+
context['scv_is_recent_tag'] = this_remote == versions.recent_tag_remote
8892
context['scv_is_root_ref'] = this_remote == versions.root_remote
8993
context['scv_is_tag'] = this_remote['kind'] == 'tags'
9094
context['scv_root_ref_is_branch'] = versions.root_remote['kind'] == 'heads'

sphinxcontrib/versioning/versions.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,10 @@ class Versions(object):
9393
URLs are just '.' initially. Set after instantiation by another function elsewhere. Will be relative URL path.
9494
9595
:ivar iter remotes: List of dicts for every branch/tag.
96+
:ivar dict greatest_tag_remote: Tag with the highest version number if it's a valid semver.
97+
:ivar dict recent_branch_remote: Most recently committed branch.
98+
:ivar dict recent_remote: Most recently committed branch/tag.
99+
:ivar dict recent_tag_remote: Most recently committed tag.
96100
:ivar dict root_remote: Branch/tag at the root of all HTML docs.
97101
"""
98102

@@ -114,6 +118,10 @@ def __init__(self, remotes, sort=None, prioritize=None, invert=False):
114118
found_docs=tuple(), # tuple of str
115119
url='.', # str
116120
) for r in remotes]
121+
self.greatest_tag_remote = None
122+
self.recent_branch_remote = None
123+
self.recent_remote = None
124+
self.recent_tag_remote = None
117125
self.root_remote = None
118126

119127
# Sort one or more times.
@@ -130,6 +138,19 @@ def __init__(self, remotes, sort=None, prioritize=None, invert=False):
130138
if invert:
131139
self.remotes.reverse()
132140

141+
# Get significant remotes.
142+
if self.remotes:
143+
remotes = self.remotes[:]
144+
multi_sort(remotes, ('chrono',))
145+
self.recent_remote = remotes[0]
146+
self.recent_branch_remote = ([r for r in remotes if r['kind'] != 'tags'] or [None])[0]
147+
self.recent_tag_remote = ([r for r in remotes if r['kind'] == 'tags'] or [None])[0]
148+
if self.recent_tag_remote:
149+
multi_sort(remotes, ('semver',))
150+
greatest_tag_remote = [r for r in remotes if r['kind'] == 'tags'][0]
151+
if RE_SEMVER.search(greatest_tag_remote['name']):
152+
self.greatest_tag_remote = greatest_tag_remote
153+
133154
def __bool__(self):
134155
"""True if self.remotes is not empty. Python 3.x."""
135156
return bool(self.remotes)
@@ -209,7 +230,15 @@ def copy(self, sub_depth=0, pagename=None):
209230
else:
210231
remote_new['url'] = '{}.html'.format(pagename)
211232

212-
# Handle root_remote.
233+
# Handle pinned remotes.
234+
if self.greatest_tag_remote == remote_old:
235+
new.greatest_tag_remote = remote_new
236+
if self.recent_branch_remote == remote_old:
237+
new.recent_branch_remote = remote_new
238+
if self.recent_remote == remote_old:
239+
new.recent_remote = remote_new
240+
if self.recent_tag_remote == remote_old:
241+
new.recent_tag_remote = remote_new
213242
if self.root_remote == remote_old:
214243
new.root_remote = remote_new
215244
return new

tests/test_versions.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,10 @@ def test_no_sort(remotes):
3434
assert actual_all == expected_all
3535
assert actual_branches == expected_branches
3636
assert actual_tags == expected_tags
37+
assert versions.greatest_tag_remote == versions['v10.0.0']
38+
assert versions.recent_branch_remote == versions['zh-pages']
39+
assert versions.recent_remote == versions['v2.0.0']
40+
assert versions.recent_tag_remote == versions['v2.0.0']
3741

3842

3943
@pytest.mark.parametrize('sort', ['', 'alpha', 'chrono', 'semver', 'semver,alpha', 'semver,chrono'])

0 commit comments

Comments
 (0)