Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command line renderer #27

Closed
VWoeltjen opened this issue Jul 22, 2015 · 4 comments
Closed

Command line renderer #27

VWoeltjen opened this issue Jul 22, 2015 · 4 comments

Comments

@VWoeltjen
Copy link

Would it be possible to get a node command line interface for rendering nomnoml files?

It would be great to be able to:

npm install nomnoml
nomnoml myDiagram.nomnoml -o docs/myDiagram.png

...or similar.

My use case is that I'd like to be able to keep plain-text versions of diagrams under version control, and render these to image files as part of an npm build process to generate documentation. I'd love to use nomnoml for this purpose, as the rendering and syntax are quite nice compared to the few alternatives I've seen.

@korroz
Copy link
Contributor

korroz commented Jul 22, 2015

I have looked into this since I had the same idea. The first step to reach this goal has already been completed when nomnoml was made into a standalone library. However, to render images you can't rely only on node. You would need to run the code in an environment with a graphical context (with support specifically for the canvas api), such as phantomjs. But making the core nomnoml depend on phantomjs just to support command line rendering is probably not a good idea. A better way would be to create another package that brings phantomjs and nomnoml together, something like nomnoml-phantomjs-renderer.

When time allows this is probably something I'll pursue, unless someone beats me to it, hehe.

@VWoeltjen
Copy link
Author

Took a poke at doing this with canvas ...

GLOBAL.window = GLOBAL.window || {}; // nomnoml expects window to be defined
(function () {
    'use strict';

    var fs = require('fs'),
        lodash = require('lodash'),
        nomnoml = require('nomnoml'),
        Canvas = require('canvas'),
        canvas = new Canvas(800, 800),
        source;

    if (process.argv.length < 3) {
        console.log("Supply a filename.");
        return;
    }

    source = fs.readFileSync(process.argv[2], { encoding: 'utf-8' });
    nomnoml.draw(canvas, source, 1.0);
    canvas.pngStream().pipe(process.stdout);
}());

But hit:

/.../node_modules/nomnoml/dist/nomnoml.js:953
        var g = new dagre.Digraph()
                    ^
ReferenceError: dagre is not defined

Not sure what to make of this exactly, except that it seems some work is required to get nomnoml to run under plain node. phantomjs may be a path of less resistance.

@VWoeltjen
Copy link
Author

I realized I can get around that problem by tweaking little window workaround; changing that first line to GLOBAL.window = GLOBAL.window || GLOBAL allows dagre to get exposed in the global scope where nomnoml expects it, at which point the above script works quite nicely. (Downside is that canvas requires Cairo to be installed, which needs to be done separately from npm - a phantomjs solution might be more portable.)

I think there might be some small changes to nomnoml that would eliminate the need for this workaround when running under node, but for the time being the approach above is sufficient to my needs. I agree with @korroz that the command-line renderer makes more sense as a separate package using nomnoml, as opposed to a modification to nomnoml, so I'll go ahead and close this.

@korroz
Copy link
Contributor

korroz commented Jul 24, 2015

@VWoeltjen Nice one!

When I made sure nomnoml could be required in node I knew that it didn't work well with dagre since we're using a browserify version of it. But since the issue of not having a graphical context in node was also a problem I deferred working out node compatibility for now. Nice to see the only thing dagre wanted was GLOBAL.window though, good find.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants