Skip to content

Commit

Permalink
Merge pull request #8737 from tk0miya/8510_html_logo_url
Browse files Browse the repository at this point in the history
Allow user to use url to reference html logo & favicon
  • Loading branch information
tk0miya authored Jan 24, 2021
2 parents 502c4ee + c50ce99 commit 0e6a2a9
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 25 deletions.
3 changes: 3 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ Incompatible changes
Deprecated
----------

* ``favicon`` and ``logo`` variable in HTML templates
* ``sphinx.directives.patches.CSVTable``
* ``sphinx.directives.patches.ListTable``
* ``sphinx.directives.patches.RSTTable``
Expand All @@ -53,6 +54,8 @@ Features added
the location where the object is defined
* #7784: i18n: The alt text for image is translated by default (without
:confval:`gettext_additional_targets` setting)
* #2018: html: :confval:`html_favicon` and :confval:`html_logo` now accept URL
for the image
* #8070: html search: Support searching for 2characters word
* #7830: Add debug logs for change detection of sources and templates
* #8201: Emit a warning if toctree contains duplicated entries
Expand Down
10 changes: 10 additions & 0 deletions doc/extdev/deprecated.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,16 @@ The following is a list of deprecated interfaces.
- (will be) Removed
- Alternatives

* - ``favicon`` variable in HTML templates
- 4.0
- TBD
- ``favicon_url``

* - ``logo`` variable in HTML templates
- 4.0
- TBD
- ``logo_url``

* - ``sphinx.directives.patches.CSVTable``
- 4.0
- 6.0
Expand Down
28 changes: 26 additions & 2 deletions doc/templating.rst
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,19 @@ in the future.

.. data:: favicon

The path to the HTML favicon in the static path, or ``''``.
The path to the HTML favicon in the static path, or URL to the favicon, or
``''``.

.. deprecated:: 4.0

Recommend to use ``favicon_url`` instead.

.. data:: favicon_url

The relative path to the HTML favicon image from the current document, or
URL to the favicon, or ``''``.

.. versionadded:: 4.0

.. data:: file_suffix

Expand All @@ -297,7 +309,19 @@ in the future.

.. data:: logo

The path to the HTML logo image in the static path, or ``''``.
The path to the HTML logo image in the static path, or URL to the logo, or
``''``.

.. deprecated:: 4.0

Recommend to use ``logo_url`` instead.

.. data:: logo_url

The relative path to the HTML logo image from the current document, or URL
to the logo, or ``''``.

.. versionadded:: 4.0

.. data:: master_doc

Expand Down
20 changes: 13 additions & 7 deletions doc/usage/configuration.rst
Original file line number Diff line number Diff line change
Expand Up @@ -987,26 +987,32 @@ that use Sphinx's HTMLWriter class.
.. confval:: html_logo

If given, this must be the name of an image file (path relative to the
:term:`configuration directory`) that is the logo of the docs. It is placed
at the top of the sidebar; its width should therefore not exceed 200 pixels.
Default: ``None``.
:term:`configuration directory`) that is the logo of the docs, or URL that
points an image file for the logo. It is placed at the top of the sidebar;
its width should therefore not exceed 200 pixels. Default: ``None``.

.. versionadded:: 0.4.1
The image file will be copied to the ``_static`` directory of the output
HTML, but only if the file does not already exist there.

.. versionchanged:: 4.0
Also accepts the URL for the logo file.

.. confval:: html_favicon

If given, this must be the name of an image file (path relative to the
:term:`configuration directory`) that is the favicon of the docs. Modern
browsers use this as the icon for tabs, windows and bookmarks. It should
be a Windows-style icon file (``.ico``), which is 16x16 or 32x32
pixels large. Default: ``None``.
:term:`configuration directory`) that is the favicon of the docs, or URL that
points an image file for the favicon. Modern browsers use this as the icon
for tabs, windows and bookmarks. It should be a Windows-style icon file
(``.ico``), which is 16x16 or 32x32 pixels large. Default: ``None``.

