Skip to content

Commit fa5570b

Browse files
Refactor font loading (#597)
* Refactor font loading We now only provide a mechanism for *loading* fonts from Google. To *set* a font, the user needs to provide custom CSS. The documentation gives an example. * Fix Hero font --------- Co-authored-by: Jarrod Millman <jarrod.millman@gmail.com>
1 parent 0bb187f commit fa5570b

10 files changed

Lines changed: 67 additions & 27 deletions

File tree

assets/theme-css/hero.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
align-items: center;
3232
display: flex;
3333
flex-direction: row;
34-
font-family: var(--fontFamily), sans-serif;
34+
font-family: var(--pst-font-family-heading), sans-serif;
3535
font-weight: 900;
3636
font-size: 6em;
3737
color: var(--pst-color-text-base);

assets/theme-css/vars.css

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
/* prettier-ignore */
22
:root {
3-
--fontFamily: "{{ .Site.Params.font.name }}", "Noto Sans Symbols 2";
4-
53
--spht-color-dark: rgb(1, 50, 67);
64
--spht-color-light: rgb(255, 255, 255);
75
--spht-color-link-hover: rgb(77, 171, 207);

config.yaml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@ params:
77
images:
88
- /images/logo.svg
99
navColor: blue
10-
font:
11-
name: "Lato"
12-
sizes: [400, 900]
1310
plausible:
1411
dataDomain: null
1512
javaScript: "https://views.scientific-python.org/js/script.js"

doc/assets/css/custom.css

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
.hero-title,
12
.navbar-logo-text {
23
font-family: "Lato";
34
}

doc/config.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ params:
2525
image: logo.svg
2626
text: Scientific Python Hugo Theme
2727
link: /
28+
fonts:
29+
- name: "Lato"
30+
weights: [400, 900]
2831
hero:
2932
title: Scientific Python Theme
3033
image: logo.svg

doc/content/user_guide/styling.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@ Custom styles should be added to `/assets/css/my_css.css` (where
1212

1313
## CSS theme variables
1414

15+
## Fonts
16+
17+
To override the fonts used, add a custom CSS file and set the following variables:
18+
19+
```css
20+
// General text
21+
:root {
22+
--pst-font-family-base: "Open Sans";
23+
--pst-font-family-heading: "Open Sans";
24+
--pst-font-family-monospace: "Open Sans";
25+
}
26+
27+
// Navigation bar
28+
.navbar-logo-text {
29+
font-family: "Open Sans";
30+
}
31+
```
32+
33+
After setting the font, we still need to ensure that it is loaded.
34+
If you are happy loading your fonts from Google Fonts, add them to your `config.yaml`:
35+
36+
```yaml
37+
params:
38+
fonts:
39+
- name: "Lato"
40+
weights: [400, 900]
41+
```
42+
43+
Alternatively, create `layouts/partials/head.html` and add what you need to load the font, e.g.:
44+
45+
```html
46+
<link
47+
rel="stylesheet"
48+
href="https://fonts.googleapis.com/css2?family=Lato:wght@400;900"
49+
/>
50+
```
51+
1552
### Color variables
1653

1754
We mostly rely on PyData Sphinx Theme's

layouts/_default/baseof.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
<!-- Render theme css -->
99
{{ partial "css.html" . -}}
1010

11+
<!-- Add MathJax, if needed -->
12+
{{ partial "mathjax.html" }}
13+
1114
<!-- Render custom header -->
1215
{{ partial "head.html" . -}}
1316

layouts/partials/css.html

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,19 @@
77
{{- $cssOpts := collections.Merge $sassCompiler $serverOpts -}}
88

99
<!-- Fonts -->
10-
{{- $fontName := .Site.Params.font.name | default "Open Sans" -}}
11-
{{- $fontFace := replace $fontName " " "+" -}}
12-
{{- $fontSizes := delimit (.Site.Params.font.sizes | default (slice 300 400 600 700)) ";" -}}
13-
{{- $fontUrl := printf "https://fonts.googleapis.com/css2?family=%s:wght@%s" $fontFace $fontSizes -}}
14-
15-
<link rel="icon" href="{{ "images/favicon.ico" | relURL }}" />
16-
10+
{{ with $fonts := .Site.Params.fonts -}}
1711
<link rel="preconnect" href="https://fonts.googleapis.com">
1812
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
13+
14+
{{ range $fonts }}
15+
{{- $fontFace := replace .name " " "+" -}}
16+
{{- $fontSizes := delimit (.weights | default (slice 300 400 600 700)) ";" -}}
17+
{{- $fontUrl := printf "https://fonts.googleapis.com/css2?family=%s:wght@%s" $fontFace $fontSizes -}}
1918
<link rel="stylesheet" href="{{ $fontUrl }}">
19+
{{- end }}
20+
{{- end }}
21+
22+
<link rel="icon" href="{{ "images/favicon.ico" | relURL }}" />
2023

2124
<!-- Fallback font for symbols (such as "🛈"). --
2225
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+Symbols+2">

layouts/partials/head.html

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +0,0 @@
1-
{{ if (findRE `(\$.*\$)|(\\\\\[.*\\\\\])|(\\\\\(.*\\\\\))` .Content) }}
2-
<script>
3-
MathJax = {
4-
tex: {
5-
displayMath: [['$$', '$$'], ['\\[', '\\]']],
6-
inlineMath: [['$', '$'], ['\\(', '\\)']],
7-
}
8-
};
9-
</script>
10-
<script type="text/javascript" id="MathJax-script" async
11-
src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
12-
</script>
13-
{{ end }}
14-
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Fira+Sans">

layouts/partials/mathjax.html

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{{ if (findRE `(\$.*\$)|(\\\\\[.*\\\\\])|(\\\\\(.*\\\\\))` .Content) }}
2+
<script>
3+
MathJax = {
4+
tex: {
5+
displayMath: [['$$', '$$'], ['\\[', '\\]']],
6+
inlineMath: [['$', '$'], ['\\(', '\\)']],
7+
}
8+
};
9+
</script>
10+
<script type="text/javascript" id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml.js">
11+
</script>
12+
{{ end }}

0 commit comments

Comments
 (0)