From 75cfb9f93b3560ff412cae4de3029157c14b258f Mon Sep 17 00:00:00 2001
From: John Dickinson <me@not.mn>
Date: Fri, 6 May 2016 16:49:48 -0700
Subject: [PATCH] add recent versions links to sidebar

The last five tags (that start with a number) are used as version
numbers and linked to. This updates the sphinx extension so that
every user of the theme gets this info automatically with no need
to update the conf.py on a per-project basis.

Change-Id: I9bf17872bd23bed3cab9b5e2349d615c7001cb66
---
 oslosphinx/__init__.py                 | 15 +++++++++++++++
 oslosphinx/theme/openstack/layout.html | 10 ++++++++++
 2 files changed, 25 insertions(+)

diff --git a/oslosphinx/__init__.py b/oslosphinx/__init__.py
index 77f962f..4613c89 100644
--- a/oslosphinx/__init__.py
+++ b/oslosphinx/__init__.py
@@ -16,6 +16,7 @@ import os
 import re
 import six
 from six.moves.urllib import parse
+import string
 import subprocess
 
 
@@ -42,6 +43,20 @@ def _guess_cgit_link():
 def _html_page_context(app, pagename, templatename, context, doctree):
     # Insert the cgit link into the template context.
     context['cgit_link'] = app.config.oslosphinx_cgit_link
+
+    git_cmd = ["git", "tag"]
+    try:
+        raw_version_list = subprocess.Popen(
+            git_cmd, stdout=subprocess.PIPE).communicate()[0]
+    except OSError:
+        app.warn('Cannot get tags from git repository. '
+                 'Not setting "other_versions".')
+        raw_version_list = ''
+
+    # grab last five that start with a number and reverse the order
+    other_versions = [t for t in raw_version_list.split('\n')
+                      if t and t[0] in string.digits][:-6:-1]
+    context['other_versions'] = other_versions
     return None
 
 
diff --git a/oslosphinx/theme/openstack/layout.html b/oslosphinx/theme/openstack/layout.html
index 512ab3f..ab8e7db 100644
--- a/oslosphinx/theme/openstack/layout.html
+++ b/oslosphinx/theme/openstack/layout.html
@@ -30,6 +30,16 @@
                                   title="{{ _('next chapter') }}">{{ next.title }}</a></p>
           {%- endif %}
           {%- endblock %}
+          {%- block otherversions %}
+            {%- if other_versions %}
+              <h3>{{ _('Other Versions') }}</h3>
+              <ul class="this-page-menu">
+              {%- for ver in other_versions: %}
+                  <li><a href="/{{ver}}" rel="nofollow">{{ ver }}</a></li>
+              {%- endfor %}
+              </ul>
+            {%- endif %}
+          {%- endblock %}
           {%- block projectsource %}
             {%- if cgit_link %}
             <h3>{{ _('Project Source') }}</h3>