Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Moving some template links to Python #672

Merged
merged 1 commit into from
May 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
77 changes: 40 additions & 37 deletions src/pydata_sphinx_theme/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ def update_config(app, env):
)
)

# Add an analytics ID to the site if provided
# Currently only supports the two types of Google Analytics id.
gid = theme_options.get("google_analytics_id")
if gid:
# In this case it is "new-style" google analytics
if "G-" in gid:
gid_js_path = f"https://www.googletagmanager.com/gtag/js?id={gid}"
gid_script = f"""
window.dataLayer = window.dataLayer || [];
function gtag(){{ dataLayer.push(arguments); }}
gtag('js', new Date());
gtag('config', '{gid}');
"""
# In this case it is "old-style" google analytics
else:
gid_js_path = "https://www.google-analytics.com/analytics.js"
gid_script = f"""
window.ga = window.ga || function () {{
(ga.q = ga.q || []).push(arguments) }};
ga.l = +new Date;
ga('create', '{gid}', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
"""

# Link the JS files
app.add_js_file(gid_js_path, loading_method="async")
app.add_js_file(None, body=gid_script)


def update_templates(app, pagename, templatename, context, doctree):
"""Update template names and assets for page build."""
Expand Down Expand Up @@ -88,6 +117,17 @@ def update_templates(app, pagename, templatename, context, doctree):
if theme_css_name in context["css_files"]:
context["css_files"].remove(theme_css_name)

# Add links for favicons in the topbar
for favicon in context.get("theme_favicons", []):
icon_type = Path(favicon["href"]).suffix.strip(".")
# Sphinx will auto-resolve href if it's a local file
app.add_css_file(
favicon["href"],
rel=favicon["rel"],
sizes=favicon["sizes"],
**{"type": f"image/{icon_type}"},
)


def add_toctree_functions(app, pagename, templatename, context, doctree):
"""Add functions so Jinja templates can add toctree objects."""
Expand Down Expand Up @@ -259,46 +299,9 @@ def navbar_align_class():
)
return align_options[align]

def generate_google_analytics_script(id):
"""Handle the two types of google analytics id."""
if id:
if "G-" in id:
script = f"""
<script
async
src='https://www.googletagmanager.com/gtag/js?id={id}'
></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){{ dataLayer.push(arguments); }}
gtag('js', new Date());
gtag('config', '{id}');
</script>
"""
else:
script = f"""
<script
async
src='https://www.google-analytics.com/analytics.js'
></script>
<script>
window.ga = window.ga || function () {{
(ga.q = ga.q || []).push(arguments) }};
ga.l = +new Date;
ga('create', '{id}', 'auto');
ga('set', 'anonymizeIp', true);
ga('send', 'pageview');
</script>
"""
soup = bs(script, "html.parser")
return soup
else:
return ""

context["generate_nav_html"] = generate_nav_html
context["generate_toc_html"] = generate_toc_html
context["navbar_align_class"] = navbar_align_class
context["generate_google_analytics_script"] = generate_google_analytics_script


def _add_collapse_checkboxes(soup):
Expand Down
10 changes: 0 additions & 10 deletions src/pydata_sphinx_theme/theme/pydata_sphinx_theme/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,6 @@
{%- block extrahead %}
<meta name="viewport" content="width=device-width, initial-scale=1" />
<meta name="docsearch:language" content="{{ language }}">
{% for favicon in theme_favicons %}
{% if favicon.href[:4] == 'http'%}
<link rel="{{ favicon.rel }}" sizes="{{ favicon.sizes }}" href="{{ favicon.href }}">
{% else %}
<link rel="{{ favicon.rel }}" sizes="{{ favicon.sizes }}" href="{{ pathto('_static/' + favicon.href, 1) }}">
{% endif %}
{% endfor %}

<!-- Google Analytics -->
{{ generate_google_analytics_script(id=theme_google_analytics_id) }}
{%- endblock %}

{% block body_tag %}
Expand Down
10 changes: 6 additions & 4 deletions tests/test_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -185,14 +185,16 @@ def test_favicons(sphinx_build_factory):

icon_16 = (
'<link href="https://secure.example.com/favicon/favicon-16x16.png" '
'rel="icon" sizes="16x16"/>'
'rel="icon" sizes="16x16" type="image/png">'
)
icon_32 = (
'<link href="_static/favicon-32x32.png" rel="icon" sizes="32x32" '
'type="image/png">'
)
icon_32 = '<link href="_static/favicon-32x32.png" rel="icon" sizes="32x32"/>'
icon_180 = (
'<link href="_static/apple-touch-icon-180x180.png" '
'rel="apple-touch-icon" sizes="180x180"/>'
'rel="apple-touch-icon" sizes="180x180" type="image/png">'
)

assert icon_16 in str(index_html.select("head")[0])
assert icon_32 in str(index_html.select("head")[0])
assert icon_180 in str(index_html.select("head")[0])
Expand Down