Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add parser

  • Loading branch information...
commit 3e277fb4888e4a1c88e31717bbef211b25939319 1 parent da56145
@glejeune authored
View
6 README.rdoc
@@ -16,6 +16,9 @@ Interface to the GraphViz graphing tool
=== 0.0.3 :
+* Add attributs to addNode and addEdge
+* Add parser (see tests/parse.js)
+
=== 0.0.2 :
* Added the ability to render in-memory (without writing to the filesystem) (By Mathieu Ravaux)
* Rewrite output method
@@ -34,8 +37,7 @@ A basic example
var g = graphviz.digraph("G");
// Add node (ID: Hello)
- var n1 = g.addNode( "Hello" );
- n1.set( "color", "blue" );
+ var n1 = g.addNode( "Hello", {"color" : "blue"} );
n1.set( "style", "filled" );
// Add node (ID: World)
View
20 lib/deps/graph.js
@@ -6,8 +6,7 @@ var Hash = require( './core_ext/hash' ).Hash,
Edge = require( './edge' ).Edge,
sys = require('sys'),
path = require('path'),
- spawn = require('child_process').spawn,
- child;
+ spawn = require('child_process').spawn;
/**
* Create a new graph
@@ -35,11 +34,18 @@ var Graph = exports.Graph = function(graph, id) {
* Create a new node
*
* @param {String} id The node ID
+ * @param {Object} attrs Node attributs
* @return {Node}
* @api public
*/
-Graph.prototype.addNode = function(id) {
+Graph.prototype.addNode = function(id, attrs) {
this.nodes.setItem(id, new Node(this, id));
+ if( attrs ) {
+ for( k in attrs ) {
+ this.nodes.items[id].set( k, attrs[k] );
+ }
+ }
+
return this.nodes.items[id];
}
@@ -69,10 +75,11 @@ Graph.prototype.nodeCount = function() {
*
* @param {String|Node} nodeOne
* @param {String|Node} nodeTwo
+ * @param {Object} attrs Node attributs
* @return {Edge}
* @api public
*/
-Graph.prototype.addEdge = function(nodeOne, nodeTwo) {
+Graph.prototype.addEdge = function(nodeOne, nodeTwo, attrs) {
var _nodeOne = nodeOne;
var _nodeTwo = nodeTwo;
if( typeof(nodeOne) == 'string' ) {
@@ -89,6 +96,11 @@ Graph.prototype.addEdge = function(nodeOne, nodeTwo) {
}
var edge = new Edge(this, _nodeOne, _nodeTwo);
+ if( attrs ) {
+ for( k in attrs ) {
+ edge.set( k, attrs[k] );
+ }
+ }
this.edges.push( edge );
return edge;
View
157 lib/ext/gvpr/dot2js.g
@@ -0,0 +1,157 @@
+// Copyright (c) 2010 Gregoire Lejeune <gregoire.lejeune@free.fr>
+//
+// This program is free software; you can redistribute it and/or modify
+// it under the terms of the GNU General Public License as published by
+// the Free Software Foundation; either version 2 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this program; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+//
+// Usage :
+// gvpr -f dot2js.g [-a <output type>] <dot script>
+
+BEGIN {
+ int g_strict; int g_direct;
+ graph_t cluster;
+ node_t cnode;
+ edge_t cedge;
+ string attr; string attrv;
+ graph_t subgraph; graph_t pgraph;
+ graph_t ofgraph;
+
+ string xOut;
+ if( ARGC == 0 ) {
+ xOut = "_";
+ } else {
+ xOut = tolower(ARGV[0]);
+ }
+
+ printf( "// This code was generated by dot2js.g\n\n" );
+
+ string rubyfy( string s ) {
+ string out;
+ out = tolower( s );
+ out = gsub( out, " ", "__" );
+ out = gsub( out, "'", "" );
+ out = gsub( out, "-", "_" );
+ out = gsub( out, ".", "" );
+ out = gsub( out, "%", "u" );
+ out = gsub( out, "+", "" );
+ out = gsub( out, "/", "_" );
+ return( out );
+ }
+}
+
+BEG_G {
+ printf( "var sys = require('sys'),\n graphviz = require('graphviz');\n\n");
+ // Directed
+ g_direct = isDirect($);
+ if( g_direct == 0 ) {
+ printf( "var graph_%s = graphviz.graph( \"%s\" );\n", rubyfy($.name), rubyfy($.name) );
+ } else {
+ printf( "var graph_%s = graphviz.digraph( \"%s\" );\n", rubyfy($.name), rubyfy($.name) );
+ }
+ // Strict
+ g_strict = isStrict($);
+ if( g_strict != 0 ) {
+ // printf( ", :strict => true" ); ///////////////////////// TODO
+ }
+
+ // Attributs of G
+ attr = fstAttr($, "G");
+ while( attr != "" ) {
+ attrv = aget( $, attr );
+ if( attrv != "" ) {
+ printf( "graph_%s.set( \"%s\", \"%s\" );\n", rubyfy($.name), attr, attrv );
+ }
+ attr = nxtAttr( $, "G", attr );
+ }
+
+ // Subgraph
+ subgraph = fstsubg( $ );
+ while( subgraph != NULL ) {
+ pgraph = subgraph.parent;
+ printf ( "var graph_%s = graph_%s.addCluster( \"%s\" )\n", rubyfy(subgraph.name), rubyfy(pgraph.name), rubyfy(subgraph.name) );
+
+ // ATTRS
+ attr = fstAttr(subgraph, "G");
+ while( attr != "" ) {
+ attrv = aget( subgraph, attr );
+ if( attrv != "" ) {
+ printf( "graph_%s.set( \"%s\", \"%s\" );\n", rubyfy(subgraph.name), attr, attrv );
+ }
+ attr = nxtAttr( subgraph, "G", attr );
+ }
+
+ subgraph = nxtsubg( subgraph );
+ }
+}
+
+N {
+ pgraph = $.root;
+ ofgraph = pgraph;
+
+ subgraph = fstsubg( pgraph );
+ while( subgraph != NULL ) {
+ if( isSubnode( subgraph, $ ) != 0 ) {
+ ofgraph = subgraph;
+ }
+ subgraph = nxtsubg( subgraph );
+ }
+
+ printf( "var node_%s = graph_%s.addNode( \"%s\", {", rubyfy($.name), rubyfy(ofgraph.name), $.name );
+
+ // Attributs of N
+ attr = fstAttr($G, "N");
+ while( attr != "" ) {
+ attrv = aget( $, attr );
+ if( attrv != "" ) {
+ printf( "\"%s\" : \"%s\", ", attr, gsub( attrv, "'", "\\'" ) );
+// } else {
+// printf( "\"%s\" : \"\", ", attr );
+ }
+ attr = nxtAttr( $G, "N", attr );
+ }
+
+ printf( "} );\n" );
+}
+
+E {
+ pgraph = $.root;
+ ofgraph = pgraph;
+
+ subgraph = fstsubg( pgraph );
+ while( subgraph != NULL ) {
+ if( isSubedge( subgraph, $ ) != 0 ) {
+ ofgraph = subgraph;
+ }
+ subgraph = nxtsubg( subgraph );
+ }
+
+ printf( "graph_%s.addEdge( \"%s\", \"%s\", {", rubyfy(ofgraph.name), $.tail.name, $.head.name );
+
+ // Attributs of E
+ attr = fstAttr($G, "E");
+ while( attr != "" ) {
+ attrv = aget( $, attr );
+ if( attrv != "" ) {
+ printf( "\"%s\" : \"%s\", ", attr, gsub( attrv, "'", "\\'" ) );
+// } else {
+// printf( "\"%s\" : \"\", ", attr );
+ }
+ attr = nxtAttr( $G, "E", attr );
+ }
+
+ printf( "} );\n" );
+}
+
+END_G {
+ printf( "__graph_eval = graph_%s;\n", rubyfy($.name) );
+}
View
27 lib/graphviz.js
@@ -2,6 +2,8 @@
* Module dependencies.
*/
var sys = require('sys'),
+ path = require('path'),
+ spawn = require('child_process').spawn,
Graph = require('./deps/graph').Graph;
/**
@@ -22,10 +24,33 @@ exports.graph = function(id) {
* @constructor
* @param {String} id The graphID
* @return {Graph}
- * @api public
+ * @api public
*/
exports.digraph = function(id) {
var graph = new Graph(null, id);
graph.type = 'digraph';
return graph;
}
+
+exports.parse = function(file, callback, errback) {
+ var gvprScript = path.join( __dirname, "ext", "gvpr", "dot2js.g" )
+ var parameters = ["-f"+gvprScript, file]
+ var cmd = "gvpr"
+ var __graph_eval;
+
+ graphviz = spawn(cmd, parameters);
+ graphviz.stdout.on('data', function(data) {
+ eval(data.toString());
+ });
+ graphviz.stderr.on('data', function(data) {
+ if(data) console.log( "STDERR: "+data);
+ });
+ graphviz.stdin.end();
+ graphviz.on('exit', function(code) {
+ if(code !== 0) {
+ if(errback) { errback(code) }
+ } else {
+ callback(__graph_eval)
+ }
+ });
+}
View
30 tests/cluster.dot
@@ -0,0 +1,30 @@
+digraph G {
+ subgraph cluster_0 {
+ style=filled;
+ color=lightgrey;
+ node [style=filled,color=white];
+ a0 -> a1;
+ a1 -> a2;
+ a2 -> a3;
+ label = "process #1";
+ }
+
+ subgraph cluster_1 {
+ node [style=filled];
+ b0 -> b1;
+ b1 -> b2;
+ b2 -> b3;
+ label = "process #2";
+ color=blue;
+ }
+ start -> a0;
+ start -> b0;
+ a1 -> b3;
+ b2 -> a3;
+ a3 -> a0;
+ a3 -> end;
+ b3 -> end;
+
+ start [shape=Mdiamond];
+ end [shape=Msquare];
+}
View
4 tests/hello_world.js
@@ -5,8 +5,8 @@ var sys = require('sys'),
var g = graphviz.digraph("G");
// Add node (ID: Hello)
-var n1 = g.addNode( "Hello" );
-n1.set( "color", "blue" );
+var n1 = g.addNode( "Hello", {"color" : "blue", "shape" : "Mdiamond"} );
+//n1.set( "color", "blue" );
n1.set( "style", "filled" );
// Add node (ID: World)
View
6 tests/parse.js
@@ -0,0 +1,6 @@
+var sys = require('sys'),
+ graphviz = require('../lib/graphviz');
+
+graphviz.parse( "cluster.dot", function(graph) {
+ graph.render( "png", "cluster.png" );
+})
Please sign in to comment.
Something went wrong with that request. Please try again.