forked from terrastruct/d2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd2.go
58 lines (48 loc) · 1.21 KB
/
d2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package d2
import (
"context"
"errors"
"os"
"strings"
"oss.terrastruct.com/d2/d2compiler"
"oss.terrastruct.com/d2/d2exporter"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2renderers/textmeasure"
"oss.terrastruct.com/d2/d2target"
)
type CompileOptions struct {
UTF16 bool
MeasuredTexts []*d2target.MText
Ruler *textmeasure.Ruler
Layout func(context.Context, *d2graph.Graph) error
ThemeID int64
}
func Compile(ctx context.Context, input string, opts *CompileOptions) (*d2target.Diagram, error) {
if opts == nil {
opts = &CompileOptions{}
}
g, err := d2compiler.Compile("", strings.NewReader(input), &d2compiler.CompileOptions{
UTF16: opts.UTF16,
})
if err != nil {
return nil, err
}
err = g.SetDimensions(opts.MeasuredTexts, opts.Ruler)
if err != nil {
return nil, err
}
if opts.Layout != nil {
err = opts.Layout(ctx, g)
} else if os.Getenv("D2_LAYOUT") == "dagre" && dagreLayout != nil {
err = dagreLayout(ctx, g)
} else {
err = errors.New("no available layout")
}
if err != nil {
return nil, err
}
diagram, err := d2exporter.Export(ctx, g, opts.ThemeID)
return diagram, err
}
// See c.go
var dagreLayout func(context.Context, *d2graph.Graph) error