This commit is contained in:
Alexander Wang 2025-03-02 13:26:58 -07:00
parent 42e11d1e63
commit 16b0c897e0
No known key found for this signature in database
GPG key ID: BE3937D0D52D8927
4 changed files with 871 additions and 15 deletions

View file

@ -87,6 +87,7 @@ func (c *compiler) compileBoard(g *d2graph.Graph, ir *d2ir.Map) *d2graph.Graph {
ir = ir.Copy(nil).(*d2ir.Map)
// c.preprocessSeqDiagrams(ir)
c.compileMap(g.Root, ir)
c.setDefaultShapes(g)
if len(c.err.Errors) == 0 {
c.validateKeys(g.Root, ir)
}
@ -356,14 +357,7 @@ func (c *compiler) compileField(obj *d2graph.Object, f *d2ir.Field) {
parent := obj
obj = obj.EnsureChild(([]d2ast.String{f.Name}))
if f.Primary() != nil {
var s string
if obj.OuterSequenceDiagram() != nil {
s = obj.Attributes.Shape.Value
}
c.compileLabel(&obj.Attributes, f)
if s != "" {
obj.Attributes.Shape.Value = s
}
}
if f.Map() != nil {
c.compileMap(obj, f.Map())
@ -410,8 +404,6 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {
attrs.Language = fullTag
}
switch attrs.Language {
case "latex":
attrs.Shape.Value = d2target.ShapeText
case "markdown":
rendered, err := textmeasure.RenderMarkdown(scalar.ScalarString())
if err != nil {
@ -428,9 +420,6 @@ func (c *compiler) compileLabel(attrs *d2graph.Attributes, f d2ir.Node) {
c.errorf(f.LastPrimaryKey(), "malformed Markdown: %s", err.Error())
}
}
attrs.Shape.Value = d2target.ShapeText
default:
attrs.Shape.Value = d2target.ShapeCode
}
attrs.Label.Value = scalar.ScalarString()
default:
@ -1577,3 +1566,21 @@ FOR:
}
return nil, nil
}
func (c *compiler) setDefaultShapes(g *d2graph.Graph) {
for _, obj := range g.Objects {
if obj.Shape.Value == "" {
if obj.OuterSequenceDiagram() != nil {
obj.Shape.Value = d2target.ShapeRectangle
} else if obj.Language == "latex" {
obj.Shape.Value = d2target.ShapeText
} else if obj.Language == "markdown" {
obj.Shape.Value = d2target.ShapeText
} else if obj.Language != "" {
obj.Shape.Value = d2target.ShapeCode
} else {
obj.Shape.Value = d2target.ShapeRectangle
}
}
}
}

View file

@ -5605,6 +5605,34 @@ a -> c
tassert.Equal(t, (*d2graph.Scalar)(nil), g.Edges[2].Style.StrokeWidth)
},
},
{
name: "md-shape",
run: func(t *testing.T) {
g, _ := assertCompile(t, `
a.shape: circle
a: |md #hi |
b.shape: circle
b.label: |md #hi |
c: |md #hi |
c.shape: circle
d.label: |md #hi |
d.shape: circle
e: {
shape: circle
label: |md #hi |
}
`, ``)
tassert.Equal(t, 5, len(g.Objects))
for _, obj := range g.Objects {
tassert.Equal(t, "circle", obj.Shape.Value, "Object "+obj.ID+" should have circle shape")
tassert.Equal(t, "markdown", obj.Language, "Object "+obj.ID+" should have md language")
}
},
},
}
for _, tc := range tca {

View file

@ -711,9 +711,6 @@ func (obj *Object) newObject(ids d2ast.String) *Object {
Label: Scalar{
Value: idval,
},
Shape: Scalar{
Value: d2target.ShapeRectangle,
},
},
Graph: obj.Graph,

View file

@ -0,0 +1,824 @@
{
"graph": {
"name": "",
"isFolderOnly": false,
"ast": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,0:0:0-17:8:183",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,1:0:1-1:15:16",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,1:0:1-1:7:8",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,1:0:1-1:1:2",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,1:2:3-1:7:8",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,1:9:10-1:15:16",
"value": [
{
"string": "circle",
"raw_string": "circle"
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,2:0:17-2:12:29",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,2:0:17-2:1:18",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,2:0:17-2:1:18",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"primary": {},
"value": {
"block_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,2:3:20-2:12:29",
"quote": "",
"tag": "md",
"value": "#hi"
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,4:0:31-4:15:46",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,4:0:31-4:7:38",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,4:0:31-4:1:32",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,4:2:33-4:7:38",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,4:9:40-4:15:46",
"value": [
{
"string": "circle",
"raw_string": "circle"
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,5:0:47-5:18:65",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,5:0:47-5:7:54",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,5:0:47-5:1:48",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,5:2:49-5:7:54",
"value": [
{
"string": "label",
"raw_string": "label"
}
]
}
}
]
},
"primary": {},
"value": {
"block_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,5:9:56-5:18:65",
"quote": "",
"tag": "md",
"value": "#hi"
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,7:0:67-7:12:79",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,7:0:67-7:1:68",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,7:0:67-7:1:68",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
}
]
},
"primary": {},
"value": {
"block_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,7:3:70-7:12:79",
"quote": "",
"tag": "md",
"value": "#hi"
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,8:0:80-8:15:95",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,8:0:80-8:7:87",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,8:0:80-8:1:81",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,8:2:82-8:7:87",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,8:9:89-8:15:95",
"value": [
{
"string": "circle",
"raw_string": "circle"
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,10:0:97-10:18:115",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,10:0:97-10:7:104",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,10:0:97-10:1:98",
"value": [
{
"string": "d",
"raw_string": "d"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,10:2:99-10:7:104",
"value": [
{
"string": "label",
"raw_string": "label"
}
]
}
}
]
},
"primary": {},
"value": {
"block_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,10:9:106-10:18:115",
"quote": "",
"tag": "md",
"value": "#hi"
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,11:0:116-11:15:131",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,11:0:116-11:7:123",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,11:0:116-11:1:117",
"value": [
{
"string": "d",
"raw_string": "d"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,11:2:118-11:7:123",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,11:9:125-11:15:131",
"value": [
{
"string": "circle",
"raw_string": "circle"
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,13:0:133-16:1:174",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,13:0:133-13:1:134",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,13:0:133-13:1:134",
"value": [
{
"string": "e",
"raw_string": "e"
}
]
}
}
]
},
"primary": {},
"value": {
"map": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,13:3:136-16:1:174",
"nodes": [
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,14:2:140-14:15:153",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,14:2:140-14:7:145",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,14:2:140-14:7:145",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"primary": {},
"value": {
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,14:9:147-14:15:153",
"value": [
{
"string": "circle",
"raw_string": "circle"
}
]
}
}
}
},
{
"map_key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,15:2:156-15:18:172",
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,15:2:156-15:7:161",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,15:2:156-15:7:161",
"value": [
{
"string": "label",
"raw_string": "label"
}
]
}
}
]
},
"primary": {},
"value": {
"block_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,15:9:163-15:18:172",
"quote": "",
"tag": "md",
"value": "#hi"
}
}
}
}
]
}
}
}
}
]
},
"root": {
"id": "",
"id_val": "",
"attributes": {
"label": {
"value": ""
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"shape": {
"value": ""
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
"edges": null,
"objects": [
{
"id": "a",
"id_val": "a",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,1:0:1-1:7:8",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,1:0:1-1:1:2",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,1:2:3-1:7:8",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
},
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,2:0:17-2:1:18",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,2:0:17-2:1:18",
"value": [
{
"string": "a",
"raw_string": "a"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "#hi"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"language": "markdown",
"shape": {
"value": "circle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "b",
"id_val": "b",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,4:0:31-4:7:38",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,4:0:31-4:1:32",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,4:2:33-4:7:38",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
},
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,5:0:47-5:7:54",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,5:0:47-5:1:48",
"value": [
{
"string": "b",
"raw_string": "b"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,5:2:49-5:7:54",
"value": [
{
"string": "label",
"raw_string": "label"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "#hi"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"language": "markdown",
"shape": {
"value": "circle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "c",
"id_val": "c",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,7:0:67-7:1:68",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,7:0:67-7:1:68",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
},
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,8:0:80-8:7:87",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,8:0:80-8:1:81",
"value": [
{
"string": "c",
"raw_string": "c"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,8:2:82-8:7:87",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "#hi"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"language": "markdown",
"shape": {
"value": "circle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "d",
"id_val": "d",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,10:0:97-10:7:104",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,10:0:97-10:1:98",
"value": [
{
"string": "d",
"raw_string": "d"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,10:2:99-10:7:104",
"value": [
{
"string": "label",
"raw_string": "label"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
},
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,11:0:116-11:7:123",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,11:0:116-11:1:117",
"value": [
{
"string": "d",
"raw_string": "d"
}
]
}
},
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,11:2:118-11:7:123",
"value": [
{
"string": "shape",
"raw_string": "shape"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "#hi"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"language": "markdown",
"shape": {
"value": "circle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
},
{
"id": "e",
"id_val": "e",
"references": [
{
"key": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,13:0:133-13:1:134",
"path": [
{
"unquoted_string": {
"range": "d2/testdata/d2compiler/TestCompile2/globs/md-shape.d2,13:0:133-13:1:134",
"value": [
{
"string": "e",
"raw_string": "e"
}
]
}
}
]
},
"key_path_index": 0,
"map_key_edge_index": -1
}
],
"attributes": {
"label": {
"value": "#hi"
},
"labelDimensions": {
"width": 0,
"height": 0
},
"style": {},
"near_key": null,
"language": "markdown",
"shape": {
"value": "circle"
},
"direction": {
"value": ""
},
"constraint": null
},
"zIndex": 0
}
]
},
"err": null
}