Skip to content

Commit e6e27fb

Browse files
committed
Merge remote-tracking branch 'nrekretep/master'
2 parents 1e74106 + 0427d53 commit e6e27fb

File tree

3 files changed

+1828
-20
lines changed

3 files changed

+1828
-20
lines changed

README.md

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,58 @@
1-
**GitBook PlantUml Plugin**
2-
==============
3-
* inspire by [romanlytkin/gitbook-plantuml](https://github.com/romanlytkin/gitbook-plantuml)
4-
* use [bitjourney/plantuml-service](https://github.com/bitjourney/plantuml-service)
5-
to support plantuml, so you do not need java run time on your host machine.
6-
7-
**How to use it:**
8-
--------------
9-
```$ npm install gitbook-plugin-plantuml-cloud```
10-
11-
***Additional requirements:***
12-
NO
1+
# GitBook PlantUml Plugin
2+
3+
* inspired by [romanlytkin/gitbook-plantuml](https://github.com/romanlytkin/gitbook-plantuml)
4+
5+
The plugin now supports two APIs for generating PlantUML diagrams:
6+
* [bitjourney/plantuml-service](https://github.com/bitjourney/plantuml-service)
7+
* [PlantUML Server](http://www.plantuml.com/plantuml/)
8+
9+
10+
## Installation
11+
12+
* Add the plugin to your book.json
13+
14+
```js
15+
{
16+
"plugins": ["plantuml-cloud"]
17+
}
18+
```
19+
20+
* Install the plugin to you gitbook
21+
22+
```$ gitbook install```
23+
24+
* No additional steps required
25+
26+
## Configuration
27+
28+
* If you do not add a plugin configuration to your book.json, then the following defaults will be used:
29+
30+
| Configuration option | Description | Default |
31+
| -------------------- | ----------- | ------- |
32+
| umlPath | Path to a directory where the generated images for the diagrams will be cached. | "assets/images/uml" |
33+
| type | Determines the type of the server side API | "plantuml-service" |
34+
| host | Host for the diagramming service | "plantuml-service.herokuapp.com" |
35+
| protocol | https or http | "https" |
36+
| path | URL Fragment which will be appended to the host part | "/svg/" |
37+
| blockRegex | Regular expression to select the diagramming text blocks. | ^```uml((.*\n)+?)?```$ |
38+
39+
* If want to use the PlantUML Server API the following changes need to be made to the plugin configuration in your book.json:
40+
41+
```js
42+
{
43+
"plugins": ["plantuml-cloud"],
44+
"pluginsConfig": {
45+
"plantuml-cloud": {
46+
"protocol": "http",
47+
"type": "plantuml-server",
48+
"host": "www.plantuml.com",
49+
"path": "/plantuml/svg/"
50+
}
51+
}
52+
}
53+
```
54+
55+
> The PlantUML Server API on plantuml.com expects the diagram text blocks in a special encoding. [Look here for more information.](http://plantuml.com/pte)
56+
> To make this encoding work in this plugin it was necessary to include some code for the deflate operations. [Look here for more information.](https://github.com/johan/js-deflate)
57+
58+
* [ ] Make the plugin work with nodejs zlib deflate implementation and remove the duplicated deflate code.

index.js

Lines changed: 60 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,71 @@
11
var fs = require('fs');
2-
var re = /^```uml((.*\n)+?)?```$/img;
32
var crypto = require('crypto');
43
var path = require('path');
54
var https = require('https');
5+
var http = require('http');
66
var qs = require('querystring');
7+
var zlib = require('zlib');
8+
9+
var plantUml = require('./plantuml_encode');
10+
11+
var options = {
12+
"umlPath": "assets/images/uml",
13+
"type": "plantuml-service",
14+
"host": "plantuml-service.herokuapp.com",
15+
"protocol": "https",
16+
"path": "/svg/",
17+
"blockRegex": "^```uml((.*[\r\n])+?)?```$"
18+
}
719

820
require('shelljs/global');
921

22+
23+
function plantumlServerEscape(block) {
24+
25+
//UTF8
26+
var compressedUml = unescape(encodeURIComponent(block));
27+
compressedUml = plantUml.encode64(plantUml.zip_deflate(compressedUml, 9));
28+
return compressedUml;
29+
}
30+
1031
module.exports = {
1132
hooks: {
1233
"init": function () {
1334
var output = this.output;
14-
var umlPath = output.resolve('assets/images/uml');
15-
35+
var config = this.config.values.pluginsConfig["plantuml-cloud"];
36+
if(config.umlPath != undefined) {
37+
options.umlPath = config.umlPath;
38+
}
39+
if(config.type != undefined) {
40+
options.type = config.type;
41+
}
42+
if(config.host != undefined) {
43+
options.host = config.host;
44+
}
45+
if(config.protocol != undefined) {
46+
options.protocol = config.protocol;
47+
}
48+
if(config.path != undefined) {
49+
options.path = config.path;
50+
}
51+
if(config.blockRegex != undefined) {
52+
options.blockRegex = config.blockRegex;
53+
}
54+
var umlPath = output.resolve(options.umlPath);
1655
mkdir('-p', umlPath);
1756
},
1857
"page:before": function (page) {
1958
var output = this.output;
2059
var content = page.content;
2160

2261
var umls = [];
62+
var re = new RegExp(options.blockRegex, 'img');
63+
2364
while ((match = re.exec(content))) {
2465
var rawBlock = match[0];
2566
var umlBlock = match[1];
2667
var md5 = crypto.createHash('md5').update(umlBlock).digest('hex');
27-
var svgPath = path.join('assets', 'images', 'uml', md5 + '.svg');
68+
var svgPath = path.join(options.umlPath, md5 + '.svg');
2869
umls.push({
2970
rawBlock: match[0],
3071
umlBlock: match[1],
@@ -39,9 +80,20 @@ module.exports = {
3980
return output.hasFile(uml.svgPath).then(exists => {
4081
if (!exists) {
4182
return new Promise((resolve, reject) => {
42-
https.request({
43-
host: 'plantuml-service.herokuapp.com',
44-
path: '/svg/' + qs.escape(uml.umlBlock)
83+
84+
var client = http;
85+
if(options.protocol == "https"){
86+
client = https;
87+
}
88+
89+
var path = options.path + qs.escape(uml.umlBlock);
90+
if(options.type == "plantuml-server") {
91+
path = options.path + plantumlServerEscape(uml.umlBlock);
92+
}
93+
94+
client.request({
95+
host: options.host,
96+
path: path
4597
}, (res) => {
4698
var ws = fs.createWriteStream(output.resolve(uml.svgPath));
4799
res.pipe(ws);
@@ -58,4 +110,4 @@ module.exports = {
58110
"page": function (page) { return page; },
59111
"page:after": function (page) { return page; }
60112
}
61-
};
113+
};

0 commit comments

Comments
 (0)