github.com/graemephi/kahugo@v0.62.3-0.20211121071557-d78c0423784d/tpl/transform/transform.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 provides template functions for transforming content. 15 package transform 16 17 import ( 18 "html" 19 "html/template" 20 21 "github.com/gohugoio/hugo/cache/namedmemcache" 22 23 "github.com/gohugoio/hugo/deps" 24 "github.com/gohugoio/hugo/helpers" 25 "github.com/spf13/cast" 26 ) 27 28 // New returns a new instance of the transform-namespaced template functions. 29 func New(deps *deps.Deps) *Namespace { 30 cache := namedmemcache.New() 31 deps.BuildStartListeners.Add( 32 func() { 33 cache.Clear() 34 }) 35 36 return &Namespace{ 37 cache: cache, 38 deps: deps, 39 } 40 } 41 42 // Namespace provides template functions for the "transform" namespace. 43 type Namespace struct { 44 cache *namedmemcache.Cache 45 deps *deps.Deps 46 } 47 48 // Emojify returns a copy of s with all emoji codes replaced with actual emojis. 49 // 50 // See http://www.emoji-cheat-sheet.com/ 51 func (ns *Namespace) Emojify(s interface{}) (template.HTML, error) { 52 ss, err := cast.ToStringE(s) 53 if err != nil { 54 return "", err 55 } 56 57 return template.HTML(helpers.Emojify([]byte(ss))), nil 58 } 59 60 // Highlight returns a copy of s as an HTML string with syntax 61 // highlighting applied. 62 func (ns *Namespace) Highlight(s interface{}, lang, opts string) (template.HTML, error) { 63 ss, err := cast.ToStringE(s) 64 if err != nil { 65 return "", err 66 } 67 68 highlighted, _ := ns.deps.ContentSpec.Converters.Highlight(ss, lang, opts) 69 return template.HTML(highlighted), nil 70 } 71 72 // HTMLEscape returns a copy of s with reserved HTML characters escaped. 73 func (ns *Namespace) HTMLEscape(s interface{}) (string, error) { 74 ss, err := cast.ToStringE(s) 75 if err != nil { 76 return "", err 77 } 78 79 return html.EscapeString(ss), nil 80 } 81 82 // HTMLUnescape returns a copy of with HTML escape requences converted to plain 83 // text. 84 func (ns *Namespace) HTMLUnescape(s interface{}) (string, error) { 85 ss, err := cast.ToStringE(s) 86 if err != nil { 87 return "", err 88 } 89 90 return html.UnescapeString(ss), nil 91 } 92 93 // Markdownify renders a given input from Markdown to HTML. 94 func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) { 95 ss, err := cast.ToStringE(s) 96 if err != nil { 97 return "", err 98 } 99 100 b, err := ns.deps.ContentSpec.RenderMarkdown([]byte(ss)) 101 if err != nil { 102 return "", err 103 } 104 105 // Strip if this is a short inline type of text. 106 b = ns.deps.ContentSpec.TrimShortHTML(b) 107 108 return helpers.BytesToHTML(b), nil 109 } 110 111 // Plainify returns a copy of s with all HTML tags removed. 112 func (ns *Namespace) Plainify(s interface{}) (string, error) { 113 ss, err := cast.ToStringE(s) 114 if err != nil { 115 return "", err 116 } 117 118 return helpers.StripHTML(ss), nil 119 }