github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/transform/init.go (about)

     1  // Copyright 2017 The Hugo Authors. All rights reserved.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  // http://www.apache.org/licenses/LICENSE-2.0
     7  //
     8  // Unless required by applicable law or agreed to in writing, software
     9  // distributed under the License is distributed on an "AS IS" BASIS,
    10  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package transform
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/gohugoio/hugo/deps"
    20  	"github.com/gohugoio/hugo/tpl/internal"
    21  )
    22  
    23  const name = "transform"
    24  
    25  func init() {
    26  	f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
    27  		ctx := New(d)
    28  
    29  		ns := &internal.TemplateFuncsNamespace{
    30  			Name:    name,
    31  			Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil },
    32  		}
    33  
    34  		ns.AddMethodMapping(ctx.Emojify,
    35  			[]string{"emojify"},
    36  			[][2]string{
    37  				{`{{ "I :heart: Hugo" | emojify }}`, `I ❤️ Hugo`},
    38  			},
    39  		)
    40  
    41  		ns.AddMethodMapping(ctx.Highlight,
    42  			[]string{"highlight"},
    43  			[][2]string{},
    44  		)
    45  
    46  		ns.AddMethodMapping(ctx.HTMLEscape,
    47  			[]string{"htmlEscape"},
    48  			[][2]string{
    49  				{
    50  					`{{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | safeHTML }}`,
    51  					`Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;`,
    52  				},
    53  				{
    54  					`{{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" }}`,
    55  					`Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt;`,
    56  				},
    57  				{
    58  					`{{ htmlEscape "Cathal Garvey & The Sunshine Band <cathal@foo.bar>" | htmlUnescape | safeHTML }}`,
    59  					`Cathal Garvey & The Sunshine Band <cathal@foo.bar>`,
    60  				},
    61  			},
    62  		)
    63  
    64  		ns.AddMethodMapping(ctx.HTMLUnescape,
    65  			[]string{"htmlUnescape"},
    66  			[][2]string{
    67  				{
    68  					`{{ htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | safeHTML }}`,
    69  					`Cathal Garvey & The Sunshine Band <cathal@foo.bar>`,
    70  				},
    71  				{
    72  					`{{ "Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt;" | htmlUnescape | htmlUnescape | safeHTML }}`,
    73  					`Cathal Garvey & The Sunshine Band <cathal@foo.bar>`,
    74  				},
    75  				{
    76  					`{{ "Cathal Garvey &amp;amp; The Sunshine Band &amp;lt;cathal@foo.bar&amp;gt;" | htmlUnescape | htmlUnescape }}`,
    77  					`Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;`,
    78  				},
    79  				{
    80  					`{{ htmlUnescape "Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;" | htmlEscape | safeHTML }}`,
    81  					`Cathal Garvey &amp; The Sunshine Band &lt;cathal@foo.bar&gt;`,
    82  				},
    83  			},
    84  		)
    85  
    86  		ns.AddMethodMapping(ctx.Markdownify,
    87  			[]string{"markdownify"},
    88  			[][2]string{
    89  				{`{{ .Title | markdownify }}`, `<strong>BatMan</strong>`},
    90  			},
    91  		)
    92  
    93  		ns.AddMethodMapping(ctx.Plainify,
    94  			[]string{"plainify"},
    95  			[][2]string{
    96  				{`{{ plainify  "Hello <strong>world</strong>, gophers!" }}`, `Hello world, gophers!`},
    97  			},
    98  		)
    99  
   100  		ns.AddMethodMapping(ctx.Remarshal,
   101  			nil,
   102  			[][2]string{
   103  				{`{{ "title = \"Hello World\"" | transform.Remarshal "json" | safeHTML }}`, "{\n   \"title\": \"Hello World\"\n}\n"},
   104  			},
   105  		)
   106  
   107  		ns.AddMethodMapping(ctx.Unmarshal,
   108  			[]string{"unmarshal"},
   109  			[][2]string{
   110  				{`{{ "hello = \"Hello World\"" | transform.Unmarshal }}`, "map[hello:Hello World]"},
   111  				{`{{ "hello = \"Hello World\"" | resources.FromString "data/greetings.toml" | transform.Unmarshal }}`, "map[hello:Hello World]"},
   112  			},
   113  		)
   114  
   115  		return ns
   116  	}
   117  
   118  	internal.AddTemplateFuncsNamespace(f)
   119  }