diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-08-07 10:38:12 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2023-08-07 10:38:12 +0200 |
commit | 5d5fb22ead440e118030e3440c4dc748fda9bf75 (patch) | |
tree | 64648984cabf03c1939ee8ee8e216c4cd30448a8 /docs/content/en/functions | |
parent | 641390f8f5f5b0ffd57d0fea42ae97d22ab3741e (diff) | |
parent | 7c62d6ef1654c0383eae474d3bd9ddf7754c1f30 (diff) |
Merge commit '7c62d6ef1654c0383eae474d3bd9ddf7754c1f30'
Diffstat (limited to 'docs/content/en/functions')
22 files changed, 115 insertions, 165 deletions
diff --git a/docs/content/en/functions/adddate.md b/docs/content/en/functions/adddate.md index 96a9df833..cae270fb8 100644 --- a/docs/content/en/functions/adddate.md +++ b/docs/content/en/functions/adddate.md @@ -13,11 +13,11 @@ relatedfuncs: [now] ```go-html-template {{ $d := "2022-01-01" | time.AsTime }} -{{ $d.AddDate 0 0 1 | time.Format "2006-01-02" }} --> 2022-01-02 -{{ $d.AddDate 0 1 1 | time.Format "2006-01-02" }} --> 2022-02-02 -{{ $d.AddDate 1 1 1 | time.Format "2006-01-02" }} --> 2023-02-02 +{{ $d.AddDate 0 0 1 | time.Format "2006-01-02" }} → 2022-01-02 +{{ $d.AddDate 0 1 1 | time.Format "2006-01-02" }} → 2022-02-02 +{{ $d.AddDate 1 1 1 | time.Format "2006-01-02" }} → 2023-02-02 -{{ $d.AddDate -1 -1 -1 | time.Format "2006-01-02" }} --> 2020-11-30 +{{ $d.AddDate -1 -1 -1 | time.Format "2006-01-02" }} → 2020-11-30 ``` {{% note %}} @@ -28,11 +28,11 @@ See [this explanation](https://github.com/golang/go/issues/31145#issuecomment-47 ```go-html-template {{ $d := "2023-01-31" | time.AsTime }} -{{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} --> 2023-03-03 +{{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} → 2023-03-03 {{ $d := "2024-01-31" | time.AsTime }} -{{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} --> 2024-03-02 +{{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} → 2024-03-02 {{ $d := "2024-02-29" | time.AsTime }} -{{ $d.AddDate 1 0 0 | time.Format "2006-01-02" }} --> 2025-03-01 +{{ $d.AddDate 1 0 0 | time.Format "2006-01-02" }} → 2025-03-01 ``` diff --git a/docs/content/en/functions/after.md b/docs/content/en/functions/after.md index 5318d3b0c..85aa6104d 100644 --- a/docs/content/en/functions/after.md +++ b/docs/content/en/functions/after.md @@ -33,7 +33,7 @@ You can use `after` in combination with the [`first` function] and Hugo's [power <h2>Featured Article</h2> {{ range first 1 .Pages.ByPublishDate.Reverse }} <header> - <h3><a href="{{ . Permalink }}">{{ .Title }}</a></h3> + <h3><a href="{{ .Permalink }}">{{ .Title }}</a></h3> </header> <p>{{ .Description }}</p> {{ end }} diff --git a/docs/content/en/functions/anchorize.md b/docs/content/en/functions/anchorize.md index bdf322a6e..51ef67bb2 100644 --- a/docs/content/en/functions/anchorize.md +++ b/docs/content/en/functions/anchorize.md @@ -15,10 +15,10 @@ If [Goldmark](/getting-started/configuration-markup#goldmark) is set as `default Since the `defaultMarkdownHandler` and this template function use the same sanitizing logic, you can use the latter to determine the ID of a header for linking with anchor tags. ```go-html-template -{{ anchorize "This is a header" }} --> "this-is-a-header" -{{ anchorize "This is also a header" }} --> "this-is-also----a-header" -{{ anchorize "main.go" }} --> "maingo" -{{ anchorize "Article 123" }} --> "article-123" -{{ anchorize "<- Let's try this, shall we?" }} --> "--lets-try-this-shall-we" -{{ anchorize "Hello, 世界" }} --> "hello-世界" +{{ anchorize "This is a header" }} → "this-is-a-header" +{{ anchorize "This is also a header" }} → "this-is-also----a-header" +{{ anchorize "main.go" }} → "maingo" +{{ anchorize "Article 123" }} → "article-123" +{{ anchorize "<- Let's try this, shall we?" }} → "--lets-try-this-shall-we" +{{ anchorize "Hello, 世界" }} → "hello-世界" ``` diff --git a/docs/content/en/functions/append.md b/docs/content/en/functions/append.md index 626bc1242..6d7ffa245 100644 --- a/docs/content/en/functions/append.md +++ b/docs/content/en/functions/append.md @@ -26,4 +26,12 @@ The same example appending a slice to a slice: {{ $s = $s | append (slice "d" "e") }} ``` +If a slice contains other slices, further slices will be appended as values: + +```go-html-template +{{ $s := slice (slice "a" "b") (slice "c" "d") }} +{{ $s = $s | append (slice "e" "f") (slice "g" "h") }} +{{/* $s now contains a [][]string containing four slices: ["a" "b"], ["c" "d"], ["e" "f"], and ["g" "h"] */}} +``` + The `append` function works for all types, including `Pages`. diff --git a/docs/content/en/functions/common/index.md b/docs/content/en/functions/common/index.md new file mode 100644 index 000000000..cbb7365a6 --- /dev/null +++ b/docs/content/en/functions/common/index.md @@ -0,0 +1,3 @@ ++++ +headless = true ++++ diff --git a/docs/content/en/functions/common/regular-expressions.md b/docs/content/en/functions/common/regular-expressions.md new file mode 100644 index 000000000..9da340849 --- /dev/null +++ b/docs/content/en/functions/common/regular-expressions.md @@ -0,0 +1,8 @@ +When specifying the regular expression, use a raw [string literal] (backticks) instead of an interpreted string literal (double quotes) to simplify the syntax. With an interpreted string literal you must escape backslashes. + +Go's regular expression package implements the [RE2 syntax]. The RE2 syntax is a subset of that accepted by [PCRE], roughly speaking, and with various [caveats]. Note that the RE2 `\C` escape sequence is not supported. + +[caveats]: https://swtch.com/~rsc/regexp/regexp3.html#caveats +[PCRE]: https://www.pcre.org/ +[RE2 syntax]: https://github.com/google/re2/wiki/Syntax/ +[string literal]: https://go.dev/ref/spec#String_literals diff --git a/docs/content/en/functions/fileExists.md b/docs/content/en/functions/fileExists.md index 5a365b7c1..f7b5c37e0 100644 --- a/docs/content/en/functions/fileExists.md +++ b/docs/content/en/functions/fileExists.md @@ -24,11 +24,11 @@ content/ The function returns these values: ```go-html-template -{{ os.FileExists "content" }} --> true -{{ os.FileExists "content/news" }} --> true -{{ os.FileExists "content/news/article-1" }} --> false -{{ os.FileExists "content/news/article-1.md" }} --> true -{{ os.FileExists "news" }} --> true -{{ os.FileExists "news/article-1" }} --> false -{{ os.FileExists "news/article-1.md" }} --> true +{{ os.FileExists "content" }} → true +{{ os.FileExists "content/news" }} → true +{{ os.FileExists "content/news/article-1" }} → false +{{ os.FileExists "content/news/article-1.md" }} → true +{{ os.FileExists "news" }} → true +{{ os.FileExists "news/article-1" }} → false +{{ os.FileExists "news/article-1.md" }} → true ``` diff --git a/docs/content/en/functions/findRe.md b/docs/content/en/functions/findRe.md index 0b8978ec6..3c977118c 100644 --- a/docs/content/en/functions/findRe.md +++ b/docs/content/en/functions/findRe.md @@ -13,21 +13,7 @@ relatedfuncs: [findRESubmatch, replaceRE] --- By default, `findRE` finds all matches. You can limit the number of matches with an optional LIMIT parameter. -When specifying the regular expression, use a raw [string literal] (backticks) instead of an interpreted string literal (double quotes) to simplify the syntax. With an interpreted string literal you must escape backslashes. - -[string literal]: https://go.dev/ref/spec#String_literals - -This function uses the [RE2] regular expression library. See the [RE2 syntax documentation] for details. Note that the RE2 `\C` escape sequence is not supported. - -[RE2]: https://github.com/google/re2/ -[RE2 syntax documentation]: https://github.com/google/re2/wiki/Syntax/ - -{{% note %}} -The RE2 syntax is a subset of that accepted by [PCRE], roughly speaking, and with various [caveats]. - -[caveats]: https://swtch.com/~rsc/regexp/regexp3.html#caveats -[PCRE]: https://www.pcre.org/ -{{% /note %}} +{{% readfile file="/functions/common/regular-expressions.md" %}} This example returns a slice of all second level headings (`h2` elements) within the rendered `.Content`: diff --git a/docs/content/en/functions/findresubmatch.md b/docs/content/en/functions/findresubmatch.md index e1085a9c9..1f0f26b49 100644 --- a/docs/content/en/functions/findresubmatch.md +++ b/docs/content/en/functions/findresubmatch.md @@ -14,21 +14,7 @@ relatedfuncs: [findRE, replaceRE] By default, `findRESubmatch` finds all matches. You can limit the number of matches with an optional LIMIT parameter. A return value of nil indicates no match. -When specifying the regular expression, use a raw [string literal] (backticks) instead of an interpreted string literal (double quotes) to simplify the syntax. With an interpreted string literal you must escape backslashes. - -[string literal]: https://go.dev/ref/spec#String_literals - -This function uses the [RE2] regular expression library. See the [RE2 syntax documentation] for details. Note that the RE2 `\C` escape sequence is not supported. - -[RE2]: https://github.com/google/re2/ -[RE2 syntax documentation]: https://github.com/google/re2/wiki/Syntax/ - -{{% note %}} -The RE2 syntax is a subset of that accepted by [PCRE], roughly speaking, and with various [caveats]. - -[caveats]: https://swtch.com/~rsc/regexp/regexp3.html#caveats -[PCRE]: https://www.pcre.org/ -{{% /note %}} +{{% readfile file="/functions/common/regular-expressions.md" %}} ## Demonstrative examples diff --git a/docs/content/en/functions/getenv.md b/docs/content/en/functions/getenv.md index e014b7ad5..daeeb6532 100644 --- a/docs/content/en/functions/getenv.md +++ b/docs/content/en/functions/getenv.md @@ -12,8 +12,8 @@ relatedfuncs: [] Examples: ```go-html-template -{{ os.Getenv "HOME" }} --> /home/victor -{{ os.Getenv "USER" }} --> victor +{{ os.Getenv "HOME" }} → /home/victor +{{ os.Getenv "USER" }} → victor ``` You can pass values when building your site: @@ -31,8 +31,8 @@ hugo And then retrieve the values within a template: ```go-html-template -{{ os.Getenv "MY_VAR1" }} --> foo -{{ os.Getenv "MY_VAR2" }} --> bar +{{ os.Getenv "MY_VAR1" }} → foo +{{ os.Getenv "MY_VAR2" }} → bar ``` With Hugo v0.91.0 and later, you must explicitly allow access to environment variables. For details, review [Hugo's Security Policy](/about/security-model/#security-policy). By default, environment variables beginning with `HUGO_` are allowed when using the `os.Getenv` function. diff --git a/docs/content/en/functions/hasPrefix.md b/docs/content/en/functions/hasPrefix.md deleted file mode 100644 index 264044577..000000000 --- a/docs/content/en/functions/hasPrefix.md +++ /dev/null @@ -1,20 +0,0 @@ ---- -title: hasprefix -description: Tests whether a string begins with prefix. -date: 2017-02-01 -publishdate: 2017-02-01 -lastmod: 2017-02-01 -categories: [functions] -menu: - docs: - parent: "functions" -keywords: [strings] -signature: ["hasPrefix STRING PREFIX"] -workson: [] -hugoversion: -relatedfuncs: [hasSuffix] -deprecated: false -aliases: [] ---- - -* `{{ hasPrefix "Hugo" "Hu" }}` → true diff --git a/docs/content/en/functions/hasSuffix.md b/docs/content/en/functions/hasSuffix.md deleted file mode 100644 index 5ab38866d..000000000 --- a/docs/content/en/functions/hasSuffix.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -title: hassuffix -linkTitle: hasSuffix -description: Tests whether a string ends with suffix. -date: 2023-03-01 -publishdate: 2023-03-01 -lastmod: 2023-03-01 -categories: [functions] -menu: -docs: -parent: "functions" -keywords: [strings] -signature: ["hasSuffix STRING SUFFIX"] -workson: [] -hugoversion: -relatedfuncs: [hasPrefix] -deprecated: false -aliases: [] ---- - -* `{{ hasSuffix "Hugo" "go" }}` → true diff --git a/docs/content/en/functions/merge.md b/docs/content/en/functions/merge.md index 68e561450..ed370549e 100644 --- a/docs/content/en/functions/merge.md +++ b/docs/content/en/functions/merge.md @@ -27,9 +27,9 @@ Example 1 ```go-html-template {{ $merged := merge $m1 $m2 $m3 }} -{{ $merged.x }} --> baz -{{ $merged.y }} --> wobble -{{ $merged.z.a }} --> huey +{{ $merged.x }} → baz +{{ $merged.y }} → wobble +{{ $merged.z.a }} → huey ``` Example 2 @@ -37,9 +37,9 @@ Example 2 ```go-html-template {{ $merged := merge $m3 $m2 $m1 }} -{{ $merged.x }} --> foo -{{ $merged.y }} --> wibble -{{ $merged.z.a }} --> huey +{{ $merged.x }} → foo +{{ $merged.y }} → wibble +{{ $merged.z.a }} → huey ``` Example 3 @@ -47,9 +47,9 @@ Example 3 ```go-html-template {{ $merged := merge $m2 $m3 $m1 }} -{{ $merged.x }} --> foo -{{ $merged.y }} --> wobble -{{ $merged.z.a }} --> huey +{{ $merged.x }} → foo +{{ $merged.y }} → wobble +{{ $merged.z.a }} → huey ``` Example 4 @@ -57,9 +57,9 @@ Example 4 ```go-html-template {{ $merged := merge $m1 $m3 $m2 }} -{{ $merged.x }} --> bar -{{ $merged.y }} --> wibble -{{ $merged.z.a }} --> huey +{{ $merged.x }} → bar +{{ $merged.y }} → wibble +{{ $merged.z.a }} → huey ``` {{% note %}} diff --git a/docs/content/en/functions/os.Stat.md b/docs/content/en/functions/os.Stat.md index 9ace3b8bf..51d35ae2f 100644 --- a/docs/content/en/functions/os.Stat.md +++ b/docs/content/en/functions/os.Stat.md @@ -13,13 +13,13 @@ The `os.Stat` function attempts to resolve the path relative to the root of your ```go-html-template {{ $f := os.Stat "README.md" }} -{{ $f.IsDir }} --> false (bool) -{{ $f.ModTime }} --> 2021-11-25 10:06:49.315429236 -0800 PST (time.Time) -{{ $f.Name }} --> README.md (string) -{{ $f.Size }} --> 241 (int64) +{{ $f.IsDir }} → false (bool) +{{ $f.ModTime }} → 2021-11-25 10:06:49.315429236 -0800 PST (time.Time) +{{ $f.Name }} → README.md (string) +{{ $f.Size }} → 241 (int64) {{ $d := os.Stat "content" }} -{{ $d.IsDir }} --> true (bool) +{{ $d.IsDir }} → true (bool) ``` Details of the `FileInfo` structure are available in the [Go documentation](https://pkg.go.dev/io/fs#FileInfo). diff --git a/docs/content/en/functions/readdir.md b/docs/content/en/functions/readdir.md index 9fc4d3013..a76e3c5f6 100644 --- a/docs/content/en/functions/readdir.md +++ b/docs/content/en/functions/readdir.md @@ -26,7 +26,7 @@ This template code: ```go-html-template {{ range os.ReadDir "content" }} - {{ .Name }} --> {{ .IsDir }} + {{ .Name }} → {{ .IsDir }} {{ end }} ``` diff --git a/docs/content/en/functions/replacere.md b/docs/content/en/functions/replacere.md index 22f81a2f5..8c3cc13c2 100644 --- a/docs/content/en/functions/replacere.md +++ b/docs/content/en/functions/replacere.md @@ -13,21 +13,7 @@ relatedfuncs: [findRE, FindRESubmatch, replace] --- By default, `replaceRE` replaces all matches. You can limit the number of matches with an optional LIMIT parameter. -When specifying the regular expression, use a raw [string literal] (backticks) instead of an interpreted string literal (double quotes) to simplify the syntax. With an interpreted string literal you must escape backslashes. - -[string literal]: https://go.dev/ref/spec#String_literals - -This function uses the [RE2] regular expression library. See the [RE2 syntax documentation] for details. Note that the RE2 `\C` escape sequence is not supported. - -[RE2]: https://github.com/google/re2/ -[RE2 syntax documentation]: https://github.com/google/re2/wiki/Syntax/ - -{{% note %}} -The RE2 syntax is a subset of that accepted by [PCRE], roughly speaking, and with various [caveats]. - -[caveats]: https://swtch.com/~rsc/regexp/regexp3.html#caveats -[PCRE]: https://www.pcre.org/ -{{% /note %}} +{{% readfile file="/functions/common/regular-expressions.md" %}} This example replaces two or more consecutive hyphens with a single hyphen: diff --git a/docs/content/en/functions/scratch.md b/docs/content/en/functions/scratch.md index 2e00f41bd..16e502b84 100644 --- a/docs/content/en/functions/scratch.md +++ b/docs/content/en/functions/scratch.md @@ -72,7 +72,7 @@ Get the value of a given key. Add a given value to existing value(s) of the given key. -For single values, `Add` accepts values that support Go's `+` operator. If the first `Add` for a key is an array or slice, the following adds will be appended to that list. +For single values, `Add` accepts values that support Go's `+` operator. If the first `Add` for a key is an array or slice, the following adds will be [appended](/functions/append/) to that list. ```go-html-template {{ $scratch.Add "greetings" "Hello" }} diff --git a/docs/content/en/functions/strings.ContainsNonSpace.md b/docs/content/en/functions/strings.ContainsNonSpace.md new file mode 100644 index 000000000..eafe292f5 --- /dev/null +++ b/docs/content/en/functions/strings.ContainsNonSpace.md @@ -0,0 +1,27 @@ +--- +title: strings.ContainsNonSpace +description: Reports whether a string contains any non-space characters as defined by Unicode’s White Space property. +categories: [functions] +menu: + docs: + parent: functions +keywords: [whitespace space] +signature: ["strings.ContainsNonSpace STRING"] +relatedfuncs: ["strings.Contains","strings.ContainsAny"] +--- + +```go-html-template +{{ strings.ContainsNonSpace "\n" }} → false +{{ strings.ContainsNonSpace " " }} → false +{{ strings.ContainsNonSpace "\n abc" }} → true +``` + +Common white space characters include: + +```text +'\t', '\n', '\v', '\f', '\r', ' ' +``` + +See the [Unicode Character Database] for a complete list. + +[Unicode Character Database]: https://www.unicode.org/Public/UCD/latest/ucd/PropList.txt diff --git a/docs/content/en/functions/transform.Unmarshal.md b/docs/content/en/functions/transform.Unmarshal.md index ca5433761..7d0920da8 100644 --- a/docs/content/en/functions/transform.Unmarshal.md +++ b/docs/content/en/functions/transform.Unmarshal.md @@ -62,12 +62,12 @@ The following example lists the items of an RSS feed: ```go-html-template {{ with resources.GetRemote "https://example.com/rss.xml" | transform.Unmarshal }} - {{ range .channel.item }} - <strong>{{ .title | plainify | htmlUnescape }}</strong><br /> - <p>{{ .description | plainify | htmlUnescape }}</p> - {{ $link := .link | plainify | htmlUnescape }} - <a href="{{ $link }}">{{ $link }}</a><br /> - <hr> - {{ end }} + {{ range .channel.item }} + <strong>{{ .title | plainify | htmlUnescape }}</strong><br> + <p>{{ .description | plainify | htmlUnescape }}</p> + {{ $link := .link | plainify | htmlUnescape }} + <a href="{{ $link }}">{{ $link }}</a><br> + <hr> + {{ end }} {{ end }} ``` diff --git a/docs/content/en/functions/uniq.md b/docs/content/en/functions/uniq.md index 4584ec5ad..aecdccf95 100644 --- a/docs/content/en/functions/uniq.md +++ b/docs/content/en/functions/uniq.md @@ -11,5 +11,5 @@ signature: [uniq SET] ```go-html-template -{{ slice 1 3 2 1 | uniq }} --> [1 3 2] +{{ slice 1 3 2 1 | uniq }} → [1 3 2] ``` diff --git a/docs/content/en/functions/urlize.md b/docs/content/en/functions/urlize.md index 8c9aeb1c3..7a9cf25e8 100644 --- a/docs/content/en/functions/urlize.md +++ b/docs/content/en/functions/urlize.md @@ -22,20 +22,20 @@ The following might be used as a partial within a [single page template][singlet {{< code file="layouts/partials/content-header.html" >}} <header> - <h1>{{ .Title }}</h1> - {{ with .Params.location }} - <div><a href="/locations/{{ . | urlize }}">{{ . }}</a></div> - {{ end }} - <!-- Creates a list of tags for the content and links to each of their pages --> - {{ with .Params.tags }} + <h1>{{ .Title }}</h1> + {{ with .Params.location }} + <div><a href="/locations/{{ . | urlize }}">{{ . }}</a></div> + {{ end }} + <!-- Creates a list of tags for the content and links to each of their pages --> + {{ with .Params.tags }} <ul> - {{ range .}} - <li> - <a href="/tags/{{ . | urlize }}">{{ . }}</a> - </li> - {{ end }} + {{ range .}} + <li> + <a href="/tags/{{ . | urlize }}">{{ . }}</a> + </li> + {{ end }} </ul> - {{ end }} + {{ end }} </header> {{< /code >}} diff --git a/docs/content/en/functions/where.md b/docs/content/en/functions/where.md index f2e264235..9618ea4c6 100644 --- a/docs/content/en/functions/where.md +++ b/docs/content/en/functions/where.md @@ -109,25 +109,12 @@ You can also put the returned value of the `where` clauses into a variable: This example matches pages where the "foo" parameter begins with "ab": ```go-html-template -{{ range where site.RegularPages "Params.foo" "like" "^ab" }} +{{ range where site.RegularPages "Params.foo" "like" `^ab` }} <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2> {{ end }} ``` -When specifying the regular expression, use a raw [string literal] (backticks) instead of an interpreted string literal (double quotes) to simplify the syntax. With an interpreted string literal you must escape backslashes. - -[string literal]: https://go.dev/ref/spec#String_literals - -Go's regular expression package implements the [RE2 syntax]. Note that the RE2 `\C` escape sequence is not supported. - -[RE2 syntax]: https://github.com/google/re2/wiki/Syntax/ - -{{% note %}} -The RE2 syntax is a subset of that accepted by [PCRE], roughly speaking, and with various [caveats]. - -[caveats]: https://swtch.com/~rsc/regexp/regexp3.html#caveats -[PCRE]: https://www.pcre.org/ -{{% /note %}} +{{% readfile file="/functions/common/regular-expressions.md" %}} ## Use `where` with `first` |