github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/tpl/diagrams/goat.go (about) 1 // Copyright 2022 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 diagrams 15 16 import ( 17 "bytes" 18 "html/template" 19 "io" 20 "strings" 21 22 "github.com/bep/goat" 23 "github.com/gohugoio/hugo/deps" 24 "github.com/spf13/cast" 25 ) 26 27 type goatDiagram struct { 28 d goat.SVG 29 } 30 31 func (d goatDiagram) Inner() template.HTML { 32 return template.HTML(d.d.Body) 33 } 34 35 func (d goatDiagram) Wrapped() template.HTML { 36 return template.HTML(d.d.String()) 37 } 38 39 func (d goatDiagram) Width() int { 40 return d.d.Width 41 } 42 43 func (d goatDiagram) Height() int { 44 return d.d.Height 45 } 46 47 // Namespace provides template functions for the diagrams namespace. 48 type Namespace struct { 49 d *deps.Deps 50 } 51 52 // Goat creates a new SVG diagram from input v. 53 func (d *Namespace) Goat(v any) SVGDiagram { 54 var r io.Reader 55 56 switch vv := v.(type) { 57 case io.Reader: 58 r = vv 59 case []byte: 60 r = bytes.NewReader(vv) 61 default: 62 r = strings.NewReader(cast.ToString(v)) 63 } 64 65 return goatDiagram{ 66 d: goat.BuildSVG(r), 67 } 68 }