diff --git a/mkdocs/config/__init__.py b/mkdocs/config/__init__.py index f88280b6..3f8314f3 100644 --- a/mkdocs/config/__init__.py +++ b/mkdocs/config/__init__.py @@ -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'] diff --git a/mkdocs/config/config_options.py b/mkdocs/config/config_options.py index 0126f91d..c2f8b99b 100644 --- a/mkdocs/config/config_options.py +++ b/mkdocs/config/config_options.py @@ -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): diff --git a/mkdocs/nav.py b/mkdocs/nav.py index a0fa030c..55d45a9f 100644 --- a/mkdocs/nav.py +++ b/mkdocs/nav.py @@ -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) diff --git a/mkdocs/tests/config/config_options_tests.py b/mkdocs/tests/config/config_options_tests.py index 99e6d981..39978307 100644 --- a/mkdocs/tests/config/config_options_tests.py +++ b/mkdocs/tests/config/config_options_tests.py @@ -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): diff --git a/mkdocs/tests/nav_tests.py b/mkdocs/tests/nav_tests.py index 7cab5e34..48ebe249 100644 --- a/mkdocs/tests/nav_tests.py +++ b/mkdocs/tests/nav_tests.py @@ -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)