Skip to content

Commit

Permalink
Merge pull request #5513 from stsewd/fix-mkdocs-view-links
Browse files Browse the repository at this point in the history
Link to the docdir of the remote repo in non-rtd themes for mkdocs
  • Loading branch information
stsewd authored Mar 21, 2019
2 parents 4263c14 + 6eb162a commit 50cb9a2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
27 changes: 19 additions & 8 deletions readthedocs/builds/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,10 +345,8 @@ def get_github_url(
if not docroot:
return ''

if docroot[0] != '/':
docroot = '/{}'.format(docroot)
if docroot[-1] != '/':
docroot = '{}/'.format(docroot)
# Normalize /docroot/
docroot = '/' + docroot.strip('/') + '/'

if action == 'view':
action_string = 'blob'
Expand All @@ -360,6 +358,10 @@ def get_github_url(
return ''
repo = repo.rstrip('/')

if not filename:
# If there isn't a filename, we don't need a suffix
source_suffix = ''

return GITHUB_URL.format(
user=user,
repo=repo,
Expand All @@ -384,10 +386,8 @@ def get_gitlab_url(
if not docroot:
return ''

if docroot[0] != '/':
docroot = '/{}'.format(docroot)
if docroot[-1] != '/':
docroot = '{}/'.format(docroot)
# Normalize /docroot/
docroot = '/' + docroot.strip('/') + '/'

if action == 'view':
action_string = 'blob'
Expand All @@ -399,6 +399,10 @@ def get_gitlab_url(
return ''
repo = repo.rstrip('/')

if not filename:
# If there isn't a filename, we don't need a suffix
source_suffix = ''

return GITLAB_URL.format(
user=user,
repo=repo,
Expand All @@ -416,11 +420,18 @@ def get_bitbucket_url(self, docroot, filename, source_suffix='.rst'):
if not docroot:
return ''

# Normalize /docroot/
docroot = '/' + docroot.strip('/') + '/'

user, repo = get_bitbucket_username_repo(repo_url)
if not user and not repo:
return ''
repo = repo.rstrip('/')

if not filename:
# If there isn't a filename, we don't need a suffix
source_suffix = ''

return BITBUCKET_URL.format(
user=user,
repo=repo,
Expand Down
4 changes: 1 addition & 3 deletions readthedocs/restapi/views/footer_views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

"""Endpoint to generate footer HTML."""

from django.conf import settings
Expand Down Expand Up @@ -71,7 +69,7 @@ def footer_html(request):
# pylint: disable=too-many-locals
project_slug = request.GET.get('project', None)
version_slug = request.GET.get('version', None)
page_slug = request.GET.get('page', None)
page_slug = request.GET.get('page', '')
theme = request.GET.get('theme', False)
docroot = request.GET.get('docroot', '')
subproject = request.GET.get('subproject', False)
Expand Down
19 changes: 18 additions & 1 deletion readthedocs/rtd_tests/tests/test_repo_parsing.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
from django.test import TestCase

from readthedocs.projects.models import Project
Expand Down Expand Up @@ -34,6 +33,12 @@ def test_github(self):
self.pip.repo = 'https://github.com/user/repo.git.git'
self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo.git/blob/master/docs/file.rst')

self.pip.repo = 'https://github.com/user/repo/'
self.assertEqual(
self.version.get_github_url(docroot='/docs/', filename=''),
'https://github.com/user/repo/blob/master/docs/',
)

def test_github_ssh(self):
self.pip.repo = 'git@github.com:user/repo.git'
self.assertEqual(self.version.get_github_url(docroot='/docs/', filename='file'), 'https://github.com/user/repo/blob/master/docs/file.rst')
Expand Down Expand Up @@ -63,6 +68,12 @@ def test_gitlab(self):
self.pip.repo = 'https://gitlab.com/user/repo.git.git'
self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo.git/blob/master/foo/bar/file.rst')

self.pip.repo = 'https://gitlab.com/user/repo.git'
self.assertEqual(
self.version.get_gitlab_url(docroot='/foo/bar/', filename=''),
'https://gitlab.com/user/repo/blob/master/foo/bar/',
)

def test_gitlab_ssh(self):
self.pip.repo = 'git@gitlab.com:user/repo.git'
self.assertEqual(self.version.get_gitlab_url(docroot='/foo/bar/', filename='file'), 'https://gitlab.com/user/repo/blob/master/foo/bar/file.rst')
Expand Down Expand Up @@ -92,6 +103,12 @@ def test_bitbucket(self):
self.pip.repo = 'https://bitbucket.org/user/repo.git.git'
self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo.git/src/master/foo/bar/file.rst')

self.pip.repo = 'https://bitbucket.org/user/repo/'
self.assertEqual(
self.version.get_bitbucket_url(docroot='/foo/bar/', filename=''),
'https://bitbucket.org/user/repo/src/master/foo/bar/',
)

def test_bitbucket_https(self):
self.pip.repo = 'https://user@bitbucket.org/user/repo.git'
self.assertEqual(self.version.get_bitbucket_url(docroot='/foo/bar/', filename='file'), 'https://bitbucket.org/user/repo/src/master/foo/bar/file.rst')
Expand Down

0 comments on commit 50cb9a2

Please sign in to comment.