| 1 |
--- |
| 2 |
title: diagrams.Goat |
| 3 |
description: Returns an SVGDiagram object created from the given GoAT markup and options. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [] |
| 9 |
returnType: diagrams.SVGDiagram |
| 10 |
signatures: [diagrams.Goat MARKUP] |
| 11 |
--- |
| 12 |
|
| 13 |
Useful in a [code block render hook][], the `diagrams.Goat` function returns an SVGDiagram object created from the given [GoAT][] markup. |
| 14 |
|
| 15 |
## Methods |
| 16 |
|
| 17 |
Use these methods on the `SVGDiagram` object. |
| 18 |
|
| 19 |
`Inner` |
| 20 |
: (`template.HTML`) Returns the SVG child elements without a wrapping `svg` element, allowing you to create your own wrapper. |
| 21 |
|
| 22 |
`Wrapped` |
| 23 |
: (`template.HTML`) Returns the SVG child elements wrapped in an `svg` element. |
| 24 |
|
| 25 |
`Width` |
| 26 |
: (`int`) Returns the width of the rendered diagram, in pixels. |
| 27 |
|
| 28 |
`Height` |
| 29 |
: (`int`) Returns the height of the rendered diagram, in pixels. |
| 30 |
|
| 31 |
## GoAT Diagrams |
| 32 |
|
| 33 |
Hugo natively supports GoAT diagrams with an [embedded code block render hook][]. |
| 34 |
|
| 35 |
This Markdown: |
| 36 |
|
| 37 |
````md |
| 38 |
```goat |
| 39 |
.---. .-. .-. .-. .---. |
| 40 |
| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B | |
| 41 |
'---' '-' '+' '+' '---' |
| 42 |
``` |
| 43 |
```` |
| 44 |
|
| 45 |
Is rendered to: |
| 46 |
|
| 47 |
```html |
| 48 |
<div class="goat svg-container"> |
| 49 |
<svg xmlns="http://www.w3.org/2000/svg" font-family="Menlo,Lucida Console,monospace" viewBox="0 0 352 57"> |
| 50 |
... |
| 51 |
</svg> |
| 52 |
</div> |
| 53 |
``` |
| 54 |
|
| 55 |
Which appears in your browser as: |
| 56 |
|
| 57 |
```goat {class="mw6-ns"} |
| 58 |
.---. .-. .-. .-. .---. |
| 59 |
| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B | |
| 60 |
'---' '-' '+' '+' '---' |
| 61 |
``` |
| 62 |
|
| 63 |
To customize rendering, override Hugo's [embedded code block render hook][] for GoAT diagrams. |
| 64 |
|
| 65 |
## Code block render hook |
| 66 |
|
| 67 |
By way of example, let's create a code block render hook to render GoAT diagrams as `figure` elements with an optional caption. |
| 68 |
|
| 69 |
```go-html-template {file="layouts/_markup/render-codeblock-goat.html"} |
| 70 |
{{ $caption := or .Attributes.caption "" }} |
| 71 |
{{ $class := or .Attributes.class "diagram" }} |
| 72 |
{{ $id := or .Attributes.id (printf "diagram-%d" (add 1 .Ordinal)) }} |
| 73 |
|
| 74 |
<figure id="{{ $id }}"> |
| 75 |
{{ with diagrams.Goat (trim .Inner "\n\r") }} |
| 76 |
<svg class="{{ $class }}" width="{{ .Width }}" height="{{ .Height }}" xmlns="http://www.w3.org/2000/svg" version="1.1"> |
| 77 |
{{ .Inner }} |
| 78 |
</svg> |
| 79 |
{{ end }} |
| 80 |
<figcaption>{{ $caption }}</figcaption> |
| 81 |
</figure> |
| 82 |
``` |
| 83 |
|
| 84 |
This Markdown: |
| 85 |
|
| 86 |
````md {file="content/example.md" } |
| 87 |
```goat {class="foo" caption="Diagram 1: Example"} |
| 88 |
.---. .-. .-. .-. .---. |
| 89 |
| A +--->| 1 |<--->| 2 |<--->| 3 |<---+ B | |
| 90 |
'---' '-' '+' '+' '---' |
| 91 |
``` |
| 92 |
```` |
| 93 |
|
| 94 |
Is rendered to: |
| 95 |
|
| 96 |
```html |
| 97 |
<figure id="diagram-1"> |
| 98 |
<svg class="foo" width="272" height="57" xmlns="http://www.w3.org/2000/svg" version="1.1"> |
| 99 |
... |
| 100 |
</svg> |
| 101 |
<figcaption>Diagram 1: Example</figcaption> |
| 102 |
</figure> |
| 103 |
``` |
| 104 |
|
| 105 |
Use CSS to style the SVG as needed: |
| 106 |
|
| 107 |
```css |
| 108 |
svg.foo { |
| 109 |
font-family: "Segoe UI","Noto Sans",Helvetica,Arial,sans-serif |
| 110 |
} |
| 111 |
``` |
| 112 |
|
| 113 |
[GoAT]: https://github.com/bep/goat |
| 114 |
[code block render hook]: /render-hooks/code-blocks/ |
| 115 |
[embedded code block render hook]: <{{% eturl render-codeblock-goat %}}> |