From 02db42959f2146e939fe1fa4736bab1969bd95e0 Mon Sep 17 00:00:00 2001 From: Aofei Sheng Date: Fri, 12 Mar 2021 03:28:43 +0800 Subject: [PATCH] feat: add str2html to built-in HTML template functions --- air.go | 8 +++++--- renderer.go | 24 +++++++++++++++--------- renderer_test.go | 13 +++++++++---- 3 files changed, 29 insertions(+), 16 deletions(-) diff --git a/air.go b/air.go index 3b8a285..1234ef7 100644 --- a/air.go +++ b/air.go @@ -419,6 +419,11 @@ type Air struct { // The HTML template functions described in // https://pkg.go.dev/text/template#hdr-Functions and the following are // always available: + // * locstr + // Returns a localized string for its argument. It works exactly + // the same as the `Request.LocalizedString` + // * str2html + // Returns a `template.HTML` for its argument. // * strlen // Returns the number of characters of its argument. // * substr @@ -429,9 +434,6 @@ type Air struct { // * timefmt // Returns a textual representation of its first argument for the // time layout (the second argument). - // * locstr - // Returns a localized string for its argument. It works exactly - // the same as the `Request.LocalizedString` // // Default value: nil RendererTemplateFuncMap template.FuncMap `mapstructure:"-"` diff --git a/renderer.go b/renderer.go index e88d7a4..fdb4dfb 100644 --- a/renderer.go +++ b/renderer.go @@ -75,10 +75,11 @@ func (r *renderer) load() { r.a.RendererTemplateRightDelim, ). Funcs(template.FuncMap{ - "strlen": strlen, - "substr": substr, - "timefmt": timefmt, - "locstr": locstr, + "locstr": locstr, + "str2html": str2html, + "strlen": strlen, + "substr": substr, + "timefmt": timefmt, }). Funcs(r.a.RendererTemplateFuncMap) if r.loadError = filepath.Walk( @@ -142,6 +143,16 @@ func (r *renderer) render( }).Execute(w, v) } +// locstr returns the key without any changes. +func locstr(key string) string { + return key +} + +// str2html returns a `template.HTML` for the s. +func str2html(s string) template.HTML { + return template.HTML(s) +} + // strlen returns the number of characters of the s. func strlen(s string) int { return len([]rune(s)) @@ -158,8 +169,3 @@ func substr(s string, i, j int) string { func timefmt(t time.Time, layout string) string { return t.Format(layout) } - -// locstr returns the key without any changes. -func locstr(key string) string { - return key -} diff --git a/renderer_test.go b/renderer_test.go index e8ea7fc..e056d09 100644 --- a/renderer_test.go +++ b/renderer_test.go @@ -1,6 +1,7 @@ package air import ( + "html/template" "io/ioutil" "os" "path/filepath" @@ -78,6 +79,14 @@ func TestRendererRender(t *testing.T) { assert.NoError(t, r.render(ioutil.Discard, "test.html", nil, locstr)) } +func TestLocstr(t *testing.T) { + assert.Equal(t, "Foobar", locstr("Foobar")) +} + +func TestStr2html(t *testing.T) { + assert.Equal(t, template.HTML("Foobar"), str2html("Foobar")) +} + func TestStrlen(t *testing.T) { assert.Equal(t, 6, strlen("Foobar")) assert.Equal(t, 2, strlen("测试")) @@ -95,7 +104,3 @@ func TestTimefmt(t *testing.T) { timefmt(time.Unix(0, 0).UTC(), time.RFC3339), ) } - -func TestLocstr(t *testing.T) { - assert.Equal(t, "Foobar", locstr("Foobar")) -}