github.com/kovansky/hugo@v0.92.3-0.20220224232819-63076e4ff19f/tpl/diagrams/diagrams.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 SVGDiagram interface { 28 Body() template.HTML 29 SVG() template.HTML 30 Width() int 31 Height() int 32 } 33 34 type goatDiagram struct { 35 d goat.SVG 36 } 37 38 func (d goatDiagram) Body() template.HTML { 39 return template.HTML(d.d.Body) 40 } 41 42 func (d goatDiagram) SVG() template.HTML { 43 return template.HTML(d.d.String()) 44 } 45 46 func (d goatDiagram) Width() int { 47 return d.d.Width 48 } 49 50 func (d goatDiagram) Height() int { 51 return d.d.Height 52 } 53 54 type Diagrams struct { 55 d *deps.Deps 56 } 57 58 func (d *Diagrams) Goat(v interface{}) SVGDiagram { 59 var r io.Reader 60 61 switch vv := v.(type) { 62 case io.Reader: 63 r = vv 64 case []byte: 65 r = bytes.NewReader(vv) 66 default: 67 r = strings.NewReader(cast.ToString(v)) 68 } 69 70 return goatDiagram{ 71 d: goat.BuildSVG(r), 72 } 73 }