From c123e027a5d2f235488f6ee7e66c68df1331e08d Mon Sep 17 00:00:00 2001
From: Christoph Groth <christoph.groth@cea.fr>
Date: Mon, 13 Aug 2018 17:14:10 +0200
Subject: [PATCH] links to source: get refname from version string

So it's no longer necessary to provide the REFNAME environment variable.
Also the link template string format is now more general to support at
least gitlab github and cgit.
---
 .gitlab-ci.yml     |  2 +-
 RELEASE.rst        |  2 +-
 doc/source/conf.py | 23 +++++++++++++++++------
 3 files changed, 19 insertions(+), 8 deletions(-)

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index 03f95826..5de6fad8 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -38,7 +38,7 @@ check for dependencies installed:
 build documentation:
   stage: test
   script:
-    - make -C doc realclean; make -C doc html SPHINXOPTS='-A website_deploy=True -n -W' REFNAME="${CI_COMMIT_TAG:-$CI_COMMIT_SHA}" SOURCE_URL="$CI_PROJECT_URL"/blob
+    - make -C doc realclean; make -C doc html SPHINXOPTS='-A website_deploy=True -n -W' SOURCE_LINK_TEMPLATE="$CI_PROJECT_URL"/blob/$$r/$$f
   artifacts:
     paths:
       - doc/build/html/
diff --git a/RELEASE.rst b/RELEASE.rst
index 12074d7a..468b8fcd 100644
--- a/RELEASE.rst
+++ b/RELEASE.rst
@@ -112,7 +112,7 @@ Building the documentation requires 'sphinx' and a Latex installation.
 First build the HTML and PDF documentation::
 
     ./setup.py build
-    make -C doc realclean html latex SPHINXOPTS='-A website_deploy=True -n -W' REFNAME="<version>"
+    make -C doc realclean html latex SPHINXOPTS='-A website_deploy=True -n -W'
     cd doc/build/latex
     make all-pdf
 
diff --git a/doc/source/conf.py b/doc/source/conf.py
index d2688e87..90c1cd7f 100644
--- a/doc/source/conf.py
+++ b/doc/source/conf.py
@@ -11,8 +11,9 @@
 # All configuration values have a default; values that are commented out
 # serve to show the default.
 
-import sys, os
-
+import sys
+import os
+import string
 from distutils.util import get_platform
 sys.path.insert(0, "../../build/lib.{0}-{1}.{2}".format(
         get_platform(), *sys.version_info[:2]))
@@ -298,8 +299,18 @@ def linkcode_resolve(domain, info):
         filename = 'kwant/%s#L%d-%d' % find_source()
     except Exception:
         filename = info['module'].replace('.', '/') + '.py'
-    source_branch = os.environ.get('REFNAME', 'master')
-    url = os.environ.get('SOURCE_URL',
-                         "https://gitlab.kwant-project.org/kwant/kwant/blob")
 
-    return "{}/{}/{}".format(url, source_branch, filename)
+    # The following relies on the documented format of kwant.__version__.
+    rel = release.rstrip('.dirty')
+    if '.dev' in rel:
+        try:
+            refname = rel[rel.index('+g') + 2:]
+        except ValueError:
+            return
+    else:
+        refname = 'v' + rel
+
+    templ = os.environ.get(
+        "SOURCE_LINK_TEMPLATE",
+        "https://gitlab.kwant-project.org/kwant/kwant/blob/$r/$f")
+    return string.Template(templ).safe_substitute(r=refname, f=filename)
-- 
GitLab