From 5c3e61e5e19fbdff93c37b63e5b53723fddf7530 Mon Sep 17 00:00:00 2001 From: skanaar Date: Sun, 26 Apr 2020 18:30:06 +0200 Subject: [PATCH] v0.7.0 expose compileFile for usage from NodeJS environment --- changelog.md | 4 ++++ dist/nomnoml-cli.js | 24 +++++++----------------- dist/nomnoml.js | 39 ++++++++++++++++++++++++++++++++++++++- package-lock.json | 7 ++++++- package.json | 4 +++- src/node-compiler.ts | 25 +++++++++++++++++++++++++ src/nomnoml.ts | 2 +- 7 files changed, 84 insertions(+), 21 deletions(-) create mode 100644 src/node-compiler.ts diff --git a/changelog.md b/changelog.md index 5f9cb8f..abc76cb 100644 --- a/changelog.md +++ b/changelog.md @@ -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 diff --git a/dist/nomnoml-cli.js b/dist/nomnoml-cli.js index 18d37e4..e14d53e 100644 --- a/dist/nomnoml-cli.js +++ b/dist/nomnoml-cli.js @@ -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. @@ -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 `) + > node nomnoml-cli.js `) 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) - }) } \ No newline at end of file diff --git a/dist/nomnoml.js b/dist/nomnoml.js index 13bc03a..ec058bd 100644 --- a/dist/nomnoml.js +++ b/dist/nomnoml.js @@ -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) { @@ -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); } diff --git a/package-lock.json b/package-lock.json index dfdd40f..3540af3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,9 +1,14 @@ { "name": "nomnoml", - "version": "0.6.2", + "version": "0.7.0", "lockfileVersion": 1, "requires": true, "dependencies": { + "@types/node": { + "version": "13.13.2", + "resolved": "https://registry.npmjs.org/@types/node/-/node-13.13.2.tgz", + "integrity": "sha512-LB2R1Oyhpg8gu4SON/mfforE525+Hi/M1ineICEDftqNVTyFg1aRIeGuTvXAoWHc4nbrFncWtJgMmoyRvuGh7A==" + }, "JSONSelect": { "version": "0.4.0", "resolved": "https://registry.npmjs.org/JSONSelect/-/JSONSelect-0.4.0.tgz", diff --git a/package.json b/package.json index e768ea6..fcb70e5 100644 --- a/package.json +++ b/package.json @@ -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 ", @@ -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": { diff --git a/src/node-compiler.ts b/src/node-compiler.ts new file mode 100644 index 0000000..d41011b --- /dev/null +++ b/src/node-compiler.ts @@ -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) + }) + } + +} \ No newline at end of file diff --git a/src/nomnoml.ts b/src/nomnoml.ts index b3731d0..6d58348 100644 --- a/src/nomnoml.ts +++ b/src/nomnoml.ts @@ -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)