Refactor relationship between repo_url, repo_name, edit_uri

And deprecate RepoURL
This commit is contained in:
Oleh Prypin
2022-08-13 10:33:36 +02:00
parent 9ac7322b00
commit 287a11dc60
3 changed files with 59 additions and 13 deletions

View File

@@ -5,6 +5,7 @@ import os
import sys
import traceback
import typing as t
import warnings
from typing import NamedTuple
from urllib.parse import urlsplit, urlunsplit
@@ -347,12 +348,11 @@ class URL(OptionallyRequired):
class RepoURL(URL):
"""
Repo URL Config Option
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 __init__(self, *args, **kwargs):
warnings.warn(
"RepoURL is no longer used in MkDocs and will be removed.", DeprecationWarning
)
super().__init__(*args, **kwargs)
def post_validation(self, config, key_name):
repo_host = urlsplit(config['repo_url']).netloc.lower()
@@ -385,6 +385,52 @@ class RepoURL(URL):
config['edit_uri'] = edit_uri
class EditURI(Type):
def __init__(self, repo_url_key):
super().__init__(str)
self.repo_url_key = repo_url_key
def post_validation(self, config, key_name):
edit_uri = config.get(key_name)
repo_url = config.get(self.repo_url_key)
if edit_uri is None and repo_url is not None:
repo_host = urlsplit(repo_url).netloc.lower()
if repo_host == 'github.com' or repo_host == 'gitlab.com':
edit_uri = 'edit/master/docs/'
elif repo_host == 'bitbucket.org':
edit_uri = 'src/default/docs/'
# ensure a well-formed edit_uri
if edit_uri and not edit_uri.endswith('/'):
edit_uri += '/'
config[key_name] = edit_uri
class RepoName(Type):
def __init__(self, repo_url_key):
super().__init__(str)
self.repo_url_key = repo_url_key
def post_validation(self, config, key_name):
repo_name = config.get(key_name)
repo_url = config.get(self.repo_url_key)
# derive repo_name from repo_url if unset
if repo_url is not None and repo_name is None:
repo_host = urlsplit(config['repo_url']).netloc.lower()
if repo_host == 'github.com':
repo_name = 'GitHub'
elif repo_host == 'bitbucket.org':
repo_name = 'Bitbucket'
elif repo_host == 'gitlab.com':
repo_name = 'GitLab'
else:
repo_name = repo_host.split('.')[0].title()
config[key_name] = repo_name
class FilesystemObject(Type):
"""
Base class for options that point to filesystem objects.

View File

@@ -63,17 +63,17 @@ class _MkDocsConfig:
True generates nicer URLs, but False is useful if browsing the output on
a filesystem."""
repo_url = config_options.RepoURL()
repo_url = config_options.URL()
"""Specify a link to the project source repo to be included
in the documentation pages."""
repo_name = config_options.Type(str)
repo_name = config_options.RepoName('repo_url')
"""A name to use for the link to the project source repo.
Default, If repo_url is unset then None, otherwise
"GitHub", "Bitbucket" or "GitLab" for known url or Hostname
for unknown urls."""
edit_uri = config_options.Type(str)
edit_uri = config_options.EditURI('repo_url')
"""Specify a URI to the docs dir in the project source repo, relative to the
repo_url. When set, a link directly to the page in the source repo will
be added to the generated HTML. If repo_url is not set also, this option

View File

@@ -419,9 +419,9 @@ class URLTest(TestCase):
class RepoURLTest(TestCase):
class Schema:
repo_url = config_options.RepoURL()
repo_name = config_options.Type(str)
edit_uri = config_options.Type(str)
repo_url = config_options.URL()
repo_name = config_options.RepoName('repo_url')
edit_uri = config_options.EditURI('repo_url')
def test_repo_name_github(self):
config = self.get_config(
@@ -479,7 +479,7 @@ class RepoURLTest(TestCase):
self.Schema,
{'repo_url': "https://launchpad.net/python-tuskarclient"},
)
self.assertEqual(config.get('edit_uri'), '')
self.assertEqual(config.get('edit_uri'), None)
self.assertEqual(config['repo_url'], "https://launchpad.net/python-tuskarclient")
def test_repo_name_custom_and_empty_edit_uri(self):