Releases: ts-graphviz/parser
v0.6.0 π
v0.5.0 π
π Features
- export convert method to allow downstream adopters to leverage it @ChristianMurphy (#26)
Changes
- docs: correct minor typo in readme @ChristianMurphy (#25)
- docs: add ChristianMurphy as a contributor for code, ideas, doc @allcontributors (#29)
- docs: add kamiazya as a contributor for code, doc, test @allcontributors (#28)
Special thanks for @ChristianMurphy.
v0.4.2 π
v0.4.1 π
v0.4.0 π
v0.3.0 π
v0.2.0 π
v0.1.0 π
Graphviz dot language parser for ts-graphviz.
Installation
The module can then be installed using npm:
# yarn
$ yarn add @ts-graphviz/parser
# or npm
$ npm install -S @ts-graphviz/parser
High level API
parse
function
Parse a string written in dot language and convert it to a model.
The return value is a Graph
or Digraph
that inherits from RootCluster
.
import { parse } from '@ts-graphviz/parser';
const G = parse(`
digraph hoge {
a -> b;
}`);
This is equivalent to the code below when using ts-graphviz.
import { digraph } from 'ts-graphviz';
const G = digraph('hoge', (g) => {
g.edge(['a', 'b']);
});
If the given string is invalid, a SyntaxError exception will be thrown.
import { parse, SyntaxError } from '@ts-graphviz/parser';
try {
parse(`invalid`);
} catch (e) {
if (e instanceof SyntaxError) {
console.log(e.message);
}
}
dot
tagged template
This is an experimental API.
Behavior may change in the future.
A tag template version of the parse function.
Returns a Graph or Digraph object based on the parsed result.
If the given string is invalid, a SyntaxError exception will be thrown.
import { dot } from '@ts-graphviz/parser';
const G = dot`
graph {
a -- b
}
`;
Low lebel API
AST
module
The AST
module provides the ability to handle the AST as a result of parsing the dot language
for lower level operations.
AST.parse
function
The basic usage is the same as the parse
function, except that it returns the dot language AST.
import { inspect } from 'util';
import { AST } from '@ts-graphviz/parser';
const ast = AST.parse(`
strict digraph example {
subgraph cluster_0 {
label="Subgraph A";
a -> b -> c -> d;
}
subgraph cluster_1 {
label="Subgraph B";
a -> f;
f -> c;
}
}
`);
console.log(inspect(ast, false, 6));
In the case of the above code, the structure of AST is as follows.
{
type: 'graph',
id: 'example',
directed: true,
strict: true,
children: [
{
type: 'subgraph',
id: 'cluster_0',
children: [
{ type: 'attribute', key: 'label', value: 'Subgraph A' },
{
type: 'edge',
targets: [ { id: 'a' }, { id: 'b' }, { id: 'c' }, { id: 'd' } ],
attributes: []
}
]
},
{
type: 'subgraph',
id: 'cluster_1',
children: [
{ type: 'attribute', key: 'label', value: 'Subgraph B' },
{
type: 'edge',
targets: [ { id: 'a' }, { id: 'f' } ],
attributes: []
},
{
type: 'edge',
targets: [ { id: 'f' }, { id: 'c' } ],
attributes: []
}
]
}
]
}