d2/d2plugin/plugin_dagre.go

78 lines
2 KiB
Go
Raw Normal View History

2022-12-03 18:54:54 +00:00
//go:build !nodagre
package d2plugin
import (
"context"
2022-12-30 08:09:28 +00:00
"encoding/json"
"oss.terrastruct.com/d2/d2graph"
"oss.terrastruct.com/d2/d2layouts/d2dagrelayout"
2022-12-30 05:09:53 +00:00
"oss.terrastruct.com/util-go/xmain"
)
var DagrePlugin = dagrePlugin{}
func init() {
2022-12-30 05:09:53 +00:00
plugins = append(plugins, &DagrePlugin)
}
2022-12-30 05:09:53 +00:00
type dagrePlugin struct {
2022-12-30 20:40:52 +00:00
opts *d2dagrelayout.ConfigurableOpts
2022-12-30 05:09:53 +00:00
}
2022-12-30 19:33:32 +00:00
func (p dagrePlugin) Flags(context.Context) ([]PluginSpecificFlag, error) {
2022-12-30 08:09:28 +00:00
return []PluginSpecificFlag{
{
Name: "dagre-nodesep",
Type: "int64",
Default: int64(d2dagrelayout.DefaultOpts.NodeSep),
Usage: "number of pixels that separate nodes horizontally.",
Tag: "nodesep",
},
{
Name: "dagre-edgesep",
Type: "int64",
Default: int64(d2dagrelayout.DefaultOpts.EdgeSep),
Usage: "number of pixels that separate edges horizontally.",
Tag: "edgesep",
},
2022-12-30 19:33:32 +00:00
}, nil
2022-12-30 08:09:28 +00:00
}
func (p *dagrePlugin) HydrateOpts(opts []byte) error {
2022-12-30 05:09:53 +00:00
if opts != nil {
2022-12-30 20:40:52 +00:00
var dagreOpts d2dagrelayout.ConfigurableOpts
2022-12-30 08:09:28 +00:00
err := json.Unmarshal(opts, &dagreOpts)
if err != nil {
2022-12-30 05:09:53 +00:00
return xmain.UsageErrorf("non-dagre layout options given for dagre")
}
p.opts = &dagreOpts
}
return nil
}
func (p dagrePlugin) Info(context.Context) (*PluginInfo, error) {
return &PluginInfo{
Name: "dagre",
ShortHelp: "The directed graph layout library Dagre",
LongHelp: `dagre is a directed graph layout library for JavaScript.
See https://github.com/dagrejs/dagre
The implementation of this plugin is at: https://github.com/terrastruct/d2/tree/master/d2plugin/d2dagrelayout
note: dagre is the primary layout algorithm for text to diagram generator Mermaid.js.
See https://github.com/mermaid-js/mermaid
We have a useful comparison at https://text-to-diagram.com/?example=basic&a=d2&b=mermaid
`,
}, nil
}
func (p dagrePlugin) Layout(ctx context.Context, g *d2graph.Graph) error {
2022-12-30 05:09:53 +00:00
return d2dagrelayout.Layout(ctx, g, p.opts)
}
func (p dagrePlugin) PostProcess(ctx context.Context, in []byte) ([]byte, error) {
return in, nil
}