|
| 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/gomarkdown/markdown" |
| 22 | + |
| 23 | + "hugo-renderer/deps" |
| 24 | + |
| 25 | + "hugo-renderer/helpers" |
| 26 | + |
| 27 | + "github.com/spf13/cast" |
| 28 | +) |
| 29 | + |
| 30 | +// New returns a new instance of the transform-namespaced template functions. |
| 31 | +func New(deps *deps.Deps) *Namespace { |
| 32 | + // Bookshop: Build listeners & cache intentionally removed |
| 33 | + |
| 34 | + return &Namespace{ |
| 35 | + deps: deps, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// Namespace provides template functions for the "transform" namespace. |
| 40 | +type Namespace struct { |
| 41 | + // Bookshop: Build listeners & cache intentionally removed |
| 42 | + deps *deps.Deps |
| 43 | +} |
| 44 | + |
| 45 | +// Emojify returns a copy of s with all emoji codes replaced with actual emojis. |
| 46 | +// |
| 47 | +// See http://www.emoji-cheat-sheet.com/ |
| 48 | +func (ns *Namespace) Emojify(s interface{}) (template.HTML, error) { |
| 49 | + ss, err := cast.ToStringE(s) |
| 50 | + if err != nil { |
| 51 | + return "", err |
| 52 | + } |
| 53 | + |
| 54 | + return template.HTML(helpers.Emojify([]byte(ss))), nil |
| 55 | +} |
| 56 | + |
| 57 | +// Highlight returns a copy of s as an HTML string with syntax |
| 58 | +// highlighting applied. |
| 59 | +func (ns *Namespace) Highlight(s interface{}, lang string, opts ...interface{}) (template.HTML, error) { |
| 60 | + ss, err := cast.ToStringE(s) |
| 61 | + if err != nil { |
| 62 | + return "", err |
| 63 | + } |
| 64 | + |
| 65 | + // Bookshop: Highlighting logic intentionally removed for bloat |
| 66 | + |
| 67 | + return template.HTML(ss), nil |
| 68 | +} |
| 69 | + |
| 70 | +// HTMLEscape returns a copy of s with reserved HTML characters escaped. |
| 71 | +func (ns *Namespace) HTMLEscape(s interface{}) (string, error) { |
| 72 | + ss, err := cast.ToStringE(s) |
| 73 | + if err != nil { |
| 74 | + return "", err |
| 75 | + } |
| 76 | + |
| 77 | + return html.EscapeString(ss), nil |
| 78 | +} |
| 79 | + |
| 80 | +// HTMLUnescape returns a copy of with HTML escape requences converted to plain |
| 81 | +// text. |
| 82 | +func (ns *Namespace) HTMLUnescape(s interface{}) (string, error) { |
| 83 | + ss, err := cast.ToStringE(s) |
| 84 | + if err != nil { |
| 85 | + return "", err |
| 86 | + } |
| 87 | + |
| 88 | + return html.UnescapeString(ss), nil |
| 89 | +} |
| 90 | + |
| 91 | +// Markdownify renders a given input from Markdown to HTML. |
| 92 | +func (ns *Namespace) Markdownify(s interface{}) (template.HTML, error) { |
| 93 | + ss, err := cast.ToStringE(s) |
| 94 | + if err != nil { |
| 95 | + return "", err |
| 96 | + } |
| 97 | + |
| 98 | + // Bookshop: markdownify implementation has been intentionally modified for bloat |
| 99 | + // This includes using a different (smaller) markdown implementation. |
| 100 | + output := markdown.ToHTML([]byte(ss), nil, nil) |
| 101 | + |
| 102 | + return helpers.BytesToHTML(output), nil |
| 103 | +} |
| 104 | + |
| 105 | +// Plainify returns a copy of s with all HTML tags removed. |
| 106 | +func (ns *Namespace) Plainify(s interface{}) (string, error) { |
| 107 | + ss, err := cast.ToStringE(s) |
| 108 | + if err != nil { |
| 109 | + return "", err |
| 110 | + } |
| 111 | + |
| 112 | + return helpers.StripHTML(ss), nil |
| 113 | +} |
0 commit comments