From b13d0a68e67e728fcfaa2507b16c7ef4704b6fe6 Mon Sep 17 00:00:00 2001 From: Joe Mooring Date: Wed, 8 Jan 2025 10:10:18 -0800 Subject: [PATCH] tpl/images: Change signature of images.QR to images.QR TEXT OPTIONS Closes #13230 --- docs/content/en/functions/images/QR.md | 13 +++---- tpl/images/images.go | 22 ++++++++---- tpl/images/images_integration_test.go | 34 +++++++++---------- .../embedded/templates/shortcodes/qr.html | 4 +-- tpl/tplimpl/tplimpl_integration_test.go | 4 +-- 5 files changed, 42 insertions(+), 35 deletions(-) diff --git a/docs/content/en/functions/images/QR.md b/docs/content/en/functions/images/QR.md index c1b8fb465..9d58e7163 100644 --- a/docs/content/en/functions/images/QR.md +++ b/docs/content/en/functions/images/QR.md @@ -6,7 +6,7 @@ action: aliases: [] related: [] returnType: images.ImageResource - signatures: ['images.QR OPTIONS'] + signatures: ['images.QR TEXT OPTIONS'] toc: true math: true --- @@ -25,9 +25,6 @@ Although the default option values are sufficient for most applications, you sho ## Options -text -: (`string`) The text to encode. - level : (`string`) The error correction level to use when encoding the text, one of `low`, `medium`, `quartile`, or `high`. Default is `medium`. @@ -51,8 +48,8 @@ targetDir To create a QR code using the default values for `level` and `scale`: ```go-html-template -{{ $opts := dict "text" "https://gohugo.io" }} -{{ with images.QR $opts }} +{{ $text := "https://gohugo.io" }} +{{ with images.QR $text }} {{ end }} ``` @@ -62,13 +59,13 @@ To create a QR code using the default values for `level` and `scale`: Specify `level`, `scale`, and `targetDir` as needed to achieve the desired result: ```go-html-template +{{ $text := "https://gohugo.io" }} {{ $opts := dict - "text" "https://gohugo.io" "level" "high" "scale" 3 "targetDir" "codes" }} -{{ with images.QR $opts }} +{{ with images.QR $text $opts }} {{ end }} ``` diff --git a/tpl/images/images.go b/tpl/images/images.go index 106c2b280..6296a7214 100644 --- a/tpl/images/images.go +++ b/tpl/images/images.go @@ -135,14 +135,13 @@ var qrErrorCorrectionLevels = map[string]qr.Level{ // QR encodes the given text into a QR code using the specified options, // returning an image resource. -func (ns *Namespace) QR(options any) (images.ImageResource, error) { +func (ns *Namespace) QR(args ...any) (images.ImageResource, error) { const ( qrDefaultErrorCorrectionLevel = "medium" qrDefaultScale = 4 ) opts := struct { - Text string // text to encode Level string // error correction level; one of low, medium, quartile, or high Scale int // number of image pixels per QR code module TargetDir string // target directory relative to publishDir @@ -151,15 +150,26 @@ func (ns *Namespace) QR(options any) (images.ImageResource, error) { Scale: qrDefaultScale, } - err := mapstructure.WeakDecode(options, &opts) + if len(args) == 0 || len(args) > 2 { + return nil, errors.New("requires 1 or 2 arguments") + } + + text, err := cast.ToStringE(args[0]) if err != nil { return nil, err } - if opts.Text == "" { + if text == "" { return nil, errors.New("cannot encode an empty string") } + if len(args) == 2 { + err := mapstructure.WeakDecode(args[1], &opts) + if err != nil { + return nil, err + } + } + level, ok := qrErrorCorrectionLevels[opts.Level] if !ok { return nil, errors.New("error correction level must be one of low, medium, quartile, or high") @@ -169,14 +179,14 @@ func (ns *Namespace) QR(options any) (images.ImageResource, error) { return nil, errors.New("scale must be an integer greater than or equal to 2") } - targetPath := path.Join(opts.TargetDir, fmt.Sprintf("qr_%s.png", hashing.HashStringHex(opts))) + targetPath := path.Join(opts.TargetDir, fmt.Sprintf("qr_%s.png", hashing.HashStringHex(text, opts))) r, err := ns.createClient.FromOpts( create.Options{ TargetPath: targetPath, TargetPathHasHash: true, CreateContent: func() (func() (hugio.ReadSeekCloser, error), error) { - code, err := qr.Encode(opts.Text, level) + code, err := qr.Encode(text, level) if err != nil { return nil, err } diff --git a/tpl/images/images_integration_test.go b/tpl/images/images_integration_test.go index 0f89dbe09..2847128e1 100644 --- a/tpl/images/images_integration_test.go +++ b/tpl/images/images_integration_test.go @@ -61,17 +61,17 @@ disableKinds = ['page','rss','section','sitemap','taxonomy','term'] -- layouts/index.html -- {{- $text := "https://gohugo.io" }} {{- $optionMaps := slice - (dict "text" $text) - (dict "text" $text "level" "medium") - (dict "text" $text "level" "medium" "scale" 4) - (dict "text" $text "level" "low" "scale" 2) - (dict "text" $text "level" "medium" "scale" 3) - (dict "text" $text "level" "quartile" "scale" 5) - (dict "text" $text "level" "high" "scale" 6) - (dict "text" $text "level" "high" "scale" 6 "targetDir" "foo/bar") + (dict) + (dict "level" "medium") + (dict "level" "medium" "scale" 4) + (dict "level" "low" "scale" 2) + (dict "level" "medium" "scale" 3) + (dict "level" "quartile" "scale" 5) + (dict "level" "high" "scale" 6) + (dict "level" "high" "scale" 6 "targetDir" "foo/bar") }} {{- range $k, $opts := $optionMaps }} - {{- with images.QR $opts }} + {{- with images.QR $text $opts }} {{- end }} {{- end }} @@ -79,14 +79,14 @@ disableKinds = ['page','rss','section','sitemap','taxonomy','term'] b := hugolib.Test(t, files) b.AssertFileContent("public/index.html", - ``, - ``, - ``, - ``, - ``, - ``, - ``, - ``, + ``, + ``, + ``, + ``, + ``, + ``, + ``, + ``, ) files = strings.ReplaceAll(files, "low", "foo") diff --git a/tpl/tplimpl/embedded/templates/shortcodes/qr.html b/tpl/tplimpl/embedded/templates/shortcodes/qr.html index cae7e5670..252ebeae7 100644 --- a/tpl/tplimpl/embedded/templates/shortcodes/qr.html +++ b/tpl/tplimpl/embedded/templates/shortcodes/qr.html @@ -64,8 +64,8 @@ Encodes the given text into a QR code using the specified options and renders th {{- /* Render image. */}} {{- if not $errors }} - {{- $opts := dict "text" $text "level" $level "scale" $scale "targetDir" $targetDir }} - {{- with images.QR $opts -}} + {{- $opts := dict "level" $level "scale" $scale "targetDir" $targetDir }} + {{- with images.QR $text $opts -}} {{ $alt }}`, - ``, + `QR code linking to https://gohugo.io`, + ``, ) }