Properly validate edit_uri config setting (#1273)

Account for edit_uri being None. And move handling to config validation rather than 
revalidating/reformatting on each page in the nav.
This commit is contained in:
Frank Sachsenheim
2017-09-08 21:23:44 +02:00
committed by Waylan Limberg
parent 74660b8254
commit 31de2edc37
5 changed files with 39 additions and 22 deletions

View File

@@ -1,5 +1,6 @@
from mkdocs.config.base import load_config
from mkdocs.config.config_options import Config
from mkdocs.config.base import load_config, Config
from mkdocs.config.defaults import DEFAULT_SCHEMA
__all__ = ["load_config", 'Config', 'DEFAULT_SCHEMA']
__all__ = [load_config.__name__,
Config.__name__,
'DEFAULT_SCHEMA']

View File

@@ -212,15 +212,16 @@ class RepoURL(URL):
"""
Repo URL Config Option
A small extension to the URL config that sets the repo_name, based on the
url if it hasn't already been provided.
A small extension to the URL config that sets the repo_name and edit_uri,
based on the url if they haven't already been provided.
"""
def post_validation(self, config, key_name):
repo_host = utils.urlparse(config['repo_url']).netloc.lower()
edit_uri = config.get('edit_uri')
# derive repo_name from repo_url if unset
if config['repo_url'] is not None and config.get('repo_name') is None:
repo_host = utils.urlparse(
config['repo_url']).netloc.lower()
if repo_host == 'github.com':
config['repo_name'] = 'GitHub'
elif repo_host == 'bitbucket.org':
@@ -228,11 +229,24 @@ class RepoURL(URL):
else:
config['repo_name'] = repo_host.split('.')[0].title()
if config['repo_url'] is not None and config.get('edit_uri') is None:
if config['repo_name'].lower() == 'github':
config['edit_uri'] = 'edit/master/docs/'
elif config['repo_name'].lower() == 'bitbucket':
config['edit_uri'] = 'src/default/docs/'
# derive edit_uri from repo_name if unset
if config['repo_url'] is not None and edit_uri is None:
if repo_host == 'github.com':
edit_uri = 'edit/master/docs/'
elif repo_host == 'bitbucket.org':
edit_uri = 'src/default/docs/'
else:
edit_uri = ''
# ensure a well-formed edit_uri
if edit_uri:
if not edit_uri.startswith(('?', '#')) \
and not config['repo_url'].endswith('/'):
edit_uri = '/' + edit_uri
if not edit_uri.endswith('/'):
edit_uri += '/'
config['edit_uri'] = edit_uri
class Dir(Type):

View File

@@ -204,17 +204,11 @@ class Page(object):
self.canonical_url = utils.urljoin(base, self.abs_url.lstrip('/'))
def set_edit_url(self, repo_url, edit_uri):
if not repo_url.endswith('/'):
# Skip when using query or fragment in edit_uri
if not edit_uri.startswith('?') and not edit_uri.startswith('#'):
repo_url += '/'
if not edit_uri:
self.edit_url = repo_url
else:
# Normalize URL from Windows path '\\' -> '/'
input_path_url = self.input_path.replace('\\', '/')
if not edit_uri.endswith('/'):
edit_uri += '/'
self.edit_url = utils.urljoin(
repo_url,
edit_uri + input_path_url)

View File

@@ -197,7 +197,7 @@ class RepoURLTest(unittest.TestCase):
option = config_options.RepoURL()
config = {'repo_url': "https://github.com/mkdocs/mkdocs"}
option.post_validation(config, 'repo_url')
self.assertEqual(config['edit_uri'], 'edit/master/docs/')
self.assertEqual(config['edit_uri'], '/edit/master/docs/')
def test_edit_uri_bitbucket(self):
@@ -211,7 +211,15 @@ class RepoURLTest(unittest.TestCase):
option = config_options.RepoURL()
config = {'repo_url': "https://launchpad.net/python-tuskarclient"}
option.post_validation(config, 'repo_url')
self.assertEqual(config.get('edit_uri'), None)
self.assertEqual(config.get('edit_uri'), '')
def test_repo_name_custom_and_empty_edit_uri(self):
option = config_options.RepoURL()
config = {'repo_url': "https://github.com/mkdocs/mkdocs",
'repo_name': 'mkdocs'}
option.post_validation(config, 'repo_url')
self.assertEqual(config.get('edit_uri'), '/edit/master/docs/')
class DirTest(unittest.TestCase):

View File

@@ -642,7 +642,7 @@ class TestLegacyPagesConfig(unittest.TestCase):
# Ensure the '/' is added to the repo_url and edit_uri
repo_url = 'http://example.com'
edit_uri = 'edit/master/docs'
edit_uri = 'edit/master/docs/'
site_navigation = nav.SiteNavigation(pages)
@@ -715,7 +715,7 @@ class TestLegacyPagesConfig(unittest.TestCase):
# Ensure the '/' is added to the repo_url and edit_uri
repo_url = 'http://example.com'
edit_uri = 'edit/master/docs'
edit_uri = 'edit/master/docs/'
site_navigation = nav.SiteNavigation(pages)