Permalink
Browse files

parse now accept a file or a script

  • Loading branch information...
1 parent f754948 commit 6a6aa04e9d81c367538c6e778bb713ed0c7ee394 @glejeune committed Oct 8, 2010
Showing with 128 additions and 64 deletions.
  1. +1 −0 README.rdoc
  2. +11 −0 lib/deps/core_ext/fs-ext.js
  3. +24 −1 lib/graphviz.js
  4. +49 −51 tests/server/app.js
  5. +35 −0 tests/server/static/dotgraph.js
  6. +7 −11 tests/server/views/index.ejs
  7. +1 −1 tests/server/views/layout.ejs
View
@@ -19,6 +19,7 @@ Interface to the GraphViz graphing tool
* Major correction in parse : error callback now take three arguments : code, stdout, stderr
* Major bugs corrections
* Update Attributs class
+* parse now accept a file or a script
=== 0.0.3 :
@@ -0,0 +1,11 @@
+var fs = require('fs');
+
+exports.exist = function(path) {
+ try {
+ fs.statSync( path );
+ }
+ catch(e) {
+ return( false );
+ }
+ return( true );
+}
View
@@ -4,6 +4,9 @@
var sys = require('sys'),
path = require('path'),
spawn = require('child_process').spawn,
+ temp = require('temp'),
+ fs = require('fs'),
+ fsExt = require('./deps/core_ext/fs-ext'),
Graph = require('./deps/graph').Graph;
/**
@@ -32,7 +35,7 @@ exports.digraph = function(id) {
return graph;
}
-exports.parse = function(file, callback, errback) {
+function _parse(file, callback, errback) {
var gvprScript = path.join( __dirname, "ext", "gvpr", "dot2js.g" );
var parameters = ["-f"+gvprScript, file];
var cmd = "gvpr";
@@ -57,3 +60,23 @@ exports.parse = function(file, callback, errback) {
}
});
}
+/**
+ * Create a new graph from a dot script
+ * @constructor
+ * @param {String} file_or_script The DOT script or file
+ * @param {Function} callback
+ * @param {Function} errback
+ * @api public
+ */
+exports.parse = function(file_or_script, callback, errback) {
+ if( fsExt.exist(file_or_script) ) {
+ _parse(file_or_script, callback, errback)
+ } else {
+ temp.open('node-graphviz', function(err, info) {
+ fs.write(info.fd, file_or_script);
+ fs.close(info.fd, function(err) {
+ _parse(info.path, callback, errback)
+ });
+ });
+ }
+}
View
@@ -1,48 +1,66 @@
-var express = require('express'),
- app = express.createServer(),
- temp = require('temp'),
- fs = require('fs');
+var fs = require('fs');
+var sys = require('sys');
+var http = require('http');
+var url = require('url');
-var sys = require('sys'),
- graphviz = require('../../lib/graphviz');
+// npm install express
+var express = require('express'),
+ app = express.createServer();
+// nom install temp
+var temp = require('temp');
+// npm install graphviz
+var graphviz = require('../../lib/graphviz');
+// npm install jsmin
+var jsmin = require('jsmin').jsmin;
-var http = require('http'),
- url = require('url');
+// Configuration --------------------------------------------------------------
app.set('views', __dirname + '/views');
app.configure(function(){
app.use(express.bodyDecoder());
+ app.use(express.staticProvider(__dirname + '/static'));
})
+// Site -----------------------------------------------------------------------
+
app.get('/', function(req, res){
res.render('index.ejs', {});
});
-app.post('/test', function(req,res){
- temp.open('dotGraph', function(err, info) {
- fs.write(info.fd, req.body.data);
- fs.close(info.fd, function(err) {
- graphviz.parse( info.path, function(graph) {
- graph.render( "png", function(render) {
- img = '<img src="data:image/png;base64,'+render.toString("base64")+'"/>'
- res.send(img)
- }, function(code, out, err) {
- img = '<div class="error"><p><b>Render error (code '+code+')</b></p>';
- img += '<p>STDOUT : '+out+'</p>';
- img += '<p>STDERR : '+err+'</p></div>';
- res.send(img)
- });
- }, function(code, out, err){
- img = '<div class="error"><p><b>Parser error (code '+code+')</b></p>';
- img += '<p>STDERR : '+err+'</p></div>';
- img += '<p>STDOUT : '+out+'</p></div>';
- res.send(img)
- });
- });
+// APIs -----------------------------------------------------------------------
+
+app.get('/dotgraph.min.js', function(req,res) {
+ fs.readFile(__dirname+'/static/dotgraph.js', function (err, data) {
+ if (err) throw err;
+ res.contentType('text/javascript');
+ res.send( jsmin(data.toString('utf8') ) );
});
})
-app.get('/draw/*', function(req,res){
+function __do( req, res, data ) {
+ graphviz.parse( data, function(graph) {
+ graph.render( "png", function(render) {
+ img = '<img src="data:image/png;base64,'+render.toString("base64")+'"/>'
+ res.send(img)
+ }, function(code, out, err) {
+ img = '<div class="error"><p><b>Render error (code '+code+')</b></p>';
+ img += '<p>STDOUT : '+out+'</p>';
+ img += '<p>STDERR : '+err+'</p></div>';
+ res.send(img)
+ });
+ }, function(code, out, err){
+ img = '<div class="error"><p><b>Parser error (code '+code+')</b></p>';
+ img += '<p>STDERR : '+err+'</p></div>';
+ img += '<p>STDOUT : '+out+'</p></div>';
+ res.send(img)
+ });
+}
+
+app.post('/script', function(req,res){
+ __do(req, res, req.body.data)
+})
+
+app.get('/file/*', function(req,res){
var urlData = url.parse(req.params[0]);
var urlPort = urlData.port;
@@ -62,27 +80,7 @@ app.get('/draw/*', function(req,res){
res.send('<div class="error"><p><b>'+req.params[0]+'</b> does not exist (404 error)</p></div>');
} else {
response.on('data', function (chunk) {
- temp.open('dotGraph', function(err, info) {
- fs.write(info.fd, chunk);
- fs.close(info.fd, function(err) {
- graphviz.parse( info.path, function(graph) {
- graph.render( "png", function(render) {
- img = '<img src="data:image/png;base64,'+render.toString("base64")+'"/>'
- res.send(img)
- }, function(code, out, err) {
- img = '<div class="error"><p><b>Render error (code '+code+')</b></p>';
- img += '<p>STDOUT : '+out+'</p>';
- img += '<p>STDERR : '+err+'</p></div>';
- res.send(img)
- });
- }, function(code, out, err){
- img = '<div class="error"><p><b>Parser error (code '+code+')</b></p>';
- img += '<p>STDERR : '+err+'</p></div>';
- img += '<p>STDOUT : '+out+'</p></div>';
- res.send(img)
- });
- });
- });
+ __do(req, res, chunk)
});
}
});
@@ -0,0 +1,35 @@
+var DotGraphClient = {
+ version: '0.0.1',
+ host: 'http://localhost:3000',
+
+ require: function(libraryName) {
+ try{
+ document.write('<script type="text/javascript" src="'+libraryName+'"><\/script>');
+ } catch(e) {
+ var script = document.createElement('script');
+ script.type = 'text/javascript';
+ script.src = libraryName;
+ document.getElementsByTagName('head')[0].appendChild(script);
+ }
+ },
+
+ load: function() {
+ DotGraphClient.require( 'http://code.jquery.com/jquery-latest.min.js' );
+ }
+};
+
+DotGraphClient.load();
+
+var DotGraph = {
+ file: function( url, id ) {
+ $.get(DotGraphClient.host+'/file/'+url, function(data) {
+ $(id).html(data);
+ });
+ },
+
+ script: function( data, id ) {
+ $.post(DotGraphClient.host+'/script', { data: data }, function(data) {
+ $(id).html(data);
+ });
+ }
+};
@@ -2,30 +2,26 @@
<table>
<tr><td>
- <p>Enter your dot code :</p>
+ <p>Enter your dot code :</p>
<textarea id="dot" cols="60" rows="20"></textarea><br />
- <input id="button" type="button" value="Generate" /><br />
- <p>Or give me a script URL :<br/><small>Try http://dl.dropbox.com/u/72629/cluster.dot</p>
- <input id="file" type="text" size="50"/>
- <input id="bouton" type="button" value="Let see!" /><br />
+ <input id="button" type="button" value="Generate" /><br />
+ <p>Or give me a script URL :<br/><small>Try http://dl.dropbox.com/u/72629/cluster.dot</p>
+ <input id="file" type="text" size="50"/>
+ <input id="bouton" type="button" value="Let see!" /><br />
</td><td>
<div id="result"></div>
</td></tr>
</table>
<script type="text/javascript">
$('#button').click(function() {
- $.post('test', { data: $('#dot').val() }, function(data) {
- $('#result').html(data);
- });
+ DotGraph.script( $('#dot').val(), '#result' )
});
</script>
<script type="text/javascript">
$('#bouton').click(function() {
- $.get('draw/'+$('#file').val(), function(data) {
- $('#result').html(data);
- });
+ DotGraph.file( $('#file').val(), '#result' )
});
</script>
@@ -7,7 +7,7 @@
padding: 5px;
}
</style>
- <script src="http://code.jquery.com/jquery-latest.min.js"></script>
+ <script src="/dotgraph.min.js"></script>
</head>
<body>
<%- body %>

0 comments on commit 6a6aa04

Please sign in to comment.