Generate visual representations of the networks underlying video-gamebooks on YouTube
https://youtube-maze.delab.re/
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
51 lines
1.2 KiB
'use strict';
|
|
|
|
import util from 'util';
|
|
|
|
const GRAPH_NODE = ' "%s" [label="%s"]';
|
|
const GRAPH_NODE_URL = ' "%s" [label="%s", URL="%s", fontcolor=blue]';
|
|
const GRAPH_LINK = ' "%s" -> "%s"';
|
|
|
|
/**
|
|
* Convert a graph to the DOT format.
|
|
*
|
|
* @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.
|
|
*/
|
|
export const graphToDOT = (graph, title = id => id, url = () => '') =>
|
|
{
|
|
const ser = graph.serialize();
|
|
|
|
// Convert nodes
|
|
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');
|
|
|
|
// Convert edges
|
|
const links = ser.links.map(({source, target}) =>
|
|
util.format(GRAPH_LINK, source, target)
|
|
).join('\n');
|
|
|
|
return (
|
|
'digraph epenser {\n'
|
|
+ nodes
|
|
+ '\n'
|
|
+ links
|
|
+ '\n}'
|
|
);
|
|
};
|
|
|