Skip to content

Commit

Permalink
Enable alabaster render Travis CI badges for .com
Browse files Browse the repository at this point in the history
Fixes #145
  • Loading branch information
webknjaz committed Dec 17, 2018
1 parent 7d94bd5 commit 45af7b0
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 8 deletions.
46 changes: 46 additions & 0 deletions alabaster/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import os

try:
import requests
except ImportError:
pass

from alabaster import _version as version


Expand All @@ -14,6 +19,47 @@ 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":
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+
Expand Down
11 changes: 3 additions & 8 deletions alabaster/about.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,11 @@ <h1 class="logo"><a href="{{ pathto(master_doc) }}">{{ project }}</a></h1>
{% 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 %}
<p>
<a class="badge" href="https://travis-ci.org/{{ path }}">
<a class="badge" href="{{ theme_travis_build_url }}">
<img
alt="https://secure.travis-ci.org/{{ path }}.svg?branch={{ theme_badge_branch }}"
src="https://secure.travis-ci.org/{{ path }}.svg?branch={{ theme_badge_branch }}"
alt="{{ theme_travis_badge_url }}"
src="{{ theme_travis_badge_url }}"
/>
</a>
</p>
Expand Down
1 change: 1 addition & 0 deletions alabaster/theme.conf
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ sidebar_width = 220px
tidelift_url =
touch_icon =
travis_button = false
travis_tld = org

gray_1 = #444
gray_2 = #EEE
Expand Down

0 comments on commit 45af7b0

Please sign in to comment.