Skip to content

Instantly share code, notes, and snippets.

@schlessera
Created February 22, 2022 22:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save schlessera/adad08028b680c2bfaf8d35e4a081df0 to your computer and use it in GitHub Desktop.
Save schlessera/adad08028b680c2bfaf8d35e4a081df0 to your computer and use it in GitHub Desktop.
Gulp task to process PlantUML diagrams, including those found in source code (OLD)
var gulp = require('gulp');
var rename = require('gulp-rename');
var changed = require('gulp-changed');
var request = require('sync-request');
var through = require('through2');
var gutil = require('gulp-util');
var prettyHrtime = require('pretty-hrtime');
var fs = require('fs');
var config = require('../config');
// Load legacy deflate library for PlantUML
filedata = fs.readFileSync('./gulp/util/rawdeflate.js', 'utf8');
eval(filedata);
// main task
gulp.task('diagrams', ['diagrams-svg', 'diagrams-png']);
// request SVG files
gulp.task('diagrams-svg', function() {
return gulp.src(config.diagrams.buildSrc)
.pipe(changed(config.diagrams.dest + '/svg', {extension: '.svg'}))
.pipe(through.obj(function (data, enc, cb) {
URL = geturl(data, 'svg');
startTime = process.hrtime();
var content = request('GET', URL);
data.contents = content.body;
this.push(data);
taskTime = process.hrtime(startTime);
gutil.log('Downloaded \'' + gutil.colors.green(data.relative) + '\' in ' + gutil.colors.magenta(prettyHrtime(taskTime)));
cb();
}))
.pipe(rename(function( path ) {
path.dirname += '/svg';
path.extname = '.svg';
}))
.pipe(gulp.dest(config.diagrams.dest));
});
// request PNG files
gulp.task('diagrams-png', function() {
return gulp.src(config.diagrams.buildSrc)
.pipe(changed(config.diagrams.dest + '/png', {extension: '.png'}))
.pipe(through.obj(function (data, enc, cb) {
URL = geturl(data, 'png');
startTime = process.hrtime();
var content = request('GET', URL);
data.contents = content.body;
this.push(data);
taskTime = process.hrtime(startTime);
gutil.log('Downloaded \'' + gutil.colors.green(data.relative) + '\' in ' + gutil.colors.magenta(prettyHrtime(taskTime)));
cb();
}))
.pipe(rename(function( path ) {
path.dirname += '/png';
path.extname = '.png';
}))
.pipe(gulp.dest(config.diagrams.dest));
});
// deflate puml file, run it through PlantUML-modified encode64
function geturl(file, version) {
var note = 1;
var UML = file.contents.toString('utf-8');
var includes = UML.match(/\s!include\s+[a-z0-9\.\-\_/\\:]+/ig);
if( includes != null ) {
includes.forEach( function( include ) {
filename = include.replace("!include", "").trim();
try {
data = fs.readFileSync(filename);
} catch (e) {
UML = UML.replace(include, 'note as N' + note + '\n<u><b>ERROR:</b></u>\nIncluded file not found:\n<b>' + filename + '</b>\nend note\n');
note += 1;
}
if( typeof data != "undefined" ) {
var sourceString = data.toString();
var UMLBufferArray = sourceString.match(/@startuml(.|\n|\f|\r)*@enduml/im);
if( UMLBufferArray != null ) {
var UMLBuffer = UMLBufferArray[0].toString();
UMLBuffer = UMLBuffer.replace("@startuml", "");
UMLBuffer = UMLBuffer.replace("@enduml", "");
UML = UML.replace(include, UMLBuffer);
} else {
UML = UML.replace(include, 'note as N' + note + '\n<u><b>WARNING:</b></u>\nIncluded file did not have any UML code!\nend note\n');
note += 1;
}
}
});
}
UML = unescape(encodeURIComponent(UML));
var CompressedUML = deflate( UML, 9 );
var URL = 'http://www.plantuml.com/plantuml/' + version + '/' + encode64(CompressedUML);
return URL;
}
// PlantUML-modified encode64
function encode64(data) {
r = "";
for (i=0; i<data.length; i+=3) {
if (i+2==data.length) {
r +=append3bytes(data.charCodeAt(i), data.charCodeAt(i+1), 0);
} else if (i+1==data.length) {
r += append3bytes(data.charCodeAt(i), 0, 0);
} else {
r += append3bytes(data.charCodeAt(i), data.charCodeAt(i+1), data.charCodeAt(i+2));
}
}
return r;
}
// used by PlantUML-modified encode64
function append3bytes(b1, b2, b3) {
c1 = b1 >> 2;
c2 = ((b1 & 0x3) << 4) | (b2 >> 4);
c3 = ((b2 & 0xF) << 2) | (b3 >> 6);
c4 = b3 & 0x3F;
r = "";
r += encode6bit(c1 & 0x3F);
r += encode6bit(c2 & 0x3F);
r += encode6bit(c3 & 0x3F);
r += encode6bit(c4 & 0x3F);
return r;
}
// used by PlantUML-modified encode64
function encode6bit(b) {
if (b < 10) {
return String.fromCharCode(48 + b);
}
b -= 10;
if (b < 26) {
return String.fromCharCode(65 + b);
}
b -= 26;
if (b < 26) {
return String.fromCharCode(97 + b);
}
b -= 26;
if (b == 0) {
return '-';
}
if (b == 1) {
return '_';
}
return '?';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment