This commit is contained in:
Knut Sveidqvist
2024-09-12 12:10:41 +02:00
parent 8bb70ea493
commit 8b6fb94835
4 changed files with 99 additions and 23 deletions

View File

@@ -421,6 +421,10 @@ flowchart LR
</pre> </pre>
<pre id="diagram4" class="mermaid">
flowchart
D@{ shape: circle, label: "circle" } & E
</pre>
<script type="module"> <script type="module">
import mermaid from './mermaid.esm.mjs'; import mermaid from './mermaid.esm.mjs';
import layouts from './mermaid-layout-elk.esm.mjs'; import layouts from './mermaid-layout-elk.esm.mjs';

View File

@@ -123,10 +123,10 @@ export const addVertex = function (
if (shapeData !== undefined) { if (shapeData !== undefined) {
let yamlData; let yamlData;
// detect if shapeData contains a newline character // detect if shapeData contains a newline character
console.log('shapeData', shapeData);
if (!shapeData.includes('\n')) { if (!shapeData.includes('\n')) {
// console.log('yamlData shapeData has no new lines', shapeData); // console.log('yamlData shapeData has no new lines', shapeData);
yamlData = '{\n' + shapeData + '\n'; yamlData = '{\n' + shapeData + '\n}';
} else { } else {
// console.log('yamlData shapeData has new lines', shapeData); // console.log('yamlData shapeData has new lines', shapeData);
yamlData = shapeData + '\n'; yamlData = shapeData + '\n';
@@ -136,6 +136,7 @@ export const addVertex = function (
yamlData = yamlData.substring(0, lastPos) + '\n'; yamlData = yamlData.substring(0, lastPos) + '\n';
} }
} }
console.log('yamlData', yamlData);
const doc = yaml.load(yamlData, { schema: yaml.JSON_SCHEMA }) as NodeMetaData; const doc = yaml.load(yamlData, { schema: yaml.JSON_SCHEMA }) as NodeMetaData;
// console.log('yamlData doc', doc); // console.log('yamlData doc', doc);
if (doc?.shape) { if (doc?.shape) {

View File

@@ -15,7 +15,16 @@ describe('when parsing directions', function () {
it('should handle basic shape data statements', function () { it('should handle basic shape data statements', function () {
const res = flow.parser.parse(`flowchart TB const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded }@`); D@{ shape: rounded}`);
const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(1);
expect(data4Layout.nodes[0].shape).toEqual('rounded');
expect(data4Layout.nodes[0].label).toEqual('D');
});
it('should handle basic shape data statements', function () {
const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded }`);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(1); expect(data4Layout.nodes.length).toBe(1);
@@ -23,9 +32,69 @@ describe('when parsing directions', function () {
expect(data4Layout.nodes[0].label).toEqual('D'); expect(data4Layout.nodes[0].label).toEqual('D');
}); });
it('should handle basic shape data statements with &', function () {
const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded } & E`);
const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(2);
expect(data4Layout.nodes[0].shape).toEqual('rounded');
expect(data4Layout.nodes[0].label).toEqual('D');
expect(data4Layout.nodes[1].label).toEqual('E');
});
it('should handle shape data statements with edges', function () {
const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded } --> E`);
const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(2);
expect(data4Layout.nodes[0].shape).toEqual('rounded');
expect(data4Layout.nodes[0].label).toEqual('D');
expect(data4Layout.nodes[1].label).toEqual('E');
});
it('should handle basic shape data statements with amp and edges 1', function () {
const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded } & E --> F`);
const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(3);
expect(data4Layout.nodes[0].shape).toEqual('rounded');
expect(data4Layout.nodes[0].label).toEqual('D');
expect(data4Layout.nodes[1].label).toEqual('E');
});
it('should handle basic shape data statements with amp and edges 2', function () {
const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded } & E@{ shape: rounded } --> F`);
const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(3);
expect(data4Layout.nodes[0].shape).toEqual('rounded');
expect(data4Layout.nodes[0].label).toEqual('D');
expect(data4Layout.nodes[1].label).toEqual('E');
});
it('should handle basic shape data statements with amp and edges 3', function () {
const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded } & E@{ shape: rounded } --> F & G@{ shape: rounded }`);
const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(4);
expect(data4Layout.nodes[0].shape).toEqual('rounded');
expect(data4Layout.nodes[0].label).toEqual('D');
expect(data4Layout.nodes[1].label).toEqual('E');
});
it('should handle basic shape data statements with amp and edges 4', function () {
const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded } & E@{ shape: rounded } --> F@{ shape: rounded } & G@{ shape: rounded }`);
const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(4);
expect(data4Layout.nodes[0].shape).toEqual('rounded');
expect(data4Layout.nodes[0].label).toEqual('D');
expect(data4Layout.nodes[1].label).toEqual('E');
});
it('should no matter of there are no leading spaces', function () { it('should no matter of there are no leading spaces', function () {
const res = flow.parser.parse(`flowchart TB const res = flow.parser.parse(`flowchart TB
D@{shape: rounded }@`); D@{shape: rounded}`);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -36,7 +105,7 @@ describe('when parsing directions', function () {
it('should no matter of there are many leading spaces', function () { it('should no matter of there are many leading spaces', function () {
const res = flow.parser.parse(`flowchart TB const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded }@`); D@{ shape: rounded}`);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -47,7 +116,7 @@ describe('when parsing directions', function () {
it('should be forgiving with many spaces before teh end', function () { it('should be forgiving with many spaces before teh end', function () {
const res = flow.parser.parse(`flowchart TB const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded }@`); D@{ shape: rounded }`);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -57,7 +126,7 @@ describe('when parsing directions', function () {
}); });
it('should be possible to add multiple properties on the same line', function () { it('should be possible to add multiple properties on the same line', function () {
const res = flow.parser.parse(`flowchart TB const res = flow.parser.parse(`flowchart TB
D@{ shape: rounded , label: "DD" }@`); D@{ shape: rounded , label: "DD"}`);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -70,7 +139,7 @@ describe('when parsing directions', function () {
A --> D@{ A --> D@{
shape: circle shape: circle
icon: "clock" icon: "clock"
}@ }
`); `);
@@ -89,11 +158,11 @@ describe('when parsing directions', function () {
B@{ B@{
shape: circle shape: circle
icon: "clock" icon: "clock"
}@ }
C[Hello]@{ C[Hello]@{
shape: circle shape: circle
icon: "clock" icon: "clock"
}@ }
`); `);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -105,12 +174,12 @@ describe('when parsing directions', function () {
expect(data4Layout.nodes[2].shape).toEqual('circle'); expect(data4Layout.nodes[2].shape).toEqual('circle');
expect(data4Layout.nodes[2].label).toEqual('Hello'); expect(data4Layout.nodes[2].label).toEqual('Hello');
}); });
it('should use handle bracket end (}) character inside the shape data', function () { it.only('should use handle bracket end (}) character inside the shape data', function () {
const res = flow.parser.parse(`flowchart TB const res = flow.parser.parse(`flowchart TB
A@{ A@{
label: "This is }" label: "This is }"
icon: "clock" icon: "clock"
}@ }
`); `);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -135,7 +204,7 @@ describe('when parsing directions', function () {
This is a This is a
multiline string multiline string
icon: "clock" icon: "clock"
}@ }
`); `);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -149,7 +218,7 @@ describe('when parsing directions', function () {
label: "This is a label: "This is a
multiline string" multiline string"
icon: "clock" icon: "clock"
}@ }
`); `);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -162,7 +231,7 @@ describe('when parsing directions', function () {
A@{ A@{
label: "This is a string with }" label: "This is a string with }"
icon: "clock" icon: "clock"
}@ }
`); `);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -175,7 +244,7 @@ describe('when parsing directions', function () {
A@{ A@{
label: "This is a string with @" label: "This is a string with @"
icon: "clock" icon: "clock"
}@ }
`); `);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
@@ -186,14 +255,14 @@ describe('when parsing directions', function () {
it(' should be possible to use @ in strings', function () { it(' should be possible to use @ in strings', function () {
const res = flow.parser.parse(`flowchart TB const res = flow.parser.parse(`flowchart TB
A@{ A@{
label: "This is a string with }@" label: "This is a string with}"
icon: "clock" icon: "clock"
}@ }
`); `);
const data4Layout = flow.parser.yy.getData(); const data4Layout = flow.parser.yy.getData();
expect(data4Layout.nodes.length).toBe(1); expect(data4Layout.nodes.length).toBe(1);
expect(data4Layout.nodes[0].shape).toEqual('squareRect'); expect(data4Layout.nodes[0].shape).toEqual('squareRect');
expect(data4Layout.nodes[0].label).toEqual('This is a string with }@'); expect(data4Layout.nodes[0].label).toEqual('This is a string with}');
}); });
}); });

View File

@@ -39,12 +39,12 @@ accDescr\s*"{"\s* { this.begin("acc_descr_multilin
\@\{ { this.pushState("shapeData"); yytext=""; return 'SHAPE_DATA' } \@\{ { this.pushState("shapeData"); yytext=""; return 'SHAPE_DATA' }
<shapeData>["] { <shapeData>["] {
this.pushState("shapeDataStr"); this.pushState("shapeDataStr");
return 'SHAPE_DATA'; return 'SHAPE_DATA';
} }
<shapeDataStr>["] { this.popState(); return 'SHAPE_DATA'} <shapeDataStr>["] { this.popState(); return 'SHAPE_DATA'}
<shapeDataStr>[^\"]+ { <shapeDataStr>[^\"]+ {
const re = /\n\s*/g; const re = /\n\s*/g;
yytext = yytext.replace(re,"<br/>"); yytext = yytext.replace(re,"<br/>");
return 'SHAPE_DATA'} return 'SHAPE_DATA'}
@@ -404,8 +404,10 @@ vertexStatement: vertexStatement link node shapeData
node: styledVertex node: styledVertex
{ /*console.warn('nod', $styledVertex);*/ $$ = [$styledVertex];} { /*console.warn('nod', $styledVertex);*/ $$ = [$styledVertex];}
| node shapeData spaceList AMP spaceList styledVertex
{ yy.addVertex($node[0],undefined,undefined,undefined, undefined,undefined, undefined,$shapeData); $$ = $node.concat($styledVertex); console.warn('pip2', $node[0], $styledVertex, $$); }
| node spaceList AMP spaceList styledVertex | node spaceList AMP spaceList styledVertex
{ $$ = $node.concat($styledVertex); /* console.warn('pip', $node[0], $styledVertex, $$); */ } { $$ = $node.concat($styledVertex); console.warn('pip', $node[0], $styledVertex, $$); }
; ;
styledVertex: vertex styledVertex: vertex