-
Notifications
You must be signed in to change notification settings - Fork 208
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
command line interface with #import support
- Loading branch information
Showing
3 changed files
with
47 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ nomnoml.sublime-workspace | |
node_modules/ | ||
|
||
output.* | ||
nomnoml-0.1.0.tgz |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#! /usr/bin/env node | ||
var fs = require('fs') | ||
var nomnoml = require('./nomnoml.js') | ||
|
||
var args = process.argv | ||
|
||
if (args[2] == '--help' || args.length == 2){ | ||
console.log(` | ||
Load source file and send rendered svg to stdout | ||
nomnoml <source_file> | ||
Load source file and save rendered svg to <output_file> | ||
nomnoml <source_file> <output_file> | ||
Third parameter overrides the import depth limit | ||
that protects us from recursive imports | ||
nomnoml <source_file> <output_file> <max_import_chain_length>`) | ||
return | ||
} | ||
|
||
var maxImportChainLength = args[4] || 10 | ||
|
||
var svg = nomnoml.renderSvg(preprocessFile(args[2], 0)) | ||
if (args[3]){ | ||
fs.writeFileSync(args[3], svg) | ||
} | ||
else { | ||
console.log(svg) | ||
} | ||
|
||
function preprocessFile(filepath, depth){ | ||
if (depth > maxImportChainLength) | ||
throw Error('max_import_chain_length exceeded') | ||
var source = fs.readFileSync(filepath, {encoding:'utf8'}) | ||
return source.replace(/#import: *(.*)/g, function (a, file) { | ||
return preprocessFile(file, depth+1) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters