Skip to content

Commit

Permalink
Enable alabaster render Travis CI badges for .com
Browse files Browse the repository at this point in the history
  • Loading branch information
webknjaz committed Dec 17, 2018
1 parent 7d94bd5 commit bc86b80
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 8 deletions.
44 changes: 44 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,45 @@ 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 bc86b80

Please sign in to comment.