docs: Prepare for new sub tree

See #11925
This commit is contained in:
Bjørn Erik Pedersen 2024-01-27 10:47:28 +01:00
parent 1083bf7c08
commit fc7de7136a
No known key found for this signature in database
1157 changed files with 0 additions and 64232 deletions

View file

@ -1,27 +0,0 @@
---
title: strings.Chomp
description: Returns the given string, removing all trailing newline characters and carriage returns.
categories: []
keywords: []
action:
aliases: [chomp]
related:
- functions/strings/Trim
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimRight
- functions/strings/TrimSuffix
returnType: any
signatures: [strings.Chomp STRING]
aliases: [/functions/chomp]
---
If the argument is of type `template.HTML`, returns `template.HTML`, else returns a `string`.
```go-html-template
{{ chomp | "foo\n" }} → foo
{{ chomp | "foo\n\n" }} → foo
{{ chomp | "foo\r\n" }} → foo
{{ chomp | "foo\r\n\r\n" }} → foo
```

View file

@ -1,27 +0,0 @@
---
title: strings.Contains
description: Reports whether the given string contains the given substring.
categories: []
keywords: []
action:
aliases: []
related:
- functions/strings/ContainsAny
- functions/strings/ContainsNonSpace
- functions/strings/HasPrefix
- functions/strings/HasSuffix
- functions/collections/In
returnType: bool
signatures: [strings.Contains STRING SUBSTRING]
aliases: [/functions/strings.contains]
---
```go-html-template
{{ strings.Contains "Hugo" "go" }} → true
```
The check is case sensitive:
```go-html-template
{{ strings.Contains "Hugo" "Go" }} → false
```

View file

@ -1,27 +0,0 @@
---
title: strings.ContainsAny
description: Reports whether the given string contains any character within the given set.
categories: []
keywords: []
action:
aliases: []
related:
- functions/strings/Contains
- functions/strings/ContainsNonSpace
- functions/strings/HasPrefix
- functions/strings/HasSuffix
- functions/collections/In
returnType: bool
signatures: [strings.ContainsAny STRING SET]
aliases: [/functions/strings.containsany]
---
```go-html-template
{{ strings.ContainsAny "Hugo" "gm" }} → true
```
The check is case sensitive:
```go-html-template
{{ strings.ContainsAny "Hugo" "Gm" }} → false
```

View file

