Skip to content

Commit 83c99b8

Browse files
committed
Refactor templates to support categories
1 parent 27a6ccd commit 83c99b8

6 files changed

Lines changed: 93 additions & 18 deletions

File tree

README.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ TODO: Define the YAML format.
1313
For example, to create `v1` of the glossary when `v0` already exists:
1414

1515
1. Add the new version of the glossary under `assets/`
16-
2. Copy `content/v0.md` to `content/v1.md`
17-
3. Edit `content/v1.md`:
18-
- Update the filename of the new glossary version under `assets/`
19-
- Update the title
16+
2. Create a new directory `content/v1/`
17+
3. Copy `content/v0/_index.md` and `content/v0/_content.gotmpl` to `content/v1`
18+
4. Edit `content/v1/_index.md`: Change `glossary_yaml` to the filename of the new glossary version under `assets/`
19+
5. Edit `content/v1/_content.gotmpl`: Change `$glossary_yaml` to the filename of the new glossary version under `assets/`

assets/glossary_xlsx_to_yaml.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
"tags": [r["Context/Tag/Category"]] if r["Context/Tag/Category"] else [],
1414
"definition": LiteralScalarString(r["Definition"]),
1515
}, axis=1)
16+
glossary = list(glossary)
1617

17-
glossary_terms = {"glossary": list(glossary)}
18+
categories = sorted(set(t for r in glossary for t in r["tags"]))
19+
glossary_terms = {"categories": categories, "glossary": glossary}
1820

1921
yaml = YAML()
2022
yaml.indent(mapping=2, sequence=4, offset=2)

assets/uk-tre-glossary-v0.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
version: "v0"
2+
3+
categories:
4+
- Analysis
5+
- Computing
6+
- Data Management
7+
- Data in General
8+
- Data in general
9+
- Health Research
10+
- Health Services & Health Data
11+
- Identifiability
12+
- Management
13+
- Other
14+
- Processes
15+
- Research Management
16+
- Risk Management
17+
- Running and Overseeing Research
18+
- Running and overseeing research
19+
- Security Management
20+
- Services
21+
- Special aspects in the NHS Context
22+
- UK law and rules
123
glossary:
224
- term: AAI
325
tags:

content/v0/_content.gotmpl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{{/*
2+
Generate the category pages for the current glossary
3+
https://gohugo.io/content-management/content-adapters/
4+
*/}}
5+
6+
{{ $glossary_yaml := "uk-tre-glossary-v0.yaml" }}
7+
8+
{{ $data := (resources.Get $glossary_yaml | transform.Unmarshal) }}
9+
10+
{{ range $data.categories }}
11+
12+
{{/* Add category page. */}}
13+
{{ $content := dict "mediaType" "text/markdown" }}
14+
{{ $page := dict
15+
"content" $content
16+
"kind" "page"
17+
"type" "page"
18+
"path" .
19+
"title" (print "UK TRE Glossary " $data.version ": " .)
20+
"layout" "glossary"
21+
"params" (dict "glossary_yaml" $glossary_yaml "glossary_category" .)
22+
}}
23+
{{ $.AddPage $page }}
24+
25+
{{ end }}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: UK TRE Glossary v0
3+
type: page
34
layout: glossary
45
glossary_yaml: uk-tre-glossary-v0.yaml
56
---

layouts/page/glossary.html

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ <h1>{{ .Title }}</h1>
33
{{- .Content -}}
44

55
{{ $data := resources.Get .Params.glossary_yaml | transform.Unmarshal }}
6+
{{ $category := .Params.glossary_category }}
67

78
<table>
89
<tr>
@@ -13,23 +14,47 @@ <h1>{{ .Title }}</h1>
1314

1415
{{- range $data.glossary }}
1516
{{- $term := .term }}
16-
{{- $tag := .tags }}
17+
{{- $tags := .tags }}
1718
{{- $definition := .definition }}
1819
<!-- https://discourse.gohugo.io/t/loop-through-a-list-of-string-ids/33715/2 -->
1920

20-
{{- $crossrefs := findRE `\[[^]]+\]` $definition }}
21-
{{- range $crossrefs }}
22-
{{- $target_term := trim . "[]" }}
23-
{{- $link_target := printf "#term-%s" ( $target_term | anchorize ) }}
24-
{{- $link_md := printf `[%s](%s)` $target_term $link_target }}
25-
{{- $definition = replace $definition . $link_md }}
26-
{{- end }}
21+
{{/*
22+
A complicated way of checking whether the list of tags includes $category
23+
*/}}
24+
{{ $include := 1 }}
25+
{{- if $category -}}
26+
{{ $include = 0 }}
27+
{{- range $tags -}}
28+
{{- if eq $category . -}}
29+
{{ $include = 1 }}
30+
{{- end -}}
31+
{{- end -}}
32+
{{- end -}}
2733

28-
<tr>
29-
<td id="term-{{ $term | anchorize }}"><a href="#term-{{ $term | anchorize }}">{{ $term }}</a></td>
30-
<td>{{ $tag }}</td>
31-
<td>{{ $definition | markdownify }}</td>
32-
</tr>
34+
{{- if $include -}}
35+
36+
{{/*
37+
Parse [cross-references] in definitions
38+
*/}}
39+
{{- $crossrefs := findRE `\[[^]]+\]` $definition }}
40+
{{- range $crossrefs }}
41+
{{- $target_term := trim . "[]" }}
42+
{{- $link_target := printf "#term-%s" ( $target_term | anchorize ) }}
43+
{{- $link_md := printf `[%s](%s)` $target_term $link_target }}
44+
{{- $definition = replace $definition . $link_md }}
45+
{{- end }}
46+
47+
<tr>
48+
<td id="term-{{ $term | anchorize }}"><a href="#term-{{ $term | anchorize }}">{{ $term }}</a></td>
49+
<td>
50+
{{- range $tags -}}
51+
<a href="{{ . | urlize }}">{{ . }}</a>
52+
{{- end -}}
53+
</td>
54+
<td>{{ $definition | markdownify }}</td>
55+
</tr>
56+
57+
{{ end }}
3358
{{ end }}
3459
</table>
3560

0 commit comments

Comments
 (0)