| 1 |
{{- /* |
| 2 |
Encodes the given text into a QR code using the specified options and renders the resulting image. |
| 3 |
|
| 4 |
@param {string} text The text to encode, falling back to the text between the opening and closing shortcode tags. |
| 5 |
@param {string} [level=medium] The error correction level to use when encoding the text, one of low, medium, quartile, or high. |
| 6 |
@param {int} [scale=4] The number of image pixels per QR code module. Must be greater than or equal to 2. |
| 7 |
@param {string} [targetDir] The subdirectory within the publishDir where Hugo will place the generated image. |
| 8 |
@param {string} [alt] The alt attribute of the img element. |
| 9 |
@param {string} [class] The class attribute of the img element. |
| 10 |
@param {string} [id] The id attribute of the img element. |
| 11 |
@param {string} [title] The title attribute of the img element. |
| 12 |
@param {string} [loading] The loading attribute of the img element, one of lazy or eager. |
| 13 |
|
| 14 |
@returns {template.HTML} |
| 15 |
|
| 16 |
@examples |
| 17 |
|
| 18 |
{{< qr text="https://gohugo.io" />}} |
| 19 |
|
| 20 |
{{< qr >}} |
| 21 |
https://gohugo.io" |
| 22 |
{{< /qr >}} |
| 23 |
|
| 24 |
{{< qr |
| 25 |
text="https://gohugo.io" |
| 26 |
level="high" |
| 27 |
scale=4 |
| 28 |
targetDir="codes" |
| 29 |
alt="QR code linking to https://gohugo.io" |
| 30 |
class="my-class" |
| 31 |
id="my-id" |
| 32 |
title="My Title" |
| 33 |
/>}} |
| 34 |
|
| 35 |
*/}} |
| 36 |
|
| 37 |
{{- /* Constants. */}} |
| 38 |
{{- $validLevels := slice "low" "medium" "quartile" "high" }} |
| 39 |
{{- $minimumScale := 2 }} |
| 40 |
|
| 41 |
{{- /* Get arguments. */}} |
| 42 |
{{- $text := or (.Get "text") (strings.TrimSpace .Inner) "" }} |
| 43 |
{{- $level := or (.Get "level") "medium" }} |
| 44 |
{{- $scale := or (.Get "scale") 4 }} |
| 45 |
{{- $targetDir := or (.Get "targetDir") "" }} |
| 46 |
{{- $alt := or (.Get "alt") "" }} |
| 47 |
{{- $class := or (.Get "class") "" }} |
| 48 |
{{- $id := or (.Get "id") "" }} |
| 49 |
{{- $title := or (.Get "title") "" }} |
| 50 |
{{- $loading := or (.Get "loading") "" }} |
| 51 |
|
| 52 |
{{- /* Validate arguments. */}} |
| 53 |
{{- $errors := false }} |
| 54 |
{{- if not $text }} |
| 55 |
{{- errorf "The %q shortcode requires a %q argument. See %s" .Name "text" .Position }} |
| 56 |
{{- $errors = true }} |
| 57 |
{{- end }} |
| 58 |
{{- if not (in $validLevels $level) }} |
| 59 |
{{- errorf "The %q argument passed to the %q shortcode must be one of %s. See %s" "level" .Name (delimit $validLevels ", " ", or ") .Position }} |
| 60 |
{{- $errors = true }} |
| 61 |
{{- end }} |
| 62 |
{{- if or (lt $scale $minimumScale) (ne $scale (int $scale)) }} |
| 63 |
{{- errorf "The %q argument passed to the %q shortcode must be an integer greater than or equal to %d. See %s" "scale" .Name $minimumScale .Position }} |
| 64 |
{{- $errors = true }} |
| 65 |
{{- end }} |
| 66 |
|
| 67 |
{{- /* Render image. */}} |
| 68 |
{{- if not $errors }} |
| 69 |
{{- $opts := dict "level" $level "scale" $scale "targetDir" $targetDir }} |
| 70 |
{{- with images.QR $text $opts -}} |
| 71 |
<img src="{{ .RelPermalink }}" width="{{ .Width }}" height="{{ .Height }}" |
| 72 |
{{- with $alt }} alt="{{ $alt }}" {{- end }} |
| 73 |
{{- with $class }} class="{{ $class }}" {{- end }} |
| 74 |
{{- with $id }} id="{{ $id }}" {{- end }} |
| 75 |
{{- with $title }} title="{{ $title }}" {{- end -}}{{- with $loading }} loading="{{ $loading }}" {{- end -}}> |
| 76 |
{{- end }} |
| 77 |
{{- end -}} |