Updated pie parsing with the new syntax + fix for flowDb

This commit is contained in:
Knut Sveidqvist 2022-04-30 15:13:28 +02:00
parent 8372d4952e
commit a401e44180
4 changed files with 51 additions and 45 deletions

View File

@ -4,7 +4,13 @@ import * as configApi from '../../config';
import common from '../common/common';
import mermaidAPI from '../../mermaidAPI';
import { log } from '../../logger';
import { setTitle, getTitle, getAccDescription, setAccDescription } from '../../commonDb';
import {
setTitle,
getTitle,
getAccDescription,
setAccDescription,
clear as commonClear,
} from '../../commonDb';
const MERMAID_DOM_ID_PREFIX = 'flowchart-';
let vertexCounter = 0;
@ -18,8 +24,6 @@ let tooltips = {};
let subCount = 0;
let firstGraphFlag = true;
let direction;
let title = 'Flow chart';
let description = '';
let version; // As in graph
@ -32,18 +36,6 @@ export const parseDirective = function (statement, context, type) {
mermaidAPI.parseDirective(this, statement, context, type);
};
// const getTitle = function () {
// return title;
// };
// const setAccDescription = function (txt) {
// description = sanitizeText(txt).replace(/\n\s+/g, '\n');
// };
// const getAccDescription = function () {
// return description;
// };
/**
* Function to lookup domId from id in the graph definition.
*
@ -444,8 +436,7 @@ export const clear = function (ver) {
tooltips = [];
firstGraphFlag = true;
version = ver || 'gen-1';
title = '';
description = '';
commonClear();
};
export const setGen = (ver) => {
version = ver || 'gen-1';

View File

@ -8,12 +8,13 @@
%x string
%x title
%x accDescription
%x open_directive
%x type_directive
%x arg_directive
%x close_directive
%x acc_title
%x acc_descr
%x acc_descr_multiline
%%
\%\%\{ { this.begin('open_directive'); return 'open_directive'; }
<open_directive>((?:(?!\}\%\%)[^:.])*) { this.begin('type_directive'); return 'type_directive'; }
@ -27,8 +28,14 @@
[\s]+ /* ignore */
title { this.begin("title");return 'title'; }
<title>(?!\n|;|#)*[^\n]* { this.popState(); return "title_value"; }
accDescription { this.begin("accDescription");return 'accDescription'; }
<accDescription>(?!\n|;|#)*[^\n]* { this.popState(); return "description_value"; }
accTitle\s*":"\s* { this.begin("acc_title");return 'acc_title'; }
<acc_title>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_title_value"; }
accDescr\s*":"\s* { this.begin("acc_descr");return 'acc_descr'; }
<acc_descr>(?!\n|;|#)*[^\n]* { this.popState(); return "acc_descr_value"; }
accDescr\s*"{"\s* { this.begin("acc_descr_multiline");}
<acc_descr_multiline>[\}] { this.popState(); }
<acc_descr_multiline>[^\}]* return "acc_descr_multiline_value";
["] { this.begin("string"); }
<string>["] { this.popState(); }
<string>[^"]* { return "txt"; }
@ -63,7 +70,9 @@ statement
:
| txt value { yy.addSection($1,yy.cleanupValue($2)); }
| title title_value { $$=$2.trim();yy.setTitle($$); }
| accDescription description_value { $$=$2.trim();yy.setAccDescription($$); }
| acc_title acc_title_value { $$=$2.trim();yy.setTitle($$); }
| acc_descr acc_descr_value { $$=$2.trim();yy.setAccDescription($$); }
| acc_descr_multiline_value { $$=$1.trim();yy.setAccDescription($$); } | section {yy.addSection($1.substr(8));$$=$1.substr(8);}
| directive
;

View File

@ -62,7 +62,7 @@ pie
expect(title).toBe('a 60/40 pie');
});
it('should handle simple pie without an acc description', function () {
it('should handle simple pie without an acc description (accDescr)', function () {
const res = pie.parser.parse(`pie title a neat chart
"ash" : 60
"bat" : 40
@ -77,9 +77,9 @@ pie
expect(description).toBe('');
});
it('should handle simple pie with an acc description', function () {
it('should handle simple pie with an acc description (accDescr)', function () {
const res = pie.parser.parse(`pie title a neat chart
accDescription a neat description
accDescr: a neat description
"ash" : 60
"bat" : 40
`);
@ -92,6 +92,24 @@ pie
expect(title).toBe('a neat chart');
expect(description).toBe('a neat description');
});
it('should handle simple pie with a multiline acc description (accDescr)', function () {
const res = pie.parser.parse(`pie title a neat chart
accDescr {
a neat description
on multiple lines
}
"ash" : 60
"bat" : 40
`);
const sections = pieDb.getSections();
const title = pieDb.getTitle();
const description = pieDb.getAccDescription();
const section1 = sections['ash'];
expect(section1).toBe(60);
expect(title).toBe('a neat chart');
expect(description).toBe('a neat description\non multiple lines');
});
it('should handle simple pie with positive decimal', function () {
const res = pie.parser.parse(`pie

View File

@ -2,6 +2,13 @@ import { log } from '../../logger';
import mermaidAPI from '../../mermaidAPI';
import * as configApi from '../../config';
import common from '../common/common';
import {
setTitle,
getTitle,
getAccDescription,
setAccDescription,
clear as commonClear,
} from '../../commonDb';
let sections = {};
let title = '';
@ -21,10 +28,6 @@ const addSection = function (id, value) {
};
const getSections = () => sections;
const setTitle = function (txt) {
title = common.sanitizeText(txt, configApi.getConfig());
};
const setShowData = function (toggle) {
showData = toggle;
};
@ -33,18 +36,6 @@ const getShowData = function () {
return showData;
};
const getTitle = function () {
return title;
};
const setAccDescription = function (txt) {
description = common.sanitizeText(txt, configApi.getConfig());
};
const getAccDescription = function () {
return description;
};
const cleanupValue = function (value) {
if (value.substring(0, 1) === ':') {
value = value.substring(1).trim();
@ -58,10 +49,8 @@ const clear = function () {
sections = {};
title = '';
showData = false;
commonClear();
};
// export const parseError = (err, hash) => {
// global.mermaidAPI.parseError(err, hash)
// }
export default {
parseDirective,
@ -76,5 +65,4 @@ export default {
getShowData,
getAccDescription,
setAccDescription,
// parseError
};