aboutsummaryrefslogtreecommitdiff
path: root/themes/hugo-pt2021/layouts/shortcodes/table.html
blob: 707b4694f6b63db5ad9f242e7c45fc18b0f100eb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
{{/*
Custom Hugo table shortcode which renders tables by:

1. Reads table definition and data from a structured (JSON, TOML, or
   YAML) data file in the site data directory.
2. Renders the table using CSS classes rather than inline styles.

Why does this shortcode exist?

1. The default Markdown table markup in Hugo is horrible for anything
   more than rudimentary tables.
2. The default Markdown table renderer in Hugo does not give you a way
   to set cell alignment without using inline styles, which means
   enabling `unsafe-inline` in your `Content-Security-Policy` header.
3. I prever specifying table contents in an external structured file
   rather than inline.

Features:

* Sets reasonable default `title` and `aria-label` attributes on
  table, column headers, and cells.
* Annotates column headers and cells with coordinate and column ID data
  attributes for easy styling.
* Add an optional table caption.
* Add optional table, header, row, and cell IDs.
* Specify cell alignment on a per-column or per-cell basis,
* Specify custom table, row, and cell CSS classes.
* Use a custom `data-` attribute prefix to prevent collisions.
* Render cell values as markup or raw HTML.

By default this shortcode renders using Bulma classes, but that can be
changed by setting individual CSS classes in the `params.table_shortcode`
section of the site's `config.toml`.

... TODO: move documentation to README.md, document all options ...
Notes:
* IDs keys that begin with "_" should not be used, because we use
  them to set some option.
* document `params.table_shortcode` TOML options
* document cell values as literal or as dict (latter allows custom
  alignment, id, colspan, and rowspan, class)
* row tooltip with _row and id with _id

Example:

  {{< table "favorite-fruits" "fruits" >}}

And the corresponding `data/favorite-fruits/fruits.yaml`:

  ---
  # table name (required)
  name: "Favorite Fruits"

  # column list (required)
  cols:
    - id: "name"
      name: "Name"
    - id: "color"
      name: "Color"
    - id: "description"
      name: "Description"

  # table rows (required)
  rows:
    - name: "Apple"
      color: "Red or Green"
      description: "Sweet if red, or sour if green."
    - name: "Banana"
      color: "Yellow"
      description: "Skin is slipping hazard on floor."
    - name: "Orange"
      color: "Orange"
      description: "Citrus bonanza."
    - name: "Peach"
      color: "Orange"
      description: "Don't eat the core!"

This shortcode will render the following table:

    <table class='table' title='Favorite Fruits' aria-label='Favorite Fruits'>
      <caption>My favorite fruits.</caption>

      <thead>
        <tr>
          <th title='Name' aria-label='Name' data-th_x='0' data-col_id='name'>
            Name
          </th>

          <th title='Color' aria-label='Color' data-th_x='1' data-col_id='color'>
            Color
          </th>

          <th title='Description' aria-label='Description' data-th_x='2' data-col_id='description'>
            Description
          </th>
        </tr>
      </thead>

      <tbody>
        <tr data-tr_y='0'>
          <td title='Name' aria-label='Name' data-td_x='0' data-col_id='name'>
            Apple
          </td>

          <td title='Color' aria-label='Color' data-td_x='1' data-col_id='color'>
            Red or Green
          </td>

          <td title='Description' aria-label='Description' data-td_x='2' data-col_id='description'>
            Sweet if red, or sour if green.
          </td>
        </tr>

        ... (other rows omitted for brevity) ...
      </tbody>
    </table>

You can also configure alignment on a per-column or per-cell basis, and
you can override the `class` attribute for individual cells.
*/}}

{{/* get config from site params (default to bulma) */}}
{{- $config := default (dict "data_prefix" "" "table_base_css" "table" "cell_align_left" "has-text-left" "cell_align_center" "has-text-centered" "cell_align_justify" "has-text-justified" "cell_align_right" "has-text-right") $.Site.Params.table_shortcode -}}

{{/* get data prefix */}}
{{- $dp := $config.data_prefix | safeHTMLAttr -}}

{{/* get table data */}}
{{- $table := (dict) -}}
{{- if .Get 1 -}}
  {{/* get table data from site data */}}
  {{- $table = (index .Site.Data.tables (.Get 0) (.Get 1)) -}}
{{- else -}}
  {{/* get table data from page params */}}
  {{- $table = (index $.Page.Params "tables" (.Get 0)) -}}
{{- end -}}

<table
  {{/* emit table ID */}}
  {{- if $table.id -}}id='{{- $table.id -}}'{{- end -}}

  {{/* base (e.g. "table" in bulma) and custom CSS class for table */}}
  class='{{- $config.table_base_css }} {{ default "" $table.css -}}'

  {{/* table tooltip and ARIA label */}}
  title='{{- default $table.name $table.tip -}}'
  arial-label='{{- default $table.name $table.tip -}}'
