Skip to content

Commit

Permalink
v0.7.0
Browse files Browse the repository at this point in the history
expose compileFile for usage from NodeJS environment
  • Loading branch information
skanaar committed Apr 26, 2020
1 parent 8287e07 commit 5c3e61e
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 21 deletions.
4 changes: 4 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## [0.7.0] - 2020-04-26

- expose compileFile to allow usage in a NodeJS environment

## [0.6.2] - 2019-10-27

- fix bug where SVG output did not get the correct text color
Expand Down
24 changes: 7 additions & 17 deletions dist/nomnoml-cli.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#! /usr/bin/env node
var fs = require('fs')
var path = require('path');
var nomnoml = require('./nomnoml.js')

var args = process.argv
var [_, _, filename, outfile, optionalMaxImportDepth] = process.argv

if (args[2] == '--help' || args.length == 2){
if (filename == '--help' || process.argv.length == 2){
console.log(`
Nomnoml command line utility for generating SVG diagrams.
Expand All @@ -20,25 +19,16 @@ if (args[2] == '--help' || args.length == 2){
Third parameter overrides the import depth limit
that protects us from recursive imports:
> node nomnoml-cli.js <source_file> <output_file> <max_import_chain_length>`)
> node nomnoml-cli.js <source_file> <output_file> <max_import_depth>`)
return
}

var maxImportChainLength = args[4] || 10
var maxImportDepth = optionalMaxImportDepth || 10

var svg = nomnoml.renderSvg(preprocessFile(args[2], 0))
if (args[3]){
fs.writeFileSync(args[3], svg)
var svg = nomnoml.renderSvg(nomnoml.compileFile(filename, maxImportDepth, 0))
if (outfile){
fs.writeFileSync(outfile, 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(path.dirname(filepath) + '/' + file, depth+1)
})
}
39 changes: 38 additions & 1 deletion dist/nomnoml.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,43 @@ var nomnoml;
}
nomnoml.layout = layout;
})(nomnoml || (nomnoml = {}));
var __extends = (this && this.__extends) || (function () {
var extendStatics = function (d, b) {
extendStatics = Object.setPrototypeOf ||
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
return extendStatics(d, b);
};
return function (d, b) {
extendStatics(d, b);
function __() { this.constructor = d; }
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
};
})();
var nomnoml;
(function (nomnoml) {
var ImportDepthError = (function (_super) {
__extends(ImportDepthError, _super);
function ImportDepthError() {
return _super.call(this, 'max_import_depth exceeded') || this;
}
return ImportDepthError;
}(Error));
nomnoml.ImportDepthError = ImportDepthError;
function compileFile(filepath, maxImportDepth, depth) {
var fs = require('fs');
var path = require('path');
if (depth > maxImportDepth) {
throw new ImportDepthError();
}
var source = fs.readFileSync(filepath, { encoding: 'utf8' });
var directory = path.dirname(filepath);
return source.replace(/#import: *(.*)/g, function (a, file) {
return compileFile(path.join(directory, file), depth + 1);
});
}
nomnoml.compileFile = compileFile;
})(nomnoml || (nomnoml = {}));
var nomnoml;
(function (nomnoml) {
function fitCanvasSize(canvas, rect, zoom) {
Expand Down Expand Up @@ -163,7 +200,7 @@ var nomnoml;
nomnoml.render(graphics, config, layout, measurer.setFont);
return { config: config };
}
nomnoml.version = '0.6.2';
nomnoml.version = '0.7.0';
function draw(canvas, code, scale) {
return parseAndRender(code, nomnoml.skanaar.Canvas(canvas), canvas, scale || 1);
}
Expand Down
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nomnoml",
"version": "0.6.2",
"version": "0.7.0",
"description": "The sassy UML renderer that generates diagrams from text",
"homepage": "http://www.nomnoml.com",
"author": "Daniel Kallin <[email protected]>",
Expand All @@ -11,9 +11,11 @@
"main": "dist/nomnoml.js",
"files": [
"dist/nomnoml.js",
"dist/nomnoml-file.js",
"dist/nomnoml-cli.js"
],
"dependencies": {
"@types/node": "^13.13.2",
"dagre": "0.8.4"
},
"devDependencies": {
Expand Down
25 changes: 25 additions & 0 deletions src/node-compiler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace nomnoml {

export class ImportDepthError extends Error {
constructor() {
super('max_import_depth exceeded')
}
}

export function compileFile(filepath: string, maxImportDepth: number, depth?: number): string {
var fs = require('fs')
var path = require('path')

if (depth > maxImportDepth) {
throw new ImportDepthError()
}

var source = fs.readFileSync(filepath, {encoding:'utf8'})
var directory = path.dirname(filepath)

return source.replace(/#import: *(.*)/g, function (a: any, file: string) {
return compileFile(path.join(directory, file), depth+1)
})
}

}
2 changes: 1 addition & 1 deletion src/nomnoml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ namespace nomnoml {
return { config: config }
}

export var version = '0.6.2'
export var version = '0.7.0'

export function draw(canvas: HTMLCanvasElement, code: string, scale: number): { config: Config } {
return parseAndRender(code, skanaar.Canvas(canvas), canvas, scale || 1)
Expand Down

0 comments on commit 5c3e61e

Please sign in to comment.