.. versionadded:: 0.4
The image file will be copied to the ``_static`` directory of the output
HTML, but only if the file does not already exist there.

.. versionchanged:: 4.0
Also accepts the URL for the favicon.

.. confval:: html_css_files

A list of CSS files. The entry must be a *filename* string or a tuple
Expand Down
35 changes: 30 additions & 5 deletions sphinx/builders/html/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
from sphinx.locale import _, __
from sphinx.search import js_index
from sphinx.theming import HTMLThemeFactory
from sphinx.util import logging, md5, progress_message, status_iterator
from sphinx.util import isurl, logging, md5, progress_message, status_iterator
from sphinx.util.docutils import is_html5_writer_available, new_document
from sphinx.util.fileutil import copy_asset
from sphinx.util.i18n import format_date
Expand Down Expand Up @@ -792,12 +792,12 @@ def onerror(filename: str, error: Exception) -> None:
excluded, context=context, renderer=self.templates, onerror=onerror)

def copy_html_logo(self) -> None:
if self.config.html_logo:
if self.config.html_logo and not isurl(self.config.html_logo):
copy_asset(path.join(self.confdir, self.config.html_logo),
path.join(self.outdir, '_static'))

def copy_html_favicon(self) -> None:
if self.config.html_favicon:
if self.config.html_favicon and not isurl(self.config.html_favicon):
copy_asset(path.join(self.confdir, self.config.html_favicon),
path.join(self.outdir, '_static'))

Expand Down Expand Up @@ -1155,6 +1155,26 @@ def js_tag(js: JavaScript) -> str:
context['js_tag'] = js_tag


def setup_resource_paths(app: Sphinx, pagename: str, templatename: str,
context: Dict, doctree: Node) -> None:
"""Set up relative resource paths."""
pathto = context.get('pathto')

# favicon_url
favicon = context.get('favicon')
if not isurl(favicon):
context['favicon_url'] = pathto('_static/' + favicon, resource=True)
else:
context['favicon_url'] = favicon

# logo_url
logo = context.get('logo')
if not isurl(logo):
context['logo_url'] = pathto('_static/' + logo, resource=True)
else:
context['logo_url'] = logo


def validate_math_renderer(app: Sphinx) -> None:
if app.builder.format != 'html':
return
Expand Down Expand Up @@ -1195,14 +1215,18 @@ def validate_html_static_path(app: Sphinx, config: Config) -> None:

def validate_html_logo(app: Sphinx, config: Config) -> None:
"""Check html_logo setting."""
if config.html_logo and not path.isfile(path.join(app.confdir, config.html_logo)):
if (config.html_logo and
not path.isfile(path.join(app.confdir, config.html_logo)) and
not isurl(config.html_logo)):
logger.warning(__('logo file %r does not exist'), config.html_logo)
config.html_logo = None # type: ignore


def validate_html_favicon(app: Sphinx, config: Config) -> None:
"""Check html_favicon setting."""
if config.html_favicon and not path.isfile(path.join(app.confdir, config.html_favicon)):
if (config.html_favicon and
not path.isfile(path.join(app.confdir, config.html_favicon)) and
not isurl(config.html_favicon)):
logger.warning(__('favicon file %r does not exist'), config.html_favicon)
config.html_favicon = None # type: ignore

Expand Down Expand Up @@ -1288,6 +1312,7 @@ def setup(app: Sphinx) -> Dict[str, Any]:
app.connect('config-inited', validate_html_favicon, priority=800)
app.connect('builder-inited', validate_math_renderer)
app.connect('html-page-context', setup_js_tag_helper)
app.connect('html-page-context', setup_resource_paths)