@ -1,35 +0,0 @@
---
title: strings.ContainsNonSpace
description: Reports whether the given string contains any non-space characters as defined by Unicodes White Space property.
categories: []
keywords: []
action:
aliases: []
related:
- functions/strings/Contains
- functions/strings/ContainsAny
- functions/strings/HasPrefix
- functions/strings/HasSuffix
- functions/collections/In
returnType: bool
signatures: [strings.ContainsNonSpace STRING]
aliases: [/functions/strings.containsnonspace]
---
{{< new-in 0.111.0 >}}
```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

View file

@ -1,25 +0,0 @@
---
title: strings.Count
description: Returns the number of non-overlapping instances of the given substring within the given string.
categories: []
keywords: []
action:
aliases: []
related:
- functions/go-template/len
- functions/strings/CountRunes
- functions/strings/CountWords
- functions/strings/RuneCount
returnType: int
signatures: [strings.Count SUBSTR STRING]
aliases: [/functions/strings.count]
---
If `SUBSTR` is an empty string, this function returns 1 plus the number of Unicode code points in `STRING`.
```go-html-template
{{ "aaabaab" | strings.Count "a" }} → 5
{{ "aaabaab" | strings.Count "aa" }} → 2
{{ "aaabaab" | strings.Count "aaa" }} → 1
{{ "aaabaab" | strings.Count "" }} → 8
```

View file

@ -1,24 +0,0 @@
---
title: strings.CountRunes
description: Returns the number of runes in the given string excluding whitespace.
categories: []
keywords: []
action:
aliases: [countrunes]
related:
- functions/go-template/len
- functions/strings/Count
- functions/strings/CountWords
- functions/strings/RuneCount
returnType: int
signatures: [strings.CountRunes INPUT]
aliases: [/functions/countrunes]
---
In contrast with the [`strings.RuneCount`] function, which counts every rune in a string, `strings.CountRunes` excludes whitespace.
```go-html-template
{{ "Hello, 世界" | strings.CountRunes }} → 8
```
[`strings.RuneCount`]: /functions/strings/runecount

View file

@ -1,20 +0,0 @@
---
title: strings.CountWords
description: Returns the number of words in the given string.
categories: []
keywords: []
action:
aliases: [countwords]
related:
- functions/go-template/len
- functions/strings/Count
- functions/strings/CountRunes
- functions/strings/RuneCount
returnType: int
signatures: [strings.CountWords INPUT]
aliases: [/functions/countwords]
---
```go-html-template
{{ "Hugo is a static site generator." | countwords }} → 6
```

View file

@ -1,90 +0,0 @@
---
title: strings.FindRESubmatch
description: Returns a slice of all successive matches of the regular expression. Each element is a slice of strings holding the text of the leftmost match of the regular expression and the matches, if any, of its subexpressions.
categories: []
keywords: []
action:
aliases: [findRESubmatch]
related:
- functions/strings/FindRE
- functions/strings/Replace
- functions/strings/ReplaceRE
returnType: '[][]string'
signatures: ['strings.FindRESubmatch PATTERN INPUT [LIMIT]']
aliases: [/functions/findresubmatch]
---
By default, `findRESubmatch` finds all matches. You can limit the number of matches with an optional LIMIT argument. A return value of nil indicates no match.
{{% include "functions/_common/regular-expressions.md" %}}
## Demonstrative examples
```go-html-template
{{ findRESubmatch `a(x*)b` "-ab-" }} → [["ab" ""]]
{{ findRESubmatch `a(x*)b` "-axxb-" }} → [["axxb" "xx"]]
{{ findRESubmatch `a(x*)b` "-ab-axb-" }} → [["ab" ""] ["axb" "x"]]
{{ findRESubmatch `a(x*)b` "-axxb-ab-" }} → [["axxb" "xx"] ["ab" ""]]
{{ findRESubmatch `a(x*)b` "-axxb-ab-" 1 }} → [["axxb" "xx"]]
```
## Practical example
This markdown:
```text
- [Example](https://example.org)
- [Hugo](https://gohugo.io)
```
Produces this HTML:
```html
<ul>
<li><a href="https://example.org">Example</a></li>
<li><a href="https://gohugo.io">Hugo</a></li>
</ul>
```
To match the anchor elements, capturing the link destination and text:
```go-html-template
{{ $regex := `<a\s*href="(.+?)">(.+?)</a>` }}
{{ $matches := findRESubmatch $regex .Content }}
```
Viewed as JSON, the data structure of `$matches` in the code above is:
```json
[
[
"<a href=\"https://example.org\"></a>Example</a>",
"https://example.org",
"Example"
],
[
"<a href=\"https://gohugo.io\">Hugo</a>",
"https://gohugo.io",
"Hugo"
]
]
```
To render the `href` attributes:
```go-html-template
{{ range $matches }}
{{ index . 1 }}
{{ end }}
```
Result:
```text
https://example.org
https://gohugo.io
```
{{% note %}}
You can write and test your regular expression using [regex101.com](https://regex101.com/). Be sure to select the Go flavor before you begin.
{{% /note %}}

View file

@ -1,36 +0,0 @@
---
title: strings.FindRE
description: Returns a slice of strings that match the regular expression.
categories: []
keywords: []
action:
aliases: [findRE]
related:
- functions/strings/FindRESubmatch
- functions/strings/Replace
- functions/strings/ReplaceRE
returnType: '[]string'
signatures: ['strings.FindRE PATTERN INPUT [LIMIT]']
aliases: [/functions/findre]
---
By default, `findRE` finds all matches. You can limit the number of matches with an optional LIMIT argument.
{{% include "functions/_common/regular-expressions.md" %}}
This example returns a slice of all second level headings (`h2` elements) within the rendered `.Content`:
```go-html-template
{{ findRE `(?s)<h2.*?>.*?</h2>` .Content }}
```
The `s` flag causes `.` to match `\n` as well, allowing us to find an `h2` element that contains newlines.
To limit the number of matches to one:
```go-html-template
{{ findRE `(?s)<h2.*?>.*?</h2>` .Content 1 }}
```
{{% note %}}
You can write and test your regular expression using [regex101.com](https://regex101.com/). Be sure to select the Go flavor before you begin.
{{% /note %}}

View file

@ -1,19 +0,0 @@
---
title: strings.FirstUpper
description: Returns the given string, capitalizing the first character.
categories: []
keywords: []
action:
aliases: []
related:
- functions/strings/Title
- functions/strings/ToLower
- functions/strings/ToUpper
returnType: string
signatures: [strings.FirstUpper STRING]
aliases: [/functions/strings.firstupper]
---
```go-html-template
{{ strings.FirstUpper "foo" }} → Foo
```

View file

@ -1,21 +0,0 @@
---
title: strings.HasPrefix
description: Reports whether the given string begins with the given prefix.
categories: []
keywords: []
action:
aliases: [hasPrefix]
related:
- functions/strings/Contains
- functions/strings/ContainsAny
- functions/strings/ContainsNonSpace
- functions/strings/HasSuffix
- functions/collections/In
returnType: bool
signatures: [strings.HasPrefix STRING PREFIX]
aliases: [/functions/hasprefix,/functions/strings.hasprefix]
---
```go-html-template
{{ hasPrefix "Hugo" "Hu" }} → true
```

View file

@ -1,21 +0,0 @@
---
title: strings.HasSuffix
description: Reports whether the given string ends with the given suffix.
categories: []
keywords: []
action:
aliases: [hasSuffix]
related:
- functions/strings/Contains
- functions/strings/ContainsAny
- functions/strings/ContainsNonSpace
- functions/strings/HasPrefix
- functions/collections/In
returnType: bool
signatures: [strings.HasSuffix STRING SUFFIX]
aliases: [/functions/hassuffix,/functions/strings/hassuffix]
---
```go-html-template
{{ hasSuffix "Hugo" "go" }} → true
```

View file

@ -1,16 +0,0 @@
---
title: strings.Repeat
description: Returns a new string consisting of zero or more copies of another string.
categories: []
keywords: []
action:
aliases: []
related: []
returnType: string
signatures: [strings.Repeat COUNT INPUT]
aliases: [/functions/strings.repeat]
---
```go-html-template
{{ strings.Repeat 3 "yo" }} → yoyoyo
```

View file

@ -1,26 +0,0 @@
---
title: strings.Replace
description: Returns a copy of INPUT, replacing all occurrences of OLD with NEW.
categories: []
keywords: []
action:
aliases: [replace]
related:
- functions/strings/FindRE
- functions/strings/FindRESubmatch
- functions/strings/ReplaceRE
returnType: string
signatures: ['strings.Replace INPUT OLD NEW [LIMIT]']
aliases: [/functions/replace]
---
```go-html-template
{{ $s := "Batman and Robin" }}
{{ replace $s "Robin" "Catwoman" }} → Batman and Catwoman
```
Limit the number of replacements using the `LIMIT` argument:
```go-html-template
{{ replace "aabbaabb" "a" "z" 2 }} → zzbbaabb
```

View file

@ -1,43 +0,0 @@
---
title: strings.ReplaceRE
description: Returns a copy of INPUT, replacing all occurrences of a regular expression with a replacement pattern.
categories: []
keywords: []
action:
aliases: [replaceRE]
related:
- functions/strings/FindRE
- functions/strings/FindRESubmatch
- functions/strings/Replace
returnType: string
signatures: ['strings.ReplaceRE PATTERN REPLACEMENT INPUT [LIMIT]']
aliases: [/functions/replacere]
---
{{% include "functions/_common/regular-expressions.md" %}}
```go-html-template
{{ $s := "a-b--c---d" }}
{{ replaceRE `(-{2,})` "-" $s }} → a-b-c-d
```
Limit the number of replacements using the LIMIT argument:
```go-html-template
{{ $s := "a-b--c---d" }}
{{ replaceRE `(-{2,})` "-" $s 1 }} → a-b-c---d
```
Use `$1`, `$2`, etc. within the replacement string to insert the content of each capturing group within the regular expression:
```go-html-template
{{ $s := "http://gohugo.io/docs" }}
{{ replaceRE "^https?://([^/]+).*" "$1" $s }} → gohugo.io
```
{{% note %}}
You can write and test your regular expression using [regex101.com](https://regex101.com/). Be sure to select the Go flavor before you begin.
{{% /note %}}
[RE2]: https://github.com/google/re2/wiki/Syntax
[string literal]: https://go.dev/ref/spec#String_literals

View file

@ -1,24 +0,0 @@
---
title: strings.RuneCount
description: Returns the number of runes in the given string.
categories: []
keywords: []
action:
aliases: []
related:
- functions/go-template/len
- functions/strings/Count
- functions/strings/CountRunes
- functions/strings/CountWords
returnType: int
signatures: [strings.RuneCount INPUT]
aliases: [/functions/strings.runecount]
---
In contrast with the [`strings.CountRunes`] function, which excludes whitespace, `strings.RuneCount` counts every rune in a string.
```go-html-template
{{ "Hello, 世界" | strings.RuneCount }} → 9
```
[`strings.CountRunes`]: /functions/strings/countrunes

View file

@ -1,20 +0,0 @@
---
title: strings.SliceString
description: Creates a slice of a half-open range, including start and end indices.
categories: []
keywords: []
action:
aliases: [slicestr]
related: []
returnType: string
signatures: ['strings.SliceString STRING START [END]']
aliases: [/functions/slicestr]
---
For example, 1 and 4 creates a slice including elements 1 through&nbsp;3.
The `end` index can be omitted; it defaults to the string's length.
```go-html-template
{{ slicestr "BatMan" 3 }}` → Man
{{ slicestr "BatMan" 0 3 }}` → Bat
```

View file

@ -1,26 +0,0 @@
---
title: strings.Split
description: Returns a slice of strings by splitting the given string by a delimiter.
categories: []
keywords: []
action:
aliases: [split]
related:
- functions/collections/Delimit
returnType: string
signatures: [strings.Split STRING DELIM]
aliases: [/functions/split]
---
Examples:
```go-html-template
{{ split "tag1,tag2,tag3" "," }} → ["tag1", "tag2", "tag3"]
{{ split "abc" "" }} → ["a", "b", "c"]
```
{{% note %}}
The `strings.Split` function essentially does the opposite of the [`collections.Delimit`] function. While `split` creates a slice from a string, `delimit` creates a string from a slice.
[`collections.Delimit`]: /functions/collections/delimit
{{% /note %}}

View file

@ -1,38 +0,0 @@
---
title: strings.Substr
description: Extracts parts of a string from a specified character's position and returns the specified number of characters.
categories: []
keywords: []
action:
aliases: [substr]
related: []
returnType: string
signatures: ['strings.Substr STRING START [LENGTH]']
aliases: [/functions/substr]
---
It normally takes two argument: `start` and `length`. It can also take one argument: `start`, i.e. `length` is omitted, in which case the substring starting from start until the end of the string will be returned.
To extract characters from the end of the string, use a negative start number.
If `length` is given and is negative, that number of characters will be omitted from the end of string.
```go-html-template
{{ substr "abcdef" 0 }} → abcdef
{{ substr "abcdef" 1 }} → bcdef
{{ substr "abcdef" 0 1 }} → a
{{ substr "abcdef" 1 1 }} → b
{{ substr "abcdef" 0 -1 }} → abcde
{{ substr "abcdef" 1 -1 }} → bcde
{{ substr "abcdef" -1 }} → f
{{ substr "abcdef" -2 }} → ef
{{ substr "abcdef" -1 1 }} → f
{{ substr "abcdef" -2 1 }} → e
{{ substr "abcdef" -3 -1 }} → de
{{ substr "abcdef" -3 -2 }} → d
```

View file

@ -1,32 +0,0 @@
---
title: strings.Title
description: Returns the given string, converting it to title case.
categories: []
keywords: []
action:
aliases: [title]
related:
- functions/strings/FirstUpper
- functions/strings/ToLower
- functions/strings/ToUpper
returnType: string
signatures: [strings.Title STRING]
aliases: [/functions/title]
---
```go-html-template
{{ title "table of contents (TOC)" }} → Table of Contents (TOC)
```
By default, Hugo follows the capitalization rules published in the [Associated Press Stylebook]. Change your [site configuration] if you would prefer to:
- Follow the capitalization rules published in the [Chicago Manual of Style]
- Capitalize the first letter of every word
- Capitalize the first letter of the first word
- Disable the effects of the `title` function
The last option is useful if your theme uses the `title` function, and you would prefer to manually capitalize strings as needed.
[Associated Press Stylebook]: https://www.apstylebook.com/
[Chicago Manual of Style]: https://www.chicagomanualofstyle.org/home.html
[site configuration]: /getting-started/configuration/#configure-title-case

View file

@ -1,19 +0,0 @@
---
title: strings.ToLower
description: Returns the given string, converting all characters to lowercase.
categories: []
keywords: []
action:
aliases: [lower]
related:
- functions/strings/FirstUpper
- functions/strings/Title
- functions/strings/ToUpper
returnType: string
signatures: [strings.ToLower INPUT]
aliases: [/functions/lower]
---
```go-html-template
{{ lower "BatMan" }} → batman
```

View file

@ -1,19 +0,0 @@
---
title: strings.ToUpper
description: Returns the given string, converting all characters to uppercase.
categories: []
keywords: []
action:
aliases: [upper]
related:
- functions/strings/FirstUpper
- functions/strings/Title
- functions/strings/ToLower
returnType: string
signatures: [strings.ToUpper INPUT]
aliases: [/functions/upper]
---
```go-html-template
{{ upper "BatMan" }} → BATMAN
```

View file

@ -1,59 +0,0 @@
---
title: strings.Trim
description: Returns the given string, removing leading and trailing characters specified in the cutset.
categories: []
keywords: []
action:
aliases: [trim]
related:
- functions/strings/Chomp
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimRight
- functions/strings/TrimSuffix
returnType: string
signatures: [strings.Trim INPUT CUTSET]
aliases: [/functions/trim]
---
```go-html-template
{{ trim "++foo--" "+-" }} → foo
```
To remove leading and trailing newline characters and carriage returns:
```go-html-template
{{ trim "\nfoo\n" "\n\r" }} → foo
{{ trim "\n\nfoo\n\n" "\n\r" }} → foo
{{ trim "\r\nfoo\r\n" "\n\r" }} → foo
{{ trim "\r\n\r\nfoo\r\n\r\n" "\n\r" }} → foo
```
The `strings.Trim` function is commonly used in shortcodes to remove leading and trailing newlines characters and carriage returns from the content within the opening and closing shortcode tags.
For example, with this markdown:
```text
{{</* my-shortcode */>}}
Able was I ere I saw Elba.
{{</* /my-shortcode */>}}
```
The value of `.Inner` in the shortcode template is:
```text
\nAble was I ere I saw Elba.\n
```
If authored on a Windows system the value of `.Inner` might, depending on the editor configuration, be:
```text
\r\nAble was I ere I saw Elba.\r\n
```
This construct is common in shortcode templates:
```go-html-template
{{ trim .Inner "\n\r" }}
```

View file

@ -1,28 +0,0 @@
---
title: strings.TrimLeft
description: Returns the given string, removing leading characters specified in the cutset.
categories: []
keywords: []
action:
aliases: []
related:
- functions/strings/Chomp
- functions/strings/Trim
- functions/strings/TrimPrefix
- functions/strings/TrimRight
- functions/strings/TrimSuffix
returnType: string
signatures: [strings.TrimLeft CUTSET STRING]
aliases: [/functions/strings.trimleft]
---
```go-html-template
{{ strings.TrimLeft "a" "abba" }} → bba
```
The `strings.TrimLeft` function converts the arguments to strings if possible:
```go-html-template
{{ strings.TrimLeft 21 12345 }} → 345 (string)
{{ strings.TrimLeft "rt" true }} → ue
```

View file

@ -1,23 +0,0 @@
---
title: strings.TrimPrefix
description: Returns the given string, removing the prefix from the beginning of the string.
categories: []
keywords: []
action:
aliases: []
related:
- functions/strings/Chomp
- functions/strings/Trim
- functions/strings/TrimLeft
- functions/strings/TrimRight
- functions/strings/TrimSuffix
returnType: string
signatures: [strings.TrimPrefix PREFIX STRING]
aliases: [/functions/strings.trimprefix]
---
```go-html-template
{{ strings.TrimPrefix "a" "aabbaa" }} → abbaa
{{ strings.TrimPrefix "aa" "aabbaa" }} → bbaa
{{ strings.TrimPrefix "aaa" "aabbaa" }} → aabbaa
```

View file

@ -1,28 +0,0 @@
---
title: strings.TrimRight
description: Returns the given string, removing trailing characters specified in the cutset.
categories: []
keywords: []
action:
aliases: []
related:
- functions/strings/Chomp
- functions/strings/Trim
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimSuffix
returnType: string
signatures: [strings.TrimRight CUTSET STRING]
aliases: [/functions/strings.trimright]
---
```go-html-template
{{ strings.TrimRight "a" "abba" }} → abb
```
The `strings.TrimRight` function converts the arguments to strings if possible:
```go-html-template
{{ strings.TrimRight 54 12345 }} → 123 (string)
{{ strings.TrimRight "eu" true }} → tr
```

View file

@ -1,23 +0,0 @@
---
title: strings.TrimSuffix
description: Returns the given string, removing the suffix from the end of the string.
categories: []
keywords: []
action:
aliases: []
related:
- functions/strings/Chomp
- functions/strings/Trim
- functions/strings/TrimLeft
- functions/strings/TrimPrefix
- functions/strings/TrimRight
returnType: string
signatures: [strings.TrimSuffix SUFFIX STRING]
aliases: [/functions/strings.trimsuffix]
---
```go-html-template
{{ strings.TrimSuffix "a" "aabbaa" }} → aabba
{{ strings.TrimSuffix "aa" "aabbaa" }} → aabb
{{ strings.TrimSuffix "aaa" "aabbaa" }} → aabbaa
```

View file

@ -1,24 +0,0 @@
---
title: strings.Truncate
description: Returns the given string, truncating it to a maximum length without cutting words or leaving unclosed HTML tags.
categories: []
keywords: []
action:
aliases: [truncate]
related: []
returnType: template.HTML
signatures: ['strings.Truncate SIZE [ELLIPSIS] INPUT']
aliases: [/functions/truncate]
---
Since Go templates are HTML-aware, `truncate` will intelligently handle normal strings vs HTML strings:
```go-html-template
{{ "<em>Keep my HTML</em>" | safeHTML | truncate 10 }} → <em>Keep my …</em>
```
{{% note %}}
If you have a raw string that contains HTML tags you want to remain treated as HTML, you will need to convert the string to HTML using the [`safeHTML`]function before sending the value to `truncate`. Otherwise, the HTML tags will be escaped when passed through the `truncate` function.
[`safeHTML`]: /functions/safe/html
{{% /note %}}

View file

@ -1,12 +0,0 @@
---
title: String functions
linkTitle: strings
description: Template functions to work with strings.
categories: []
keywords: []
menu:
docs:
parent: functions
---
Use these functions to work with strings.