d2/d2lib/d2.go

228 lines
5.9 KiB
Go
Raw Normal View History

2022-12-01 05:14:38 +00:00
package d2lib
import (
"context"
"errors"
2023-06-12 01:36:50 +00:00
"io/fs"
"os"
"strings"
2023-11-28 22:06:48 +00:00
"oss.terrastruct.com/d2/d2ast"
"oss.terrastruct.com/d2/d2compiler"
"oss.terrastruct.com/d2/d2exporter"
"oss.terrastruct.com/d2/d2graph"
2023-09-15 21:44:20 +00:00
"oss.terrastruct.com/d2/d2layouts"
2022-12-30 05:09:53 +00:00
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
2023-11-28 22:06:48 +00:00
"oss.terrastruct.com/d2/d2parser"
2022-12-21 07:43:45 +00:00
"oss.terrastruct.com/d2/d2renderers/d2fonts"
2023-07-14 20:08:26 +00:00
"oss.terrastruct.com/d2/d2renderers/d2svg"
"oss.terrastruct.com/d2/d2target"
2023-07-14 20:08:26 +00:00
"oss.terrastruct.com/d2/d2themes/d2themescatalog"
"oss.terrastruct.com/d2/lib/textmeasure"
2023-07-14 20:08:26 +00:00
"oss.terrastruct.com/util-go/go2"
)
type CompileOptions struct {
UTF16Pos bool
2023-07-14 20:08:26 +00:00
FS fs.FS
MeasuredTexts []*d2target.MText
Ruler *textmeasure.Ruler
RouterResolver func(engine string) (d2graph.RouteEdges, error)
2023-07-14 20:08:26 +00:00
LayoutResolver func(engine string) (d2graph.LayoutGraph, error)
Layout *string
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
2023-06-06 20:30:01 +00:00
InputPath string
}
2023-11-28 22:06:48 +00:00
func Parse(ctx context.Context, input string, compileOpts *CompileOptions) (*d2ast.Map, error) {
if compileOpts == nil {
compileOpts = &CompileOptions{}
}
ast, err := d2parser.Parse(compileOpts.InputPath, strings.NewReader(input), &d2parser.ParseOptions{
UTF16Pos: compileOpts.UTF16Pos,
})
return ast, err
}
2023-07-14 20:08:26 +00:00
func Compile(ctx context.Context, input string, compileOpts *CompileOptions, renderOpts *d2svg.RenderOpts) (*d2target.Diagram, *d2graph.Graph, error) {
if compileOpts == nil {
compileOpts = &CompileOptions{}
}
if renderOpts == nil {
renderOpts = &d2svg.RenderOpts{}
}
2023-07-14 20:08:26 +00:00
g, config, err := d2compiler.Compile(compileOpts.InputPath, strings.NewReader(input), &d2compiler.CompileOptions{
UTF16Pos: compileOpts.UTF16Pos,
FS: compileOpts.FS,
})
if err != nil {
return nil, nil, err
}
2023-07-14 20:08:26 +00:00
applyConfigs(config, compileOpts, renderOpts)
applyDefaults(compileOpts, renderOpts)
2024-10-15 22:37:49 +00:00
if config != nil {
g.Data = config.Data
}
2023-07-14 20:08:26 +00:00
d, err := compile(ctx, g, compileOpts, renderOpts)
2023-07-29 18:31:03 +00:00
if d != nil {
if config == nil {
config = &d2target.Config{}
}
// These are fields that affect a diagram's appearance, so feed them back
// into diagram.Config to ensure the hash computed for CSS styling purposes
// is unique to its appearance
config.ThemeID = renderOpts.ThemeID
config.DarkThemeID = renderOpts.DarkThemeID
config.Sketch = renderOpts.Sketch
2023-07-29 18:31:03 +00:00
d.Config = config
}
2023-07-14 20:08:26 +00:00
return d, g, err
}
2023-07-14 20:08:26 +00:00
func compile(ctx context.Context, g *d2graph.Graph, compileOpts *CompileOptions, renderOpts *d2svg.RenderOpts) (*d2target.Diagram, error) {
err := g.ApplyTheme(*renderOpts.ThemeID)
2023-03-14 17:40:52 +00:00
if err != nil {
return nil, err
}
2022-12-05 17:48:38 +00:00
if len(g.Objects) > 0 {
2023-07-14 20:08:26 +00:00
err := g.SetDimensions(compileOpts.MeasuredTexts, compileOpts.Ruler, compileOpts.FontFamily)
2022-12-05 17:48:38 +00:00
if err != nil {
return nil, err
2022-12-05 17:48:38 +00:00
}
2023-07-14 20:08:26 +00:00
coreLayout, err := getLayout(compileOpts)
2022-12-26 00:33:32 +00:00
if err != nil {
return nil, err
2022-12-26 00:33:32 +00:00
}
edgeRouter, err := getEdgeRouter(compileOpts)
if err != nil {
return nil, err
}
2022-12-26 00:33:32 +00:00
2023-09-15 21:44:20 +00:00
graphInfo := d2layouts.NestedGraphInfo(g.Root)
err = d2layouts.LayoutNested(ctx, g, graphInfo, coreLayout, edgeRouter)
2023-09-22 00:58:55 +00:00
if err != nil {
return nil, err
}
}
2023-07-14 20:08:26 +00:00
d, err := d2exporter.Export(ctx, g, compileOpts.FontFamily)
if err != nil {
return nil, err
}
for _, l := range g.Layers {
2023-07-14 20:08:26 +00:00
ld, err := compile(ctx, l, compileOpts, renderOpts)
if err != nil {
return nil, err
}
d.Layers = append(d.Layers, ld)
}
for _, l := range g.Scenarios {
2023-07-14 20:08:26 +00:00
ld, err := compile(ctx, l, compileOpts, renderOpts)
if err != nil {
return nil, err
}
d.Scenarios = append(d.Scenarios, ld)
}
for _, l := range g.Steps {
2023-07-14 20:08:26 +00:00
ld, err := compile(ctx, l, compileOpts, renderOpts)
if err != nil {
return nil, err
}
d.Steps = append(d.Steps, ld)
}
return d, nil
}
2023-04-01 00:18:17 +00:00
func getLayout(opts *CompileOptions) (d2graph.LayoutGraph, error) {
if opts.Layout != nil {
2023-07-14 20:08:26 +00:00
return opts.LayoutResolver(*opts.Layout)
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
} else {
return nil, errors.New("no available layout")
}
}
2023-07-14 20:08:26 +00:00
func getEdgeRouter(opts *CompileOptions) (d2graph.RouteEdges, error) {
if opts.Layout != nil && opts.RouterResolver != nil {
router, err := opts.RouterResolver(*opts.Layout)
if err != nil {
return nil, err
}
if router != nil {
return router, nil
}
}
return d2layouts.DefaultRouter, nil
}
2023-07-14 20:08:26 +00:00
// applyConfigs applies the configs read from D2 and applies it to passed in opts
// It will only write to opt fields that are nil, as passed-in opts have precedence
func applyConfigs(config *d2target.Config, compileOpts *CompileOptions, renderOpts *d2svg.RenderOpts) {
if config == nil {
return
}
if compileOpts.Layout == nil {
compileOpts.Layout = config.LayoutEngine
}
if renderOpts.ThemeID == nil {
renderOpts.ThemeID = config.ThemeID
}
if renderOpts.DarkThemeID == nil {
renderOpts.DarkThemeID = config.DarkThemeID
}
if renderOpts.Sketch == nil {
renderOpts.Sketch = config.Sketch
}
if renderOpts.Pad == nil {
renderOpts.Pad = config.Pad
}
if renderOpts.Center == nil {
renderOpts.Center = config.Center
}
2023-12-13 20:17:22 +00:00
renderOpts.ThemeOverrides = config.ThemeOverrides
renderOpts.DarkThemeOverrides = config.DarkThemeOverrides
2023-07-14 20:08:26 +00:00
}
func applyDefaults(compileOpts *CompileOptions, renderOpts *d2svg.RenderOpts) {
if compileOpts.Layout == nil {
compileOpts.Layout = go2.Pointer("dagre")
}
if renderOpts.ThemeID == nil {
renderOpts.ThemeID = &d2themescatalog.NeutralDefault.ID
}
if renderOpts.Sketch == nil {
renderOpts.Sketch = go2.Pointer(false)
}
if *renderOpts.Sketch {
compileOpts.FontFamily = go2.Pointer(d2fonts.HandDrawn)
}
if renderOpts.Pad == nil {
renderOpts.Pad = go2.Pointer(int64(d2svg.DEFAULT_PADDING))
}
if renderOpts.Center == nil {
renderOpts.Center = go2.Pointer(false)
}
}