-
Notifications
You must be signed in to change notification settings - Fork 14
/
index.js
168 lines (146 loc) · 6.31 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*jshint esversion: 8 */
const path = require('path');
const fs = require('fs');
const plantuml = require('node-plantuml-back');
let nailgunRunning = false;
function processBlock(blk) {
return new Promise((resolve, reject) => {
const book = this;
let code;
if (!!blk.kwargs.src) {
code = fs.readFileSync(blk.kwargs.src, "utf8");
} else {
code = blk.body;
}
let config = blk.kwargs.config || this.book.config.get('pluginsConfig.uml', {});
config.format = config.format || "svg";
config.charset = config.charset || "utf8";
config.config = config.config || "classic";
if (this.ctx && this.ctx.ctx && this.ctx.ctx.file && this.ctx.ctx.file.path) {
const includePath = path.resolve(path.dirname(this.ctx.ctx.file.path));
const cwdPath = require("process").cwd();
config.include = (includePath === cwdPath) ? `${includePath}:${cwdPath}` : `${includePath}${process.platform === 'win32' ? ';' : ':'}${cwdPath}`;
}
const gen = plantuml.generate(code, config);
const chunks = [];
gen.out.on('data', chunk => {
chunks.push(chunk);
});
gen.out.on('end', () => {
const buffer = Buffer.concat(chunks);
let result;
if (config.format === 'ascii' || config.format === 'unicode') {
result = buffer.toString(config.charset)
.replace(/&/g, "&")
.replace(/</g, "<")
.replace(/>/g, ">")
.replace(/"/g, """)
.replace(/'/g, "'")
.replace(/\n/g, "<br>")
.replace(/\t/g, " ")
.replace(/ /g, " ");
} else if (config.format === 'svg') {
result = buffer.toString(config.charset);
} else {
result = `<img src="data:image/png;base64,${buffer.toString("base64")}">`;
}
resolve(result);
});
gen.out.on('error', error => {
reject(error);
});
});
}
module.exports = {
blocks: {
uml: {
process: processBlock
}
},
hooks: {
"init": function() {
if (!Object.keys(this.book.config.get('pluginsConfig.uml', {})).length) {
this.book.config.set('pluginsConfig.uml', {
format: 'svg',
charset: 'utf8',
config: 'classic',
nailgun: false
});
}
const config = this.book.config.get('pluginsConfig.uml', {});
if (!config.format) {
config.format = this.honkit || this.output.name === 'website' ? 'svg' : 'png';
}
config.charset = config.charset || 'utf8';
config.config = config.config || 'classic';
config.nailgun = config.nailgun || false;
this.book.config.set('pluginsConfig.uml', config);
if (config.nailgun && !nailgunRunning) {
plantuml.useNailgun(() => {
nailgunRunning = true;
});
}
},
"finish": function() {},
"finish:before": function() {},
"page:before": function(page) {
let umls = page.content.match(/```(\x20|\t)*(uml|puml|plantuml)((.*[\r\n]+)+?)?```/igm);
if (Array.isArray(umls)) {
for (let i = 0; i < umls.length; i++) {
page.content = page.content.replace(
umls[i],
umls[i].replace(/```(\x20|\t)*(uml|puml|plantuml)[ \t]+{(.*)}/i, matchedStr => {
let newStr = "";
let modeQuote = false;
let modeArray = false;
let modeChar = false;
let modeEqual = false;
let str = matchedStr.replace(/^\s+|\s+$/g, "").replace(/```(\x20|\t)*(uml|puml|plantuml)/i, "");
for (let j = 0; j < str.length; j++) {
if (str.charAt(j) === "\"") {
modeQuote = !modeQuote;
modeChar = true;
newStr += str.charAt(j);
continue;
}
if (str.charAt(j) === "[") {
modeArray = true;
newStr += str.charAt(j);
continue;
}
if (str.charAt(j) === "]") {
modeArray = false;
newStr += str.charAt(j);
continue;
}
if (modeQuote || modeArray) {
newStr += str.charAt(j);
} else {
if (/[A-Za-z0-9_]/.test(str.charAt(j))) {
modeChar = true;
newStr += str.charAt(j);
} else if (str.charAt(j) === "=") {
modeEqual = true;
modeChar = false;
newStr += str.charAt(j);
} else if (modeChar && modeEqual) {
modeChar = false;
modeEqual = false;
newStr += ",";
}
}
}
return `{% uml ${newStr.replace(/,$/, "")} %}`;
})
.replace(/```(\x20|\t)*(uml|puml|plantuml)/i, '{% uml %}')
.replace(/```/, '{% enduml %}')
);
}
}
return page;
},
"page": function(page) {
return page;
}
}
};