2017-04-02 00:54:46 +00:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
const util = require('util');
|
|
|
|
|
|
|
|
const GRAPH_NODE = ' "%s" [label="%s"]';
|
|
|
|
const GRAPH_NODE_URL = ' "%s" [label="%s", URL="%s", fontcolor=blue]';
|
|
|
|
const GRAPH_LINK = ' "%s" -> "%s"';
|
|
|
|
|
|
|
|
/**
|
2020-07-15 17:14:31 +00:00
|
|
|
* Convert a graph to the DOT format.
|
2017-04-02 00:54:46 +00:00
|
|
|
*
|
2020-07-15 17:14:31 +00:00
|
|
|
* @param graph Graph to convert.
|
|
|
|
* @param [title=identity] Function giving the name of each node.
|
|
|
|
* @param [url=none] Function given the URL of each node, or an empty string
|
|
|
|
* if a node has no URL.
|
|
|
|
* @return DOT representation of the graph.
|
2017-04-02 00:54:46 +00:00
|
|
|
*/
|
|
|
|
const graphToDOT = (graph, title = id => id, url = () => '') =>
|
|
|
|
{
|
|
|
|
const ser = graph.serialize();
|
|
|
|
|
2020-07-15 17:14:31 +00:00
|
|
|
// Convert nodes
|
2017-04-02 00:54:46 +00:00
|
|
|
const nodes = ser.nodes.map(({id}) =>
|
|
|
|
{
|
|
|
|
const nodeTitle = title(id);
|
|
|
|
const nodeUrl = url(id);
|
|
|
|
|
|
|
|
if (url === '')
|
|
|
|
{
|
|
|
|
return util.format(GRAPH_NODE, id, nodeTitle);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return util.format(GRAPH_NODE_URL, id, nodeTitle, nodeUrl);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
).join('\n');
|
|
|
|
|
2020-07-15 17:14:31 +00:00
|
|
|
// Convert edges
|
2017-04-02 00:54:46 +00:00
|
|
|
const links = ser.links.map(({source, target}) =>
|
|
|
|
util.format(GRAPH_LINK, source, target)
|
|
|
|
).join('\n');
|
|
|
|
|
|
|
|
return (
|
|
|
|
'digraph epenser {\n'
|
|
|
|
+ nodes
|
|
|
|
+ '\n'
|
|
|
|
+ links
|
|
|
|
+ '\n}'
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.graphToDOT = graphToDOT;
|