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

Use canonical URL from html_baseurl #1003

Merged
merged 5 commits into from Dec 3, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/changelog.rst
Expand Up @@ -5,11 +5,17 @@ Changelog
master
======

Other Changes
-------------

* The ``canonical_url`` option was deprecated in favor of Sphinx's ``html_baseurl``.

New Features
------------

* New theme option to enable anonymous ip addresses when using Google Analytics (#889)


v0.5.0
======

Expand Down
12 changes: 7 additions & 5 deletions docs/configuring.rst
Expand Up @@ -13,7 +13,6 @@ For example:
.. code:: python

html_theme_options = {
'canonical_url': '',
'analytics_id': 'UA-XXXXXXX-1', # Provided by Google in your dashboard
'analytics_anonymize_ip': False,
'logo_only': False,
Expand Down Expand Up @@ -119,6 +118,12 @@ Miscellaneous options
documentation is available through. The URL points to the root path of the
documentation and requires a trailing slash.

.. deprecated:: 0.6.0

Use :confval:`sphinx:html_baseurl` instead.

.. _canonical URL: https://en.wikipedia.org/wiki/Canonical_link_element

.. confval:: display_version

:type: boolean
Expand Down Expand Up @@ -165,10 +170,7 @@ Miscellaneous options
:default: ``#2980B9``

Changes the background of the search area in the navigation bar. The value
can be anything valid in a CSS `background` property.

.. _canonical URL: https://en.wikipedia.org/wiki/Canonical_link_element

can be anything valid in a CSS `background` property.

File-wide metadata
==================
Expand Down
24 changes: 21 additions & 3 deletions sphinx_rtd_theme/__init__.py
Expand Up @@ -6,29 +6,47 @@

from os import path

import sphinx
from sphinx import version_info
from sphinx.locale import _

try:
# Avaliable from Sphinx 1.6
from sphinx.util.logging import getLogger
except ImportError:
from logging import getLogger


__version__ = '0.5.0'
__version_full__ = __version__

logger = getLogger(__name__)


def get_html_theme_path():
"""Return list of HTML theme paths."""
cur_dir = path.abspath(path.dirname(path.dirname(__file__)))
return cur_dir


def config_initiated(app, config):
theme_options = config.html_theme_options or {}
if theme_options.get('canonical_url'):
logger.warning(
_('The canonical_url option is deprecated, use the html_baseurl option from Sphinx instead.')
)


# See http://www.sphinx-doc.org/en/stable/theming.html#distribute-your-theme-as-a-python-package
def setup(app):
if sphinx.version_info >= (1, 6, 0):
if version_info >= (1, 6, 0):
# Register the theme that can be referenced without adding a theme path
app.add_html_theme('sphinx_rtd_theme', path.abspath(path.dirname(__file__)))

if sphinx.version_info >= (1, 8, 0):
if version_info >= (1, 8, 0):
# Add Sphinx message catalog for newer versions of Sphinx
# See http://www.sphinx-doc.org/en/master/extdev/appapi.html#sphinx.application.Sphinx.add_message_catalog
rtd_locale_path = path.join(path.abspath(path.dirname(__file__)), 'locale')
app.add_message_catalog('sphinx', rtd_locale_path)
app.connect('config-inited', config_initiated)

return {'parallel_read_safe': True, 'parallel_write_safe': True}
10 changes: 8 additions & 2 deletions sphinx_rtd_theme/layout.html
Expand Up @@ -38,11 +38,17 @@
{% if favicon %}
<link rel="shortcut icon" href="{{ pathto('_static/' + favicon, 1) }}"/>
{% endif %}
{# CANONICAL URL #}
{% if theme_canonical_url %}

{# CANONICAL URL (deprecated) #}
{% if theme_canonical_url and not pageurl %}
<link rel="canonical" href="{{ theme_canonical_url }}{{ pagename }}.html"/>
{% endif %}

{# CANONICAL URL #}
{%- if pageurl %}
<link rel="canonical" href="{{ pageurl|e }}" />
{%- endif %}

{# JAVASCRIPTS #}
{%- block scripts %}
<!--[if lt IE 9]>
Expand Down