Skip to content

Commit 6cb4b02

Browse files
Refactor tatbs (#579)
Use toml to parallel grids and move styling to partial so it can be reused.
1 parent 4cd3745 commit 6cb4b02

File tree

4 files changed

+55
-48
lines changed

4 files changed

+55
-48
lines changed

doc/content/user_guide/web-components.md

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -171,26 +171,32 @@ Code example from the PyData Sphinx Theme's kitchen sink:
171171

172172
{{< tabs >}}
173173

174-
{{< tab "c++" >}}
174+
[[tab]]
175+
name = 'c++'
176+
content = '''
175177

176178
```c++
177179
int main(const int argc, const char **argv) {
178180
return 0;
179181
}
180182
```
181183
182-
{{< /tab >}}
184+
'''
183185
184-
{{< tab "python" >}}
186+
[[tab]]
187+
name = 'python'
188+
content = '''
185189
186190
```python
187191
def main():
188192
return
189193
```
190194

191-
{{< /tab >}}
195+
'''
192196

193-
{{< tab "java" >}}
197+
[[tab]]
198+
name = 'java'
199+
content = '''
194200

195201
```java
196202
class Main {
@@ -199,33 +205,39 @@ class Main {
199205
}
200206
```
201207

202-
{{< /tab >}}
208+
'''
203209

204-
{{< tab "julia" >}}
210+
[[tab]]
211+
name = 'julia'
212+
content = '''
205213

206214
```julia
207215
function main()
208216
end
209217
```
210218

211-
{{< /tab >}}
219+
'''
212220

213-
{{< tab "fortran" >}}
221+
[[tab]]
222+
name ='fortran'
223+
content = '''
214224

215225
```fortran
216226
PROGRAM main
217227
END PROGRAM main
218228
```
219229

220-
{{< /tab >}}
230+
'''
221231

222232
{{< /tabs >}}
223233

224234
An example with headings
225235

226236
{{< tabs >}}
227237

228-
{{< tab "First example" >}}
238+
[[tab]]
239+
name = 'First example'
240+
content = '''
229241

230242
### How would this work?
231243

@@ -244,12 +256,13 @@ def foo(x):
244256
#### What now?
245257

246258
Well, this is a good question.
259+
'''
247260

248-
{{< /tab >}}
249-
250-
{{< tab "Second example" >}}
261+
[[tab]]
262+
name = 'Second example'
263+
content = '''
251264
Another hello world
252-
{{< /tab >}}
265+
'''
253266

254267
{{< /tabs >}}
255268

layouts/partials/_elements/tabs.html

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{{- $groupId := .groupId }}
2+
{{- $tabs := .tabs }}
3+
<div class="tabs">
4+
<div role="tablist" class="automatic">
5+
{{- range $idx, $tab := $tabs }}
6+
<button id="{{ $groupId }}-tab-{{ $idx }}" type="button" role="tab" aria-selected="{{ cond (eq $idx 0) "true" "false" }}" aria-controls="{{ $groupId }}-tabpanel-{{ $idx }}">
7+
{{ .name }}
8+
</button>
9+
{{- end }}
10+
</div>
11+
{{- range $idx, $tab := $tabs }}
12+
<div id="{{ $groupId }}-tabpanel-{{ $idx }}" role="tabpanel" tabindex="0" aria-labelledby="${{ $groupId }}-tab-{{ $idx }}">
13+
{{- .content | markdownify -}}
14+
</div>
15+
{{- end }}
16+
</div>

layouts/shortcodes/tab.html

Lines changed: 0 additions & 11 deletions
This file was deleted.

layouts/shortcodes/tabs.html

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,44 +4,33 @@
44

55
{{< tabs >}}
66

7-
{{< tab "c++" >}}
7+
[[tab]]
8+
name = 'c++'
9+
content = '''
810
An example program in C++:
911
```c++
1012
int main(const int argc, const char **argv) {
1113
return 0;
1214
}
1315
```
1416
It simply returns 0, indicating no errors.
15-
{{< /tab >}}
17+
'''
1618

17-
{{< tab "python" >}}
19+
[[tab]]
20+
name = 'python'
21+
content = '''
1822
An example program in Python:
1923
```python
2024
def main():
2125
return True
2226
```
2327
It returns `True`, indicating no errors.
24-
{{< /tab >}}
28+
'''
2529

2630
{{< /tabs >}}
2731

2832
*/}}
2933

30-
<!-- Render inner tabs, which adds them to a scratch variable for this parent shortcode -->
31-
{{- .Inner -}}
32-
{{- $groupId := .Page.Scratch.Get "tabgroup" | default 0 -}}
33-
{{- .Page.Scratch.Set "tabgroup" (add $groupId 1) -}}
34-
<div class="tabs">
35-
<div role="tablist" class="automatic">
36-
{{- range $idx, $tab := .Scratch.Get "tabs" }}
37-
<button id="{{ $groupId }}-tab-{{ $idx }}" type="button" role="tab" aria-selected="{{ cond (eq $idx 0) "true" "false" }}" aria-controls="{{ $groupId }}-tabpanel-{{ $idx }}">
38-
{{ .name }}
39-
</button>
40-
{{- end }}
41-
</div>
42-
{{- range $idx, $tab := .Scratch.Get "tabs" }}
43-
<div id="{{ $groupId }}-tabpanel-{{ $idx }}" role="tabpanel" tabindex="0" aria-labelledby="${{ $groupId }}-tab-{{ $idx }}">
44-
{{- .content | markdownify -}}
45-
</div>
46-
{{- end }}
47-
</div>
34+
{{- $groupId := printf "%03d" $.Ordinal -}}
35+
{{- $tabs := (index (.Inner | transform.Unmarshal) "tab") -}}
36+
{{ partial "_elements/tabs" (dict "groupId" $groupId "tabs" $tabs) }}

0 commit comments

Comments
 (0)