4.0.0-beta6 • Published 8 years ago

yuml2svg v4.0.0-beta6

Weekly downloads
3,269
License
MIT
Repository
github
Last release
8 years ago

yUML to SVG

Installation

You can install it with npm:

npm install yuml2svg

Or with yarn:

yarn add yuml2svg

Features

  • Embedded rendering engine: No need to call an external web service

yUML syntax

Please refer to the wiki page.

API

The API exports a function that accepts as arguments:

  1. A Readable stream, a Buffer or a string containing the yUML diagram.
  2. An optional plain object containing the options for the rendering.
  3. An optional plain object containing the options for Viz.js. Check it out if you are using this package in the browser.

The API returns a Promise which resolves in a string containing SVG document as a string.

The options for the rendering are:

  • dir: The direction of the diagram "TB" (default) - topDown, "LR" - leftToRight, "RL" - rightToLeft
  • type: The type of SVG - "class" (default), "usecase", "activity", "state", "deployment", "package".
  • isDark: Option to get dark or light diagram

Here are some examples of a simple usage you can make of the API:

import fs from "fs/promises"; // N.B.: fs/promises is not available before Node 10.0.0
import yuml2svg from "yuml2svg";

/**
 * Renders a given file into a SVG string asynchronously
 * @param {string} filePath Path to the yUML diagram
 * @returns {Promise<string>} callback The SVG document that represents the yUML diagram
 */
const renderFile = filePath => fs.readFile(filePath).then(yuml2svg);

/**
 * Renders a given file into a SVG string asynchronously
 * @param {string} filePath Path to the yUML diagram
 * @param {{dir:string, type: string, isDark: boolean}} [options]
 * @param {{Module:Module, render: Function}} [vizOptions] @see https://github.com/mdaines/viz.js/wiki/2.0.0-API
 * @returns {Promise<string>} callback The SVG document that represents the yUML diagram
 */
const renderFileWithOptions = (filePath, options, vizOptions) =>
  fs.readFile(filePath).then(yuml => yuml2svg(yuml, options, vizOptions));

/**
 * Generates a SVG file from a yUML file
 * @param {string} inputFile Path to the .yuml document to read
 * @param {string} outputFile Path to the .svg file to write
 * @returns {Promise<>} Promise that resolves once the SVG file is written
 */
const generateSVG = async (inputFile, outputFile) => {
  const yuml = await fs.readFile(inputFile);
  const svg = await yuml2svg(yuml);

  return await fs.writeFile(outputFile, svg);
};

Or, if you don't like Promise nor async/await syntax, you can use it with good old callbacks:

const fs = require("fs");
const yuml2svg = require("yuml2svg");

/**
 * Renders a given file into a SVG string asynchronously
 * @param {string} filePath Path to the yUML diagram
 * @param {(Error, string)=>any} callback Async callback
 */
function renderFile(filePath, callback) {
  yuml2svg(fs.createReadStream(filePath))
    .then(function(svg) {
      callback(null, svg);
    })
    .catch(callback);
}

/**
 * Renders a given file into a SVG string asynchronously
 * @param {string} filePath Path to the yUML diagram
 * @param {{dir:string, type: string, isDark: boolean}} [options]
 * @param {{Module:Module, render: Function}} [vizOptions] @see https://github.com/mdaines/viz.js/wiki/2.0.0-API
 * @param {(Error, string)=>any} callback Async callback
 */
function renderFileWithOptions(filePath, options, vizOptions) {
  fs.readFile(filePath, function(err, yuml) {
    if (err) {
      callback(err);
    } else {
      yuml2svg(yuml, options, vizOptions)
        .then(function(svg) {
          callback(null, svg);
        })
        .catch(callback);
    }
  });
}

/**
 * Generates a SVG file from a yUML file
 * @param {string} inputFile Path to the .yuml document to read
 * @param {string} outputFile Path to the .svg file to write
 * @param {(Error)=>any} callback Async callback
 */
function generateSVG(inputFile, outputFile, callback) {
  fs.readFile(filePath, function(err, yuml) {
    if (err) {
      callback(err);
    } else {
      yuml2svg(yuml)
        .then(function(svg) {
          fs.writeFile(outputFile, svg, callback);
        })
        .catch(callback);
    }
  });
}

Run on the browser

You can find a working example of a browser implementation using webpack here: yuml2svg-playground.

The Stream API in not currently supported on the browser, the API can only deal with strings.

Credits

5.0.1

6 years ago

5.0.0

6 years ago

5.0.0-rc.1

6 years ago

5.0.0-beta.12

6 years ago

5.0.0-beta.11

6 years ago

5.0.0-rc

6 years ago

5.0.0-beta.10

6 years ago

5.0.0-beta.9

7 years ago

5.0.0-beta.8

7 years ago

4.2.2

7 years ago

5.0.0-beta.7

7 years ago

5.0.0-beta.6

7 years ago

5.0.0-beta.5

7 years ago

5.0.0-beta.4

7 years ago

5.0.0-beta.3

7 years ago

5.0.0-beta.2

7 years ago

4.2.1

7 years ago

5.0.0-beta.1

8 years ago

4.2.0

8 years ago

5.0.0-beta

8 years ago

4.0.1

8 years ago

4.0.0

8 years ago

4.0.0-rc

8 years ago

4.0.0-beta7

8 years ago

4.0.0-beta6

8 years ago

4.0.0-beta5

8 years ago

4.0.0-beta4

8 years ago

4.0.0-beta3

8 years ago

4.0.0-beta2

8 years ago

4.0.0-0

8 years ago

3.1.0

9 years ago

3.0.0

9 years ago