>
  {{- if $table.caption -}}
    {{/* render caption */}}
    <caption>{{- $table.caption -}}</caption>
  {{- end -}}

  {{/* render table headers */}}
  <thead>
    <tr>
      {{- range $x, $col := $table.cols -}}
        {{- $is_map := reflect.IsMap $col -}}

        {{- if $is_map -}}
          {{/* col is a map, reference keys */}}

          {{/* set default css */}}
          {{- $css := "" -}}

          {{/* get alignment */}}
          {{- $align := default "" $col.align -}}
          {{- if eq $align "left" -}}
            {{- $css = $config.cell_align_left -}}
          {{- else if eq $align "center" -}}
            {{- $css = $config.cell_align_center -}}
          {{- else if eq $align "justified" -}}
            {{- $css = $config.cell_align_justify -}}
          {{- else if eq $align "right" -}}
            {{- $css = $config.cell_align_right -}}
          {{- end -}}

          <th
            {{if $css}} class='{{- $css -}}'{{- end}}
            title='{{- default $col.name $col.tip -}}'
            aria-label='{{- default $col.name $col.tip -}}'
            data-{{- $dp -}}th_x='{{- $x -}}'
            data-{{- $dp -}}id='{{- $col.id -}}'
          >
            {{- $col.name -}}
          </th>
        {{- else -}}
          {{/* col is a string */}}
          <th
            title='{{- $col -}}'
            aria-label='{{- $col -}}'
            data-{{- $dp -}}th_x='{{- $x -}}'
            data-{{- $dp -}}id='{{- $x -}}'
          >
            {{- $col -}}
          </th>
        {{- end -}}
      {{- end -}}
    </tr>
  </thead>

  {{- if $table.rows -}}
    <tbody>
      {{/* is the first column a map? */}}
      {{- $first_col_is_map := reflect.IsMap (index $table.cols 0) -}}

      {{/* render rows */}}
      {{- range $y, $row := $table.rows -}}
        {{- if $first_col_is_map -}}
          <tr
            {{/* emit row ID */}}
            {{- if $row._id -}}
              id='{{- $row._id -}}'
            {{- end -}}

            {{- if $row._tip -}}
              {{/* emit row tooltip */}}
              title='{{- $row._tip -}}'
              aria-label='{{- $row._tip -}}'
            {{- end -}}

            {{- if $row._css -}}
              {{/* emit row css */}}
              class='{{- $row._css -}}'
            {{- end -}}

            {{/* row position */}}
            data-{{- $dp -}}tr_y='{{- $y -}}'
          >
            {{- range $x, $col := $table.cols -}}
              {{/* get cell */}}
              {{- $cell := index $row $col.id -}}
              {{- $is_map := reflect.IsMap $cell -}}

              {{/* get cell value */}}
              {{- $val := "" -}}
              {{- if $is_map -}}
                {{- $val = index $cell "val" -}}
              {{- else -}}
                {{- $val = $cell -}}
              {{- end -}}

              {{/* get cell ID */}}
              {{- $cell_id := "" -}}
              {{- if $is_map -}}
                {{- if index $cell "id" -}}
                  {{- $cell_id = $cell.id -}}
                {{- end -}}
              {{- end -}}

              {{/* get cell tip */}}
              {{- $tip := (default $col.name $col.tip) -}}
              {{- if $is_map -}}
                {{- if index $cell "tip" -}}
                  {{- $tip = index $cell "tip" -}}
                {{- end -}}
              {{- end -}}

              {{/* get cell alignment */}}
              {{- $align := $col.align -}}
              {{- if and $is_map -}}
                {{- if (index $cell "align") -}}
                  {{- $align = index $cell "align" -}}
                {{- end -}}
              {{- end -}}

              {{/* get default cell css */}}
              {{- $css := "" -}}
              {{- if eq $align "left" -}}
                {{- $css = $config.cell_align_left -}}
              {{- else if eq $align "center" -}}
                {{- $css = $config.cell_align_center -}}
              {{- else if eq $align "justified" -}}
                {{- $css = $config.cell_align_justify -}}
              {{- else if eq $align "right" -}}
                {{- $css = $config.cell_align_right -}}
              {{- end -}}

              {{/* get cell css override */}}
              {{- if $is_map -}}
                {{- if index $cell "css" -}}
                  {{- $css = $cell.css -}}
                {{- end -}}
              {{- end -}}

              {{/* get colspan */}}
              {{- $colspan := 1 -}}
              {{- if $is_map -}}
                {{- if index $cell "colspan" -}}
                  {{- $colspan = $cell.colspan -}}
                {{- end -}}
              {{- end -}}

              {{/* get rowspan */}}
              {{- $rowspan := 1 -}}
              {{- if $is_map -}}
                {{- if index $cell "rowspan" -}}
                  {{- $rowspan = $cell.rowspan -}}
                {{- end -}}
              {{- end -}}

              {{/* emit cell */}}
              <td
                {{/* emit cell ID */}}
                {{- if $cell_id -}}
                  id='{{- $cell_id -}}'
                {{- end -}}

                title='{{- $tip -}}'
                aria-label='{{- $tip -}}'
                class='{{- $css -}}'

                data-{{- $dp -}}td_x='{{- $x -}}'
                data-{{- $dp -}}col_id='{{- $col.id -}}'

                {{- if gt $colspan 1 -}}
                  colspan='{{- $colspan -}}'
                {{- end -}}

                {{- if gt $rowspan 1 -}}
                  rowspan='{{- $rowspan -}}'
                {{- end -}}
              >
                {{- if $is_map -}}
                  {{- if $cell.html -}}
                    {{/* render as raw HTML */}}
                    {{- $val | safeHTML -}}
                  {{- else -}}
                    {{/* render as markup */}}
                    {{- $val | $.Page.RenderString -}}
                  {{- end -}}
                {{- else -}}
                  {{/* render as markup */}}
                  {{- $val | $.Page.RenderString -}}
                {{- end -}}
              </td>
            {{- end -}}
          </tr>
        {{- else -}}
          {{/* first column is string, rows are arrays of values */}}
          <tr
            {{/* row position */}}
            data-{{- $dp -}}tr_y='{{- $y -}}'
          >
            {{- range $x, $col := $table.cols -}}
              <td
                title='{{- $col -}}'
                aria-label='{{- $col -}}'
              >
                {{- index $row $x -}}
              </td>
            {{- end -}}
          </tr>
        {{- end -}}
      {{- end -}}
    </tbody>
  {{- end -}}
</table>