From c105f1a03a7acddbc369c0834bccf624782a0fc6 Mon Sep 17 00:00:00 2001
From: Tim Bannister
Date: Sat, 8 Aug 2020 15:10:53 +0100
Subject: [PATCH] Fix glossary definition shortcode
Fix a bug rendering short glossary definitions. Also, add an example
of a short glossary definition to the style guide.
---
.../contribute/style/hugo-shortcodes/index.md | 24 ++++++++-
layouts/shortcodes/glossary_definition.html | 51 +++++++++++--------
2 files changed, 53 insertions(+), 22 deletions(-)
diff --git a/content/en/docs/contribute/style/hugo-shortcodes/index.md b/content/en/docs/contribute/style/hugo-shortcodes/index.md
index ab949be7fc..dd9ed06c31 100644
--- a/content/en/docs/contribute/style/hugo-shortcodes/index.md
+++ b/content/en/docs/contribute/style/hugo-shortcodes/index.md
@@ -90,19 +90,39 @@ Renders to:
## Glossary
+There are two glossary tooltips.
+
You can reference glossary terms with an inclusion that automatically updates and replaces content with the relevant links from [our glossary](/docs/reference/glossary/). When the term is moused-over by someone
using the online documentation, the glossary entry displays a tooltip.
+As well as inclusions with tooltips, you can reuse the definitions from the glossary in
+page content.
+
The raw data for glossary terms is stored at [https://github.com/kubernetes/website/tree/master/content/en/docs/reference/glossary](https://github.com/kubernetes/website/tree/master/content/en/docs/reference/glossary), with a content file for each glossary term.
-### Glossary Demo
+### Glossary demo
For example, the following include within the markdown renders to {{< glossary_tooltip text="cluster" term_id="cluster" >}} with a tooltip:
-```liquid
+```
{{* glossary_tooltip text="cluster" term_id="cluster" */>}}
```
+Here's a short glossary definition:
+
+```
+{{* glossary_definition prepend="A cluster is" term_id="cluster" length="short" */>}}
+```
+which renders as:
+{{< glossary_definition prepend="A cluster is" term_id="cluster" length="short" >}}
+
+You can also include a full definition:
+```
+{{* glossary_definition term_id="cluster" length="all" */>}}
+```
+which renders as:
+{{< glossary_definition term_id="cluster" length="all" >}}
+
## Table captions
You can make tables more accessible to screen readers by adding a table caption. To add a [caption](https://www.w3schools.com/tags/tag_caption.asp) to a table, enclose the table with a `table` shortcode and specify the caption with the `caption` parameter.
diff --git a/layouts/shortcodes/glossary_definition.html b/layouts/shortcodes/glossary_definition.html
index 76d38fc14b..e2c9089747 100644
--- a/layouts/shortcodes/glossary_definition.html
+++ b/layouts/shortcodes/glossary_definition.html
@@ -1,29 +1,40 @@
-
{{- $id := .Get "term_id" -}}
{{- $length := .Get "length" -}}
-{{- $prepend := .Get "prepend" }}
+{{- $prepend := .Get "prepend" -}}
{{- $glossaryBundle := site.GetPage "page" "docs/reference/glossary" -}}
{{- $glossaryItems := $glossaryBundle.Resources.ByType "page" -}}
{{- $term_info := $glossaryItems.GetMatch (printf "%s.md" $id ) -}}
+{{- $showFullDefinition := false -}}
{{- if not $term_info -}}
-{{- errorf "[%s] %q: %q is not a valid glossary term_id, see ./docs/reference/glossary/* for a full list" site.Language.Lang .Page.Path $id -}}
-{{- end -}}
-{{- with $term_info -}}
-{{- if (strings.Contains "short" $length) -}}
- {{- with .Summary -}}
- {{- if $prepend }}{{- replace . "" (printf "
%s %s" $prepend .) -}}{{ else }}{{- . -}}{{ end -}}
- {{- else -}}
- {{- partial "templates/errorthrower.html" (dict "block" "summary" "purpose" .purpose "describes the key term in greater depth, supplementing the short_description") . -}}
- {{- end -}}
-{{- end -}}
-{{- if (strings.Contains "all|long" $length) -}}
-{{- with .Content -}}
-{{- if $prepend }}
-{{- $firstPara := index (findRE "(?s)
.*?
" . 1) 0 -}}
-{{- $firstPara := $firstPara | strings.TrimSuffix "
" | strings.TrimPrefix "" -}}
-{{- $first := slicestr $firstPara 0 1 | lower }}
-{{- $prepended := printf "
%s %s%s
" $prepend $first (slicestr $firstPara 1) -}}
-{{- replace . $firstPara $prepended | safeHTML -}}{{ else }}{{- . -}}{{ end -}}
+ {{- errorf "[%s] %q: %q is not a valid glossary term_id, see ./docs/reference/glossary/* for a full list" site.Language.Lang .Page.Path $id -}}
{{- end -}}
+{{- if or (eq "long" $length) (eq "all" $length) -}}
+ {{- $showFullDefinition = true -}}
+{{- else if (eq "short" $length) -}}
+ {{- $showFullDefinition = false -}}
+{{- else -}}
+ {{- errorf "[%s] %q: invalid glossary definition length %q" site.Language.Lang .Page.Path $length -}}
{{- end -}}
+{{- with $term_info.Content -}}
+ {{- if not $showFullDefinition -}}
+ {{- $firstPara := index (findRE "(?s).*?
" . 1) 0 -}}
+ {{- $firstPara := $firstPara | strings.TrimSuffix "" | strings.TrimPrefix "" -}}
+ {{- $first := slicestr $firstPara 0 1 | lower -}}
+ {{- if $prepend -}}
+ {{- $prepended := printf "
%s %s%s
" $prepend $first (slicestr $firstPara 1) -}}
+ {{- $prepended | safeHTML -}}
+ {{- else -}}
+ {{- $firstPara | safeHTML -}}
+ {{- end -}}
+ {{- else -}}
+ {{- if $prepend -}}
+ {{- $firstPara := index (findRE "(?s).*?
" . 1) 0 -}}
+ {{- $firstPara := $firstPara | strings.TrimSuffix "" | strings.TrimPrefix "" -}}
+ {{- $first := slicestr $firstPara 0 1 | lower -}}
+ {{- $prepended := printf "
%s %s%s
" $prepend $first (slicestr $firstPara 1) -}}
+ {{- replace . $firstPara $prepended | safeHTML -}}
+ {{- else -}}
+ {{- . -}}
+ {{- end -}}
+ {{- end -}}
{{- end -}}