From 0423af2e6083000712069105c04814b01a21008a Mon Sep 17 00:00:00 2001 From: Sviatoslav Sydorenko Date: Mon, 17 Dec 2018 16:07:44 +0100 Subject: [PATCH] Enable alabaster render Travis CI badges for .com Fixes #145 --- alabaster/__init__.py | 47 +++++++++++++++++++++++++++++++++++++++++++ alabaster/about.html | 17 ++++++---------- alabaster/theme.conf | 1 + 3 files changed, 54 insertions(+), 11 deletions(-) diff --git a/alabaster/__init__.py b/alabaster/__init__.py index c88b2b5..bfe5d0d 100644 --- a/alabaster/__init__.py +++ b/alabaster/__init__.py @@ -1,5 +1,10 @@ import os +try: + import requests +except ImportError: + pass + from alabaster import _version as version @@ -14,6 +19,48 @@ def get_path(): def update_context(app, pagename, templatename, context, doctree): context["alabaster_version"] = version.__version__ + set_up_travis_context(context) + + +def set_up_travis_context(context): + """Add complete Travis URLs to Jinja2 context.""" + github_slug = "/".join(( + context["theme_github_user"], context["theme_github_repo"], + )) + + travis_button = str(context["theme_travis_button"]).lower() + travis_button_enabled = travis_button == "true" + + travis_slug = github_slug if travis_button_enabled else travis_button + + travis_tld = context["theme_travis_tld"].lower() + if travis_button_enabled and travis_tld == "auto": + # detect + try: + travis_api_response = requests.get( + "https://api.travis-ci.com/repo/{}". + format(travis_slug.replace("/", "%2F")), + headers={ + "Travis-API-Version": "3", + "User-Agent": + "Sphinx-Alabaster-Theme/{version} " + "(+https://github.com/bitprophet/alabaster)". + format(version=version.__version__), + }, + ) + is_travis_com_repo = 200 <= travis_api_response.status_code < 300 + travis_tld = 'com' if is_travis_com_repo else 'org' + except NameError: + travis_tld = 'com' + elif travis_tld != 'com': + travis_tld = 'org' + travis_base_uri = 'travis-ci.{}/{}'.format(travis_tld, travis_slug) + context["theme_travis_build_url"] = 'https://{}'.format(travis_base_uri) + context["theme_travis_badge_url"] = 'https://api.{}.svg?branch={}'.format( + travis_base_uri, + context["theme_badge_branch"], + ) + def setup(app): # add_html_theme is new in Sphinx 1.6+ diff --git a/alabaster/about.html b/alabaster/about.html index dbb4262..39b8c00 100644 --- a/alabaster/about.html +++ b/alabaster/about.html @@ -24,24 +24,19 @@

{{ project }}

{% endif %} {% endif %} -{% if theme_travis_button|lower != 'false' %} -{% if theme_travis_button|lower == 'true' %} - {% set path = theme_github_user + '/' + theme_github_repo %} -{% else %} - {% set path = theme_travis_button %} -{% endif %} +{% if theme_travis_button != 'false' %}

- + https://secure.travis-ci.org/{{ path }}.svg?branch={{ theme_badge_branch }}

{% endif %} -{% if theme_codecov_button|lower != 'false' %} -{% if theme_codecov_button|lower == 'true' %} +{% if theme_codecov_button != 'false' %} +{% if theme_codecov_button == 'true' %} {% set path = theme_github_user + '/' + theme_github_repo %} {% else %} {% set path = theme_codecov_button %} diff --git a/alabaster/theme.conf b/alabaster/theme.conf index b6cdc8d..c810c69 100644 --- a/alabaster/theme.conf +++ b/alabaster/theme.conf @@ -39,6 +39,7 @@ sidebar_width = 220px tidelift_url = touch_icon = travis_button = false +travis_tld = org gray_1 = #444 gray_2 = #EEE