Skip to content

Commit

Permalink
add TrimPrefix, ToUpper, and ToLower template funcs
Browse files Browse the repository at this point in the history
We need TrimPrefix and ToUpper for some links at Tailscale, and might as
well ToLower as well at that point.

Updates #10

Change-Id: Ia101a4a3005adb9118051b3416f5a64a4a45987d
Signed-off-by: Will Norris <[email protected]>
  • Loading branch information
willnorris committed Sep 20, 2024
1 parent 701c466 commit b3ab9a6
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
3 changes: 3 additions & 0 deletions golink.go
Original file line number Diff line number Diff line change
Expand Up @@ -651,7 +651,10 @@ func (e expandEnv) User() (string, error) {
var expandFuncMap = texttemplate.FuncMap{
"PathEscape": url.PathEscape,
"QueryEscape": url.QueryEscape,
"TrimPrefix": strings.TrimPrefix,
"TrimSuffix": strings.TrimSuffix,
"ToLower": strings.ToLower,
"ToUpper": strings.ToUpper,
"Match": regexMatch,
}

Expand Down
18 changes: 18 additions & 0 deletions golink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,30 @@ func TestExpandLink(t *testing.T) {
remainder: "a/b+c",
want: "http://host.com/a%2Fb%2Bc",
},
{
name: "template-with-trimprefix-func",
long: `http://host.com/{{TrimPrefix .Path "BUG-"}}`,
remainder: "BUG-123",
want: "http://host.com/123",
},
{
name: "template-with-trimsuffix-func",
long: `http://host.com/{{TrimSuffix .Path "/"}}`,
remainder: "a/",
want: "http://host.com/a",
},
{
name: "template-with-tolower-func",
long: `http://host.com/{{ToLower .Path}}`,
remainder: "BUG-123",
want: "http://host.com/bug-123",
},
{
name: "template-with-toupper-func",
long: `http://host.com/{{ToUpper .Path}}`,
remainder: "bug-123",
want: "http://host.com/BUG-123",
},
{
name: "template-with-match-func",
long: `http://host.com/{{if Match "\\d+" .Path}}id/{{.Path}}{{else}}search/{{.Path}}{{end}}`,
Expand Down
3 changes: 3 additions & 0 deletions tmpl/help.html
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ <h2 id="advanced">Advanced destination links</h2>
<ul>
<li><code>PathEscape</code> is the <a href="https://pkg.go.dev/net/url#PathEscape">url.PathEscape</a> function for escaping values inside a URL path.
<li><code>QueryEscape</code> is the <a href="https://pkg.go.dev/net/url#QueryEscape">url.QueryEscape</a> function for escaping values inside a URL query.
<li><code>TrimPrefix</code> is the <a href="https://pkg.go.dev/strings#TrimPrefix">strings.TrimPrefix</a> function for removing a leading prefix.
<li><code>TrimSuffix</code> is the <a href="https://pkg.go.dev/strings#TrimSuffix">strings.TrimSuffix</a> function for removing a trailing suffix.
<li><code>ToLower</code> is the <a href="https://pkg.go.dev/strings#ToLower">strings.ToLower</a> function for mapping all Unicode letters to their lower case.
<li><code>ToUpper</code> is the <a href="https://pkg.go.dev/strings#ToUpper">strings.ToUpper</a> function for mapping all Unicode letters to their upper case.
<li><code>Match</code> is the <a href="https://pkg.go.dev/regexp#MatchString">regexp.MatchString</a> function for matching a regular expression pattern.
</ul>

Expand Down

0 comments on commit b3ab9a6

Please sign in to comment.