Skip to content

Commit 9fc387f

Browse files
committed
Implement caption, widths, alignment, thead, and tbody
1 parent 2720b15 commit 9fc387f

File tree

2 files changed

+47
-27
lines changed

2 files changed

+47
-27
lines changed

assets/theme-css/styles.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ a.headerlink {
168168
visibility: hidden;
169169
}
170170

171-
:is(h1, h2, h3, h4, h5, h6):hover > a.headerlink {
171+
:is(h1, h2, h3, h4, h5, h6, caption):hover > a.headerlink {
172172
visibility: visible;
173173
}
174174

layouts/shortcodes/tomlToTable.html

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
{{< tomlToTable >}}
66

77
caption = 'Table caption'
8-
align = [ "left", "left", "right" ]
9-
header-rows = 1
8+
align = ["left", "left", "right"]
9+
widths = ["10%", "50%", "20%"]
10+
headerrows = 1
1011
footer-rows = 3
1112
stub-columns = 1
1213

1314
[[row]]
14-
type = 'header'
1515

1616
[[row.column]]
1717
body = 'Project'
@@ -44,35 +44,55 @@
4444
**Windows**, **Linux**, and **Mac OS X**'''
4545

4646
[[row.column]]
47-
align = 'right'
4847
body = '''[SciPy release page](https://github.com/scipy/scipy/releases) (sources)<br />
4948
[PyPI page for SciPy](https://pypi.python.org/pypi/scipy) (all)'''
5049

5150
{{< /tomlToTable >}}
5251

5352
*/}}
5453

55-
{{ $arg := .Get 0 }}
56-
{{ $data := .Inner | transform.Unmarshal }}
57-
58-
{{ with $data }}
59-
<table class="table">
60-
{{ range $i, $row := .row }}
61-
{{if (modBool $i 2)}}
62-
<tr class="row-odd">
63-
{{else}}
64-
<tr class="row-even">
65-
{{end}}
66-
{{ $el := "td" }}
67-
{{ if (eq $row.type "header") }}
68-
{{ $el = "th class='head'" }}
69-
{{ end }}
70-
{{ range $col := .column }}
71-
{{ printf "<%s" $el | safeHTML }} colspan="{{ $col.colspan }}" rowspan="{{ $col.rowspan }}" {{ printf ">" | safeHTML }}
72-
{{ $col.body | markdownify }}
73-
{{ printf "</%s>" $el | safeHTML }}
74-
{{ end }}
75-
</tr>
54+
{{- $data := .Inner | transform.Unmarshal }}
55+
{{- $id := printf "id%03d" $.Ordinal -}}
56+
{{- $align := $data.align }}
57+
{{- $numrows := $data.row | len }}
58+
{{- $headerrows := or $data.headerrows 0 }}
59+
{{- $header := first $headerrows $data.row }}
60+
{{- $body := last (math.Sub $numrows $headerrows) $data.row }}
61+
62+
<table class="table" id="{{ $id }}">
63+
{{- with $data.caption }}
64+
<caption><span class="caption-text">{{ . }}</span><a class="headerlink" href="#{{ $id }}" title="Link to this table">#</a></caption>
65+
{{- end }}
66+
{{- with $data.widths }}
67+
<colgroup>
68+
{{- range . }}
69+
<col style="width: {{ . }}" />
70+
{{- end }}
71+
</colgroup>
7672
{{ end }}
73+
{{- if $header }}<thead>
74+
{{- range $row_idx, $row := $header }}
75+
{{ if (modBool $row_idx 2) }}<tr class="row-odd">{{- else }}<tr class="row-even">{{- end }}
76+
{{- range $col_idx, $col := .column }}
77+
{{- $alignment := or (index $align $col_idx) "left" }}
78+
<th class="head" style="text-align:{{ $alignment }}">
79+
{{ $col.body | markdownify }}
80+
</th>
81+
{{- end }}
82+
</tr>
83+
{{- end }}
84+
</thead>
85+
{{- end }}
86+
<tbody>
87+
{{- range $row_idx, $row := $body }}
88+
{{ if (modBool (add $row_idx $headerrows) 2) }}<tr class="row-odd">{{- else }}<tr class="row-even">{{- end }}
89+
{{- range $col_idx, $col := .column }}
90+
{{- $alignment := or (index $align $col_idx) "left" }}
91+
<td class="head" style="text-align:{{ $alignment }}">
92+
{{ $col.body | markdownify }}
93+
</td>
94+
{{- end }}
95+
</tr>
96+
{{- end }}
97+
</tbody>
7798
</table>
78-
{{ end }}

0 commit comments

Comments
 (0)