var join         = require('path').join;
var readFileSync = require('fs').readFileSync;

var phantom = require('phantom');
var Q       = require('q');


module.exports = {
  blocks: {
    mermaid: {
      process: function(blk) {
        var body = blk.body;

        var args = block.args;
        if(args.length)
          body = readFileSync(args[0], 'utf8');

        return processBlock(body);
      }
    }
  }
};

function processBlock(body) {
  return convertToSvg(body)
      .then(function (svgCode) {
          return svgCode.replace(/mermaidChart1/g, getId());
      });
}

function convertToSvg(mermaidCode) {
  var deferred = Q.defer();
  phantom.create(function (ph) {
    ph.createPage(function (page) {

      var htmlPagePath = join(__dirname, 'convert/converter.html');

      page.open(htmlPagePath, function (status) {
        page.evaluate(
          function (code) {
            return renderToSvg(code);
          },
          function (result) {
            ph.exit();
            deferred.resolve(result);
          },
          mermaidCode);
      });
    });
  });

  return deferred.promise;
}

function getId() {
  function s4() {
    return Math.floor((1 + Math.random()) * 0x10000)
      .toString(16)
      .substring(1);
  }
  return "mermaidChart-" + s4() + s4();
}

@@ -80,16 +80,59 @@
 ileSync;
+%0Avar resolve      = require('url').resolve;
 %0A%0Avar ph
@@ -261,16 +261,18 @@
 ction(bl
+oc
 k) %7B%0A   
@@ -289,16 +289,18 @@
 ody = bl
+oc
 k.body;%0A
@@ -312,20 +312,19 @@
     var 
-args
+src
  = block
@@ -324,20 +324,26 @@
 = block.
+kw
 args
+.src
 ;%0A      
@@ -348,23 +348,75 @@
     
-if(args.length)
+%7B%0A          var path = decodeURI(resolve(this.ctx.file.path, src));
 %0A   
@@ -446,15 +446,12 @@
 ync(
-args%5B0%5D
+path
 , 'u
@@ -456,16 +456,26 @@
 'utf8');
+%0A        %7D
 %0A%0A      

Use kwargs