Skip to content

Commit 55bcda3

Browse files
authored
Merge pull request #157 from Point72/tkp/styled-version-title
Restore the contrasting version in the docs title
2 parents 4aa5ce1 + a167048 commit 55bcda3

2 files changed

Lines changed: 23 additions & 3 deletions

File tree

‎yardang/conf.py.j2‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ from pathlib import Path
2121
from packaging.version import Version
2222

2323
from yardang.markdown import copy_relative_html_assets, rebase_relative_references
24+
from yardang.utils import HtmlTitle
2425

2526
########################
2627
# COMMON CONFIGURATION #
@@ -34,9 +35,12 @@ copyright = """{{copyright}}"""
3435
title = """{{title}}"""
3536
version = "{{version}}"
3637
release = "{{version}}"
37-
# Sphinx reuses this as the <title> tag, so it has to stay plain text; markup
38-
# here shows up escaped in browser tabs and search results.
38+
# Sphinx type-checks this as a plain str, so the styled form goes into the page
39+
# context instead (see run_title_fallback).
3940
html_title = """{{title}} v{{version}}"""
41+
styled_html_title = HtmlTitle(
42+
"""{{title}} <code style='font-size: var(--font-size--small--4);color: var(--sd-color-primary);'>v{{version}}</code>"""
43+
)
4044
docs_host_root = "{{docs_root}}"
4145
root = "{{root}}"
4246
cname = "{{ cname or '' }}"
@@ -425,6 +429,10 @@ def run_title_fallback(app, pagename, templatename, context, doctree):
425429
# root page reading "<no title>".
426430
if context.get("title") in (None, "", "&lt;no title&gt;", "<no title>"):
427431
context["title"] = title
432+
# furo's search page is the one template that drops docstitle into <title>
433+
# unfiltered, so it keeps the plain form.
434+
if pagename != "search":
435+
context["docstitle"] = styled_html_title
428436

429437

430438
def setup(app):

‎yardang/utils.py‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,21 @@
11
import os
2+
import re
23
from pathlib import Path
34

45
import toml
56

6-
__all__ = ("get_config", "get_config_flex")
7+
__all__ = ("HtmlTitle", "get_config", "get_config_flex")
8+
9+
10+
class HtmlTitle(str):
11+
"""A title carrying markup that degrades to plain text for the ``<title>`` tag.
12+
13+
Themes render ``docstitle`` into the header verbatim but escape it for the
14+
browser tab; ``__html__`` is what the ``|e`` and ``|striptags`` filters pick up.
15+
"""
16+
17+
def __html__(self):
18+
return re.sub(r"<[^>]+>", "", self)
719

820

921
def get_pyproject_toml():

0 commit comments

Comments
 (0)