From 5f2adad2c7197ae1fa073feeab1bad0a26bd5f0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Mon, 6 Jan 2025 12:15:52 +0100 Subject: [PATCH] tpl/images: Format the QR hashes as hex Slightly shorter, and it looks more like a ... hash. --- common/hashing/hashing.go | 7 +++++++ tpl/images/images.go | 2 +- tpl/images/images_integration_test.go | 16 ++++++++-------- tpl/tplimpl/tplimpl_integration_test.go | 4 ++-- 4 files changed, 18 insertions(+), 11 deletions(-) diff --git a/common/hashing/hashing.go b/common/hashing/hashing.go index 0b470cafe..18ec82623 100644 --- a/common/hashing/hashing.go +++ b/common/hashing/hashing.go @@ -83,6 +83,13 @@ func HashString(vs ...any) string { return strconv.FormatUint(hash, 10) } +// HashStringHex returns a hash from the given elements as a hex encoded string. +// See HashString for more information. +func HashStringHex(vs ...any) string { + hash := HashUint64(vs...) + return strconv.FormatUint(hash, 16) +} + var hashOptsPool = sync.Pool{ New: func() any { return &hashstructure.HashOptions{ diff --git a/tpl/images/images.go b/tpl/images/images.go index 08ab3cc95..106c2b280 100644 --- a/tpl/images/images.go +++ b/tpl/images/images.go @@ -169,7 +169,7 @@ 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.HashString(opts))) + targetPath := path.Join(opts.TargetDir, fmt.Sprintf("qr_%s.png", hashing.HashStringHex(opts))) r, err := ns.createClient.FromOpts( create.Options{ diff --git a/tpl/images/images_integration_test.go b/tpl/images/images_integration_test.go index e9a31c9af..0f89dbe09 100644 --- a/tpl/images/images_integration_test.go +++ b/tpl/images/images_integration_test.go @@ -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/tplimpl_integration_test.go b/tpl/tplimpl/tplimpl_integration_test.go index 636e6aa68..04abb5b31 100644 --- a/tpl/tplimpl/tplimpl_integration_test.go +++ b/tpl/tplimpl/tplimpl_integration_test.go @@ -730,7 +730,7 @@ https://gohugo.io" b := hugolib.Test(t, files) b.AssertFileContent("public/index.html", - `QR code linking to https://gohugo.io`, - ``, + `QR code linking to https://gohugo.io`, + ``, ) }