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