Skip to content

Commit 42fcaec

Browse files
committed
Enable alabaster render Travis CI badges for .com
Fixes #145
1 parent 7d94bd5 commit 42fcaec

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

alabaster/__init__.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import os
22

3+
try:
4+
import requests
5+
except ImportError:
6+
pass
7+
38
from alabaster import _version as version
49

510

@@ -14,6 +19,47 @@ def get_path():
1419
def update_context(app, pagename, templatename, context, doctree):
1520
context["alabaster_version"] = version.__version__
1621

22+
set_up_travis_context(context)
23+
24+
25+
def set_up_travis_context(context):
26+
"""Add complete Travis URLs to Jinja2 context."""
27+
github_slug = "/".join(
28+
(context["theme_github_user"], context["theme_github_repo"])
29+
)
30+
31+
travis_button = str(context["theme_travis_button"]).lower()
32+
travis_button_enabled = travis_button == "true"
33+
34+
travis_slug = github_slug if travis_button_enabled else travis_button
35+
36+
travis_tld = context["theme_travis_tld"].lower()
37+
if travis_button_enabled and travis_tld == "auto":
38+
try:
39+
travis_api_response = requests.get(
40+
"https://api.travis-ci.com/repo/{}".format(
41+
travis_slug.replace("/", "%2F")
42+
),
43+
headers={
44+
"Travis-API-Version": "3",
45+
"User-Agent": "Sphinx-Alabaster-Theme/{version} "
46+
"(+https://github.com/bitprophet/alabaster)".format(
47+
version=version.__version__
48+
),
49+
},
50+
)
51+
is_travis_com_repo = 200 <= travis_api_response.status_code < 300
52+
travis_tld = "com" if is_travis_com_repo else "org"
53+
except NameError:
54+
travis_tld = "com"
55+
elif travis_tld != "com":
56+
travis_tld = "org"
57+
travis_base_uri = "travis-ci.{}/{}".format(travis_tld, travis_slug)
58+
context["theme_travis_build_url"] = "https://{}".format(travis_base_uri)
59+
context["theme_travis_badge_url"] = "https://api.{}.svg?branch={}".format(
60+
travis_base_uri, context["theme_badge_branch"]
61+
)
62+
1763

1864
def setup(app):
1965
# add_html_theme is new in Sphinx 1.6+

alabaster/about.html

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,16 +25,11 @@ <h1 class="logo"><a href="{{ pathto(master_doc) }}">{{ project }}</a></h1>
2525
{% endif %}
2626

2727
{% if theme_travis_button|lower != 'false' %}
28-
{% if theme_travis_button|lower == 'true' %}
29-
{% set path = theme_github_user + '/' + theme_github_repo %}
30-
{% else %}
31-
{% set path = theme_travis_button %}
32-
{% endif %}
3328
<p>
34-
<a class="badge" href="https://travis-ci.org/{{ path }}">
29+
<a class="badge" href="{{ theme_travis_build_url }}">
3530
<img
36-
alt="https://secure.travis-ci.org/{{ path }}.svg?branch={{ theme_badge_branch }}"
37-
src="https://secure.travis-ci.org/{{ path }}.svg?branch={{ theme_badge_branch }}"
31+
alt="{{ theme_travis_badge_url }}"
32+
src="{{ theme_travis_badge_url }}"
3833
/>
3934
</a>
4035
</p>

alabaster/theme.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ sidebar_width = 220px
3939
tidelift_url =
4040
touch_icon =
4141
travis_button = false
42+
travis_tld = org
4243

4344
gray_1 = #444
4445
gray_2 = #EEE

0 commit comments

Comments
 (0)