mirror of
https://github.com/mermaid-js/mermaid.git
synced 2025-09-09 18:39:41 +02:00
Sankey syntax has beed reduced
This commit is contained in:
@@ -5,6 +5,7 @@ describe('Sankey Diagram', () => {
|
||||
imgSnapshotTest(
|
||||
`
|
||||
sankey
|
||||
|
||||
a,b,10
|
||||
`,
|
||||
{}
|
||||
|
@@ -1,5 +1,7 @@
|
||||
Agricultural 'waste',Bio-conversion,124.729
|
||||
%% there are leading and trailing spaces, do not remove
|
||||
Agricultural 'waste',Bio-conversion,124.729
|
||||
Bio-conversion,Liquid,0.597
|
||||
%% line with comment
|
||||
Bio-conversion,Losses,26.862
|
||||
Bio-conversion,Solid,280.322
|
||||
Bio-conversion,Gas,81.144
|
||||
@@ -50,6 +52,9 @@ Oil reserves,Oil,107.703
|
||||
Oil,Liquid,611.99
|
||||
Other waste,Solid,56.587
|
||||
Other waste,Bio-conversion,77.81
|
||||
%% blank lines in the middle
|
||||
|
||||
|
||||
Pumped heat,Heating and cooling - homes,193.026
|
||||
Pumped heat,Heating and cooling - commercial,70.672
|
||||
Solar PV,Electricity grid,59.901
|
||||
@@ -66,3 +71,6 @@ Tidal,Electricity grid,9.452
|
||||
UK land based bioenergy,Bio-conversion,182.01
|
||||
Wave,Electricity grid,19.013
|
||||
Wind,Electricity grid,289.366
|
||||
|
||||
%% lines at the end, do not remove
|
||||
|
||||
|
|
@@ -23,10 +23,10 @@ TEXTDATA [\u0020-\u0021\u0023-\u002B\u002D-\u007E]
|
||||
|
||||
%%
|
||||
|
||||
<<EOF>> { return 'EOF' }
|
||||
<<EOF>> { return 'EOF' } // match end of file
|
||||
|
||||
"sankey" { return 'SANKEY' }
|
||||
({CRLF}|{LF})+ { return 'NEWLINE' } // let newline to be multiple lines
|
||||
({CRLF}|{LF}) { return 'NEWLINE' }
|
||||
{COMMA} { return 'COMMA' }
|
||||
{DQUOTE} { return 'DQUOTE' }
|
||||
{TEXTDATA}* { return 'NON_ESCAPED_TEXT' }
|
||||
@@ -38,28 +38,11 @@ TEXTDATA [\u0020-\u0021\u0023-\u002B\u002D-\u007E]
|
||||
|
||||
%% // language grammar
|
||||
|
||||
start
|
||||
: SANKEY csv opt_eof
|
||||
;
|
||||
start: SANKEY NEWLINE csv opt_eof;
|
||||
|
||||
csv
|
||||
: record csv_tail
|
||||
;
|
||||
|
||||
csv_tail
|
||||
: NEWLINE csv
|
||||
| // empty
|
||||
;
|
||||
|
||||
opt_newline
|
||||
: NEWLINE
|
||||
| // empty
|
||||
;
|
||||
|
||||
opt_eof
|
||||
: EOF
|
||||
| // empty
|
||||
;
|
||||
csv: record csv_tail;
|
||||
csv_tail: NEWLINE csv | ;
|
||||
opt_eof: EOF | ;
|
||||
|
||||
record
|
||||
: field\[source] COMMA field\[target] COMMA field\[value] {
|
||||
@@ -68,7 +51,6 @@ record
|
||||
const value = parseFloat($value.trim());
|
||||
$$ = yy.addLink(source,target,value);
|
||||
} // parse only 3 fields, this is not part of CSV standard
|
||||
| {} // allow empty record to handle empty lines, this is not part of CSV standard either
|
||||
;
|
||||
|
||||
field
|
||||
|
@@ -3,7 +3,8 @@ import diagram from './sankey.jison';
|
||||
// @ts-ignore: jison doesn't export types
|
||||
import { parser } from './sankey.jison';
|
||||
import db from '../sankeyDB.js';
|
||||
// import { fail } from 'assert';
|
||||
import { cleanupComments } from '../../../diagram-api/comments.js';
|
||||
import { prepareTextForParsing } from '../sankeyDiagram.js';
|
||||
|
||||
describe('Sankey diagram', function () {
|
||||
// TODO - these examples should be put into ./parser/stateDiagram.spec.js
|
||||
@@ -18,15 +19,16 @@ describe('Sankey diagram', function () {
|
||||
const fs = await import('fs');
|
||||
const path = await import('path');
|
||||
const csv = path.resolve(__dirname, './energy.csv');
|
||||
fs.readFile(csv, 'utf8', (err: NodeJS.ErrnoException | null, data: string) => {
|
||||
if (err) {
|
||||
throw err;
|
||||
}
|
||||
const data = fs.readFileSync(csv, 'utf8');
|
||||
|
||||
const str = `sankey\\n${data}`;
|
||||
// Add \n\n + space to emulate possible possible imperfections
|
||||
const graphDefinition = prepareTextForParsing(cleanupComments('sankey\n\n ' + data));
|
||||
// const textToParse = graphDefinition
|
||||
// .replaceAll(/^[^\S\r\n]+|[^\S\r\n]+$/g, '')
|
||||
// .replaceAll(/([\n\r])+/g, "\n")
|
||||
// .trim();
|
||||
|
||||
parser.parse(str);
|
||||
});
|
||||
parser.parse(graphDefinition);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
@@ -5,6 +5,18 @@ import db from './sankeyDB.js';
|
||||
import styles from './styles.js';
|
||||
import renderer from './sankeyRenderer.js';
|
||||
|
||||
export const prepareTextForParsing = (text: string): string => {
|
||||
const textToParse = text
|
||||
.replaceAll(/^[^\S\r\n]+|[^\S\r\n]+$/g, '') // remove all trailing spaces for each row
|
||||
.replaceAll(/([\n\r])+/g, '\n') // remove empty lines duplicated
|
||||
.trim();
|
||||
|
||||
return textToParse;
|
||||
};
|
||||
|
||||
const originalParse = parser.parse.bind(parser);
|
||||
parser.parse = (text: string) => originalParse(prepareTextForParsing(text));
|
||||
|
||||
export const diagram: DiagramDefinition = {
|
||||
parser,
|
||||
db,
|
||||
|
Reference in New Issue
Block a user