2022-12-01 05:14:38 +00:00
|
|
|
package d2lib
|
2022-11-03 13:54:49 +00:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"errors"
|
|
|
|
|
"os"
|
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
"oss.terrastruct.com/d2/d2compiler"
|
|
|
|
|
"oss.terrastruct.com/d2/d2exporter"
|
|
|
|
|
"oss.terrastruct.com/d2/d2graph"
|
2022-12-30 05:09:53 +00:00
|
|
|
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
|
2022-12-26 00:33:32 +00:00
|
|
|
"oss.terrastruct.com/d2/d2layouts/d2near"
|
2022-12-01 22:32:54 +00:00
|
|
|
"oss.terrastruct.com/d2/d2layouts/d2sequence"
|
2022-12-21 07:43:45 +00:00
|
|
|
"oss.terrastruct.com/d2/d2renderers/d2fonts"
|
2022-11-03 13:54:49 +00:00
|
|
|
"oss.terrastruct.com/d2/d2target"
|
2022-12-01 13:46:45 +00:00
|
|
|
"oss.terrastruct.com/d2/lib/textmeasure"
|
2022-11-03 13:54:49 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type CompileOptions struct {
|
|
|
|
|
UTF16 bool
|
|
|
|
|
MeasuredTexts []*d2target.MText
|
|
|
|
|
Ruler *textmeasure.Ruler
|
|
|
|
|
Layout func(context.Context, *d2graph.Graph) error
|
2023-03-14 17:40:52 +00:00
|
|
|
ThemeID int64
|
2022-11-03 13:54:49 +00:00
|
|
|
|
2022-12-21 07:43:45 +00:00
|
|
|
// FontFamily controls the font family used for all texts that are not the following:
|
|
|
|
|
// - code
|
|
|
|
|
// - latex
|
|
|
|
|
// - pre-measured (web setting)
|
|
|
|
|
// TODO maybe some will want to configure code font too, but that's much lower priority
|
|
|
|
|
FontFamily *d2fonts.FontFamily
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-01 05:50:55 +00:00
|
|
|
func Compile(ctx context.Context, input string, opts *CompileOptions) (*d2target.Diagram, *d2graph.Graph, error) {
|
2022-11-03 13:54:49 +00:00
|
|
|
if opts == nil {
|
|
|
|
|
opts = &CompileOptions{}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
g, err := d2compiler.Compile("", strings.NewReader(input), &d2compiler.CompileOptions{
|
|
|
|
|
UTF16: opts.UTF16,
|
|
|
|
|
})
|
|
|
|
|
if err != nil {
|
2022-12-01 05:50:55 +00:00
|
|
|
return nil, nil, err
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-01-27 18:41:25 +00:00
|
|
|
d, err := compile(ctx, g, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, nil, err
|
|
|
|
|
}
|
|
|
|
|
return d, g, nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func compile(ctx context.Context, g *d2graph.Graph, opts *CompileOptions) (*d2target.Diagram, error) {
|
2023-03-14 17:40:52 +00:00
|
|
|
err := g.ApplyTheme(opts.ThemeID)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-05 17:48:38 +00:00
|
|
|
if len(g.Objects) > 0 {
|
2023-01-27 18:41:25 +00:00
|
|
|
err := g.SetDimensions(opts.MeasuredTexts, opts.Ruler, opts.FontFamily)
|
2022-12-05 17:48:38 +00:00
|
|
|
if err != nil {
|
2023-01-27 18:41:25 +00:00
|
|
|
return nil, err
|
2022-12-05 17:48:38 +00:00
|
|
|
}
|
2022-11-03 13:54:49 +00:00
|
|
|
|
2022-12-26 00:33:32 +00:00
|
|
|
coreLayout, err := getLayout(opts)
|
|
|
|
|
if err != nil {
|
2023-01-27 18:41:25 +00:00
|
|
|
return nil, err
|
2022-12-26 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-24 18:09:28 +00:00
|
|
|
constantNearGraphs := d2near.WithoutConstantNears(ctx, g)
|
2023-03-21 12:42:10 +00:00
|
|
|
|
|
|
|
|
// run core layout for constantNears
|
2023-03-24 18:38:05 +00:00
|
|
|
for _, tempGraph := range constantNearGraphs {
|
2023-03-21 12:42:10 +00:00
|
|
|
if err = coreLayout(ctx, tempGraph); err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2023-04-04 10:33:23 +00:00
|
|
|
tempGraph.Root.ChildrenArray[0].Parent = g.Root
|
2023-03-30 12:31:42 +00:00
|
|
|
for _, obj := range tempGraph.Objects {
|
|
|
|
|
obj.Graph = g
|
|
|
|
|
}
|
2023-03-21 12:42:10 +00:00
|
|
|
}
|
2022-12-26 00:33:32 +00:00
|
|
|
|
|
|
|
|
err = d2sequence.Layout(ctx, g, coreLayout)
|
|
|
|
|
if err != nil {
|
2023-01-27 18:41:25 +00:00
|
|
|
return nil, err
|
2022-12-26 00:33:32 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-24 18:09:28 +00:00
|
|
|
err = d2near.Layout(ctx, g, constantNearGraphs)
|
2022-12-26 00:33:32 +00:00
|
|
|
if err != nil {
|
2023-01-27 18:41:25 +00:00
|
|
|
return nil, err
|
2022-12-05 17:48:38 +00:00
|
|
|
}
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2023-02-19 11:32:44 +00:00
|
|
|
d, err := d2exporter.Export(ctx, g, opts.FontFamily)
|
2023-01-27 18:41:25 +00:00
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, l := range g.Layers {
|
|
|
|
|
ld, err := compile(ctx, l, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
d.Layers = append(d.Layers, ld)
|
|
|
|
|
}
|
|
|
|
|
for _, l := range g.Scenarios {
|
|
|
|
|
ld, err := compile(ctx, l, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
d.Scenarios = append(d.Scenarios, ld)
|
|
|
|
|
}
|
|
|
|
|
for _, l := range g.Steps {
|
|
|
|
|
ld, err := compile(ctx, l, opts)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
d.Steps = append(d.Steps, ld)
|
|
|
|
|
}
|
|
|
|
|
return d, nil
|
2022-11-03 13:54:49 +00:00
|
|
|
}
|
|
|
|
|
|
2022-12-01 18:33:20 +00:00
|
|
|
func getLayout(opts *CompileOptions) (func(context.Context, *d2graph.Graph) error, error) {
|
|
|
|
|
if opts.Layout != nil {
|
|
|
|
|
return opts.Layout, nil
|
2022-12-30 05:09:53 +00:00
|
|
|
} else if os.Getenv("D2_LAYOUT") == "dagre" {
|
|
|
|
|
defaultLayout := func(ctx context.Context, g *d2graph.Graph) error {
|
|
|
|
|
return d2dagrelayout.Layout(ctx, g, nil)
|
|
|
|
|
}
|
|
|
|
|
return defaultLayout, nil
|
2022-12-01 18:33:20 +00:00
|
|
|
} else {
|
|
|
|
|
return nil, errors.New("no available layout")
|
|
|
|
|
}
|
|
|
|
|
}
|