Skip to content

Commit e0846ba

Browse files
committed
More image updates
1 parent df1e7d4 commit e0846ba

File tree

3 files changed

+119
-98
lines changed

3 files changed

+119
-98
lines changed

docs/_data/config-header.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
brand:
2-
type: image
3-
src: https://executablebooks.org/en/latest/_static/logo.svg
2+
type: link
3+
image: https://executablebooks.org/en/latest/_static/logo.svg
44
url: https://sphinx-book-theme.readthedocs.io
55
start:
66
- type: dropdown
@@ -23,8 +23,10 @@ start:
2323
- content: Feature Voting
2424
url: https://executablebooks.org/en/latest/feature-vote.html
2525

26-
- type: button
26+
- type: link
2727
content: Book gallery
28+
external: true
29+
newwindow: true
2830
url: http://gallery.jupyterbook.org/
2931

3032

docs/customize/header.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ html_theme_options = {
6767
"header": {
6868
"brand": {
6969
# Specifies that the brand area will use an image logo
70-
"type": "image",
70+
"type": "link",
7171
# Source of the image to be used
72-
"src": "https://executablebooks.org/en/latest/_static/logo.svg",
72+
"image": "https://executablebooks.org/en/latest/_static/logo.svg",
7373
# Link for the image
7474
"url": "https://sphinx-book-theme.readthedocs.io",
7575
},

src/sphinx_book_theme/_components.py

Lines changed: 112 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,115 @@
1414

1515

1616
# Add functions to render header components
17-
def component_text(app, context, content="", url="", classes=[]):
18-
classes = " ".join(classes)
19-
html = f"<span class='component-text {classes}'>{content}</span>"
20-
if url:
21-
html = f'<a href="{url}" class="link-primary">{html}</a>'
17+
def component_link(
18+
app,
19+
context,
20+
content="",
21+
icon="",
22+
image="",
23+
doc="",
24+
url="",
25+
newwindow=False,
26+
external=False,
27+
classes=[],
28+
):
29+
kwargs = {"class": classes.copy()}
30+
if external is True:
31+
kwargs["target"] = "_blank"
32+
if newwindow is True:
33+
classes.append("external-link")
34+
35+
html = component_button(
36+
app,
37+
context,
38+
content=content,
39+
icon=icon,
40+
image=image,
41+
classes=classes,
42+
url=url,
43+
kwargs=kwargs,
44+
)
45+
return html
46+
47+
48+
def component_dropdown(
49+
app, context, content="", icon="", side="left", items=[], **kwargs
50+
):
51+
# Items to go inside dropdown
52+
dropdown_items = []
53+
for component in items:
54+
# Pop the `button` type in case it was incorrectly given, since we force button
55+
if "type" in component:
56+
component.pop("type")
57+
dropdown_items.append(
58+
component_button(
59+
app,
60+
context,
61+
**component,
62+
)
63+
)
64+
dropdown_items = "\n".join(dropdown_items)
65+
66+
# These control the look of the button
67+
button_classes = []
68+
if content:
69+
button_classes.append("dropdown-toggle")
70+
71+
# Unique ID to trigger the show event
72+
dropdown_id = "menu-dropdown-"
73+
dropdown_id += hashlib.md5(dropdown_items.encode("utf-8")).hexdigest()[:5]
74+
75+
# Generate the button HTML
76+
dropdown_kwargs = {
77+
"data-toggle": "dropdown",
78+
"aria-haspopup": "true",
79+
"aria-expanded": "false",
80+
"type": "button",
81+
}
82+
html_button = component_button(
83+
app,
84+
context,
85+
content=content,
86+
icon=icon,
87+
kwargs=dropdown_kwargs,
88+
classes=button_classes,
89+
button_id=dropdown_id,
90+
**kwargs,
91+
)
92+
93+
dropdown_classes = ["dropdown-menu"]
94+
if side == "right":
95+
dropdown_classes.append("dropdown-menu-right")
96+
dropdown_classes = " ".join(dropdown_classes)
97+
98+
html_dropdown = f"""
99+
<div class="dropdown">
100+
{html_button}
101+
<div class="{dropdown_classes}" aria-labelledby="{dropdown_id}">
102+
{dropdown_items}
103+
</div>
104+
</div>
105+
""" # noqa
106+
return html_dropdown
107+
108+
109+
def component_html(app, context, html=""):
22110
return html
23111

24112

113+
def component_icon_links(app, context, icons, classes=[]):
114+
context = {"theme_icon_links": icons}
115+
# Add the pydata theme icon-links macro as a function we can re-use
116+
return app.builder.templates.render("icon-links.html", context)
117+
118+
25119
def component_button(
26120
app,
27121
context,
28122
content="",
29123
title="",
30124
icon="",
125+
image="",
31126
url="",
32127
onclick="",
33128
button_id="",
@@ -39,13 +134,13 @@ def component_button(
39134
):
40135
kwargs = kwargs.copy()
41136
kwargs.update({"type": "button", "class": ["btn", "icon-button"]})
42-
kwargs["class"].extend(classes)
137+
kwargs["class"].extend(classes.copy())
43138
btn_content = ""
44139
if url and onclick:
45140
raise Exception("Button component cannot have both url and onclick specified.")
46141

47-
if not (icon or content):
48-
raise Exception("Button must have either icon or content specified.")
142+
if not (icon or content or image):
143+
raise Exception("Button must have either icon, content, or image specified.")
49144

50145
if onclick:
51146
kwargs["onclick"] = onclick
@@ -62,6 +157,13 @@ def component_button(
62157
icon = f'<img src="{icon}">'
63158
btn_content += f'<span class="btn__icon-container">{icon}</span>'
64159

160+
if image:
161+
if not image.startswith("http"):
162+
image = context["pathto"](image, 1)
163+
btn_content += f"""
164+
<span class="btn__image-container"><img src="{image}" /></span>
165+
"""
166+
65167
if not content:
66168
kwargs["class"].append("icon-button-no-content")
67169
else:
@@ -116,93 +218,10 @@ def component_button(
116218
return html
117219

118220

119-
def component_dropdown(
120-
app, context, content="", icon="", side="left", items=[], **kwargs
121-
):
122-
# Items to go inside dropdown
123-
dropdown_items = []
124-
for component in items:
125-
# Pop the `button` type in case it was incorrectly given, since we force button
126-
if "type" in component:
127-
component.pop("type")
128-
dropdown_items.append(
129-
component_button(
130-
app,
131-
context,
132-
**component,
133-
)
134-
)
135-
dropdown_items = "\n".join(dropdown_items)
136-
137-
# These control the look of the button
138-
button_classes = []
139-
if content:
140-
button_classes.append("dropdown-toggle")
141-
142-
# Unique ID to trigger the show event
143-
dropdown_id = "menu-dropdown-"
144-
dropdown_id += hashlib.md5(dropdown_items.encode("utf-8")).hexdigest()[:5]
145-
146-
# Generate the button HTML
147-
dropdown_kwargs = {
148-
"data-toggle": "dropdown",
149-
"aria-haspopup": "true",
150-
"aria-expanded": "false",
151-
"type": "button",
152-
}
153-
html_button = component_button(
154-
app,
155-
context,
156-
content=content,
157-
icon=icon,
158-
kwargs=dropdown_kwargs,
159-
classes=button_classes,
160-
button_id=dropdown_id,
161-
**kwargs,
162-
)
163-
164-
dropdown_classes = ["dropdown-menu"]
165-
if side == "right":
166-
dropdown_classes.append("dropdown-menu-right")
167-
dropdown_classes = " ".join(dropdown_classes)
168-
169-
html_dropdown = f"""
170-
<div class="dropdown">
171-
{html_button}
172-
<div class="{dropdown_classes}" aria-labelledby="{dropdown_id}">
173-
{dropdown_items}
174-
</div>
175-
</div>
176-
""" # noqa
177-
return html_dropdown
178-
179-
180-
def component_image(app, context, src="", url="", classes=[]):
181-
if not src.startswith("http"):
182-
src = context["pathto"](src, 1)
183-
html = f"""
184-
<img src={src}>
185-
"""
186-
if url:
187-
html = f"<a href={url}>{html}</a>"
188-
return html
189-
190-
191-
def component_html(app, context, html=""):
192-
return html
193-
194-
195-
def component_icon_links(app, context, icons, classes=[]):
196-
context = {"theme_icon_links": icons}
197-
# Add the pydata theme icon-links macro as a function we can re-use
198-
return app.builder.templates.render("icon-links.html", context)
199-
200-
201221
COMPONENT_FUNCS = {
202-
"text": component_text,
222+
"link": component_link,
203223
"button": component_button,
204-
"html": component_html,
205-
"image": component_image,
206224
"icon-links": component_icon_links,
207225
"dropdown": component_dropdown,
226+
"html": component_html,
208227
}

0 commit comments

Comments
 (0)