# load default math renderer
app.setup_extension('sphinx.ext.mathjax')
Expand Down
4 changes: 2 additions & 2 deletions sphinx/themes/agogo/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
{% block header %}
<div class="header-wrapper" role="banner">
<div class="header">
{%- if logo %}
{%- if logo_url %}
<p class="logo"><a href="{{ pathto(master_doc)|e }}">
<img class="logo" src="{{ pathto('_static/' + logo, 1)|e }}" alt="Logo"/>
<img class="logo" src="{{ logo_url|e }}" alt="Logo"/>
</a></p>
{%- endif %}
{%- block headertitle %}
Expand Down
8 changes: 4 additions & 4 deletions sphinx/themes/basic/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ <h3>{{ _('Navigation') }}</h3>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
{%- block sidebarlogo %}
{%- if logo %}
{%- if logo_url %}
<p class="logo"><a href="{{ pathto(master_doc)|e }}">
<img class="logo" src="{{ pathto('_static/' + logo, 1)|e }}" alt="Logo"/>
<img class="logo" src="{{ logo_url|e }}" alt="Logo"/>
</a></p>
{%- endif %}
{%- endblock %}
Expand Down Expand Up @@ -135,8 +135,8 @@ <h3>{{ _('Navigation') }}</h3>
title="{% trans docstitle=docstitle|e %}Search within {{ docstitle }}{% endtrans %}"
href="{{ pathto('_static/opensearch.xml', 1) }}"/>
{%- endif %}
{%- if favicon %}
<link rel="shortcut icon" href="{{ pathto('_static/' + favicon, 1)|e }}"/>
{%- if favicon_url %}
<link rel="shortcut icon" href="{{ favicon_url|e }}"/>
{%- endif %}
{%- endif %}
{%- block linktags %}
Expand Down
4 changes: 2 additions & 2 deletions sphinx/themes/basic/opensearch.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<Url type="text/html" method="get"
template="{{ use_opensearch }}/{{ pathto('search') }}?q={searchTerms}"/>
<LongName>{{ docstitle|e }}</LongName>
{%- if favicon %}
<Image height="16" width="16" type="image/x-icon">{{ use_opensearch }}/{{ pathto('_static/' + favicon, 1)|e }}</Image>
{%- if favicon_url %}
<Image height="16" width="16" type="image/x-icon">{{ use_opensearch }}/{{ favicon_url|e }}</Image>
{%- endif %}
{% block extra %} {# Put e.g. an <Image> element here. #} {% endblock %}
</OpenSearchDescription>
4 changes: 2 additions & 2 deletions sphinx/themes/haiku/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
{%- block haikuheader %}
{%- if theme_full_logo != "false" %}
<a href="{{ pathto('index') }}">
<img class="logo" src="{{ pathto('_static/' + logo, 1)|e }}" alt="Logo"/>
<img class="logo" src="{{ logo_url|e }}" alt="Logo"/>
</a>
{%- else %}
{%- if logo -%}
<img class="rightlogo" src="{{ pathto('_static/' + logo, 1)|e }}" alt="Logo"/>
<img class="rightlogo" src="{{ logo_url|e }}" alt="Logo"/>
{%- endif -%}
<h1 class="heading"><a href="{{ pathto('index') }}">
<span>{{ shorttitle|e }}</span></a></h1>
Expand Down
2 changes: 1 addition & 1 deletion sphinx/themes/pyramid/layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div class="header" role="banner">
<div class="logo">
<a href="{{ pathto(master_doc)|e }}">
<img class="logo" src="{{ pathto('_static/' + logo, 1)|e }}" alt="Logo"/>
<img class="logo" src="{{ pathto(logo, 1)|e }}" alt="Logo"/>
</a>
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions sphinx/util/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,14 @@ def encode_uri(uri: str) -> str:
return urlunsplit(split)


def isurl(url: str) -> bool:
"""Check *url* is URL or not."""
if url and '://' in url:
return True
else:
return False


def display_chunk(chunk: Any) -> str:
if isinstance(chunk, (list, tuple)):
if len(chunk) == 1:
Expand Down

0 comments on commit 0e6a2a9

Please sign in to comment.