Skip to content
This repository was archived by the owner on Apr 28, 2025. It is now read-only.

Commit d227492

Browse files
committed
refactor: remove Q and promise dependencies, refactor to native Promises
- Removed `Q` and `promise` dependencies in favor of native Promises. - Refactored `processBlock` to utilize modern Promise-based syntax. - Updated `package.json` dependencies: - Replaced `coveralls` with `codecov`. - Updated `honkit-tester` to version ^2.x. - Removed `q` and `promise` dependencies. - Updated version to 2.0.0 to reflect breaking changes. Signed-off-by: Huang Rui <[email protected]>
1 parent 14d9ac0 commit d227492

File tree

3 files changed

+685
-859
lines changed

3 files changed

+685
-859
lines changed

index.js

Lines changed: 101 additions & 174 deletions
Original file line numberDiff line numberDiff line change
@@ -1,86 +1,65 @@
11
/*jshint esversion: 8 */
22

3-
var path = require('path');
4-
var fs = require('fs');
5-
var plantuml = require('node-plantuml-back');
6-
var Q = require('q');
3+
const path = require('path');
4+
const fs = require('fs');
5+
const plantuml = require('node-plantuml-back');
76

8-
var nailgunRunning = false;
7+
let nailgunRunning = false;
98

109
function processBlock(blk) {
11-
var deferred = Q.defer();
10+
return new Promise((resolve, reject) => {
11+
const book = this;
1212

13-
var book = this;
14-
15-
var code;
16-
if (!!blk.kwargs.src) {
17-
code = fs.readFileSync(blk.kwargs.src , "utf8");
18-
} else {
19-
code = blk.body;
20-
}
13+
let code;
14+
if (!!blk.kwargs.src) {
15+
code = fs.readFileSync(blk.kwargs.src, "utf8");
16+
} else {
17+
code = blk.body;
18+
}
2119

22-
var config;
20+
let config = blk.kwargs.config || this.book.config.get('pluginsConfig.uml', {});
2321

24-
if (!!blk.kwargs.config) {
25-
config = blk.kwargs.config;
26-
} else {
27-
config = this.book.config.get('pluginsConfig.uml', {});
28-
}
22+
config.format = config.format || "svg";
23+
config.charset = config.charset || "utf8";
24+
config.config = config.config || "classic";
2925

30-
if (!config.format) {
31-
config.format = "svg";
32-
}
33-
if (!config.charset) {
34-
config.charset = "utf8";
35-
}
36-
if (!config.config) {
37-
config.config = "classic";
38-
}
26+
if (this.ctx && this.ctx.ctx && this.ctx.ctx.file && this.ctx.ctx.file.path) {
27+
const includePath = path.resolve(path.dirname(this.ctx.ctx.file.path));
28+
const cwdPath = require("process").cwd();
29+
config.include = (includePath === cwdPath) ? `${includePath}:${cwdPath}` : `${includePath}${process.platform === 'win32' ? ';' : ':'}${cwdPath}`;
30+
}
3931

40-
if (this.ctx && this.ctx.ctx && this.ctx.ctx.file && this.ctx.ctx.file.path) {
41-
var includePath = path.resolve(path.dirname(this.ctx.ctx.file.path));
42-
var cwdPath = require("process").cwd();
43-
if (includePath == cwdPath) {
44-
config.include = includePath + ':' + cwdPath;
45-
} else {
46-
if (require("process").platform == 'win32') {
47-
config.include = includePath + ';' + cwdPath;
32+
const gen = plantuml.generate(code, config);
33+
34+
const chunks = [];
35+
gen.out.on('data', chunk => {
36+
chunks.push(chunk);
37+
});
38+
gen.out.on('end', () => {
39+
const buffer = Buffer.concat(chunks);
40+
let result;
41+
if (config.format === 'ascii' || config.format === 'unicode') {
42+
result = buffer.toString(config.charset)
43+
.replace(/&/g, "&amp;")
44+
.replace(/</g, "&lt;")
45+
.replace(/>/g, "&gt;")
46+
.replace(/"/g, "&quot;")
47+
.replace(/'/g, "&#039;")
48+
.replace(/\n/g, "<br>")
49+
.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;")
50+
.replace(/ /g, "&nbsp;");
51+
} else if (config.format === 'svg') {
52+
result = buffer.toString(config.charset);
4853
} else {
49-
config.include = includePath + ':' + cwdPath;
54+
result = `<img src="data:image/png;base64,${buffer.toString("base64")}">`;
5055
}
51-
}
52-
}
53-
54-
var gen = plantuml.generate(code, config);
55-
56-
var chunks = [];
57-
gen.out.on('data', function(chunk) {
58-
chunks.push(chunk);
59-
});
60-
gen.out.on('end', function() {
61-
var buffer = Buffer.concat(chunks);
62-
var result;
63-
if (config.format == 'ascii' || config.format == 'unicode') {
64-
result = buffer.toString(config.charset)
65-
.replace(/&/g, "&amp;")
66-
.replace(/</g, "&lt;")
67-
.replace(/>/g, "&gt;")
68-
.replace(/"/g, "&quot;")
69-
.replace(/'/g, "&#039;")
70-
.replace(/\n/g, "<br>")
71-
.replace(/\t/g, "&nbsp;&nbsp;&nbsp;&nbsp;")
72-
.replace(/ /g, "&nbsp;");
73-
} else if (config.format == 'svg') {
74-
result = buffer.toString(config.charset);
75-
} else {
76-
// process config.format == 'png'
77-
result = `<img src="data:image/png;base64,${buffer.toString("base64")}">`;
78-
}
56+
resolve(result);
57+
});
7958

80-
deferred.resolve(result);
59+
gen.out.on('error', error => {
60+
reject(error);
61+
});
8162
});
82-
83-
return deferred.promise;
8463
}
8564

8665
module.exports = {
@@ -90,12 +69,6 @@ module.exports = {
9069
}
9170
},
9271
hooks: {
93-
// For all the hooks, this represent the current generator
94-
// [init", "finish", "finish:before", "page", "page:before"] are working.
95-
// page:* are marked as deprecated because it's better if plugins start using blocks instead.
96-
// But page and page:before will probably stay at the end (useful in some cases).
97-
98-
// This is called before the book is generated
9972
"init": function() {
10073
if (!Object.keys(this.book.config.get('pluginsConfig.uml', {})).length) {
10174
this.book.config.set('pluginsConfig.uml', {
@@ -106,124 +79,80 @@ module.exports = {
10679
});
10780
}
10881

109-
var config = this.book.config.get('pluginsConfig.uml', {});
82+
const config = this.book.config.get('pluginsConfig.uml', {});
11083

11184
if (!config.format) {
112-
// Auto select svg or png
113-
if (this.honkit) {
114-
// honkit support svg better, so use svg
115-
config.format = 'svg';
116-
} else {
117-
// NOTE: This fixed issue #2
118-
// https://github.com/vowstar/gitbook-plugin-uml/issues/2
119-
// Use SVG format by default in website when user not give
120-
// any configuration to get better result.
121-
if (this.output.name != 'website') {
122-
// gitbook pdf not support svg
123-
config.format = 'png';
124-
} else {
125-
config.format = 'svg';
126-
}
127-
}
85+
config.format = this.honkit || this.output.name === 'website' ? 'svg' : 'png';
12886
}
12987

130-
if (!config.charset) {
131-
config.charset = 'utf8';
132-
}
133-
if (!config.config) {
134-
config.config = 'classic';
135-
}
136-
if (!config.nailgun) {
137-
config.nailgun = false;
138-
}
88+
config.charset = config.charset || 'utf8';
89+
config.config = config.config || 'classic';
90+
config.nailgun = config.nailgun || false;
13991

14092
this.book.config.set('pluginsConfig.uml', config);
14193

142-
var startNailgun = this.book.config.get('pluginsConfig.uml.nailgun', false);
143-
if (startNailgun && !nailgunRunning) {
144-
plantuml.useNailgun(function() {
94+
if (config.nailgun && !nailgunRunning) {
95+
plantuml.useNailgun(() => {
14596
nailgunRunning = true;
14697
});
14798
}
14899
},
149100

150-
// This is called after the book generation
151-
"finish": function() {
152-
// This is called after the book generation
153-
},
154-
155-
// This is called before the end of book generation
156-
"finish:before": function() {
157-
// This is called before the end of book generation
158-
},
101+
"finish": function() {},
159102

160-
// The following hooks are called for each page of the book
161-
// and can be used to change page content (html, data or markdown)
103+
"finish:before": function() {},
162104

163-
// This is called before parsing documents
164105
"page:before": function(page) {
165-
// Get all code texts
166-
umls = page.content.match(/```(\x20|\t)*(uml|puml|plantuml)((.*[\r\n]+)+?)?```/igm);
167-
// Begin replace
168-
if (umls instanceof Array) {
169-
for (var i = 0, len = umls.length; i < len; i++) {
106+
let umls = page.content.match(/```(\x20|\t)*(uml|puml|plantuml)((.*[\r\n]+)+?)?```/igm);
107+
if (Array.isArray(umls)) {
108+
for (let i = 0; i < umls.length; i++) {
170109
page.content = page.content.replace(
171110
umls[i],
172-
// Parameter parser for user argument to gitbook argument
173-
umls[i].replace(/```(\x20|\t)*(uml|puml|plantuml)[ \t]+{(.*)}/i,
174-
function(matchedStr) {
175-
var newStr = "";
176-
var modeQuote = false;
177-
var modeArray = false;
178-
var modeChar = false;
179-
var modeEqual = false;
180-
// Trim left and right space
181-
var str = matchedStr.replace(/^\s+|\s+$/g,"");
182-
// Remove ```(uml|puml|plantuml) header
183-
str = str.replace(/```(\x20|\t)*(uml|puml|plantuml)/i, "");
184-
185-
// Build new str
186-
for(var i = 0; i < str.length; i++){
187-
if (str.charAt(i) == "\"") {
188-
modeQuote = !modeQuote;
111+
umls[i].replace(/```(\x20|\t)*(uml|puml|plantuml)[ \t]+{(.*)}/i, matchedStr => {
112+
let newStr = "";
113+
let modeQuote = false;
114+
let modeArray = false;
115+
let modeChar = false;
116+
let modeEqual = false;
117+
let str = matchedStr.replace(/^\s+|\s+$/g, "").replace(/```(\x20|\t)*(uml|puml|plantuml)/i, "");
118+
119+
for (let j = 0; j < str.length; j++) {
120+
if (str.charAt(j) === "\"") {
121+
modeQuote = !modeQuote;
122+
modeChar = true;
123+
newStr += str.charAt(j);
124+
continue;
125+
}
126+
if (str.charAt(j) === "[") {
127+
modeArray = true;
128+
newStr += str.charAt(j);
129+
continue;
130+
}
131+
if (str.charAt(j) === "]") {
132+
modeArray = false;
133+
newStr += str.charAt(j);
134+
continue;
135+
}
136+
if (modeQuote || modeArray) {
137+
newStr += str.charAt(j);
138+
} else {
139+
if (/[A-Za-z0-9_]/.test(str.charAt(j))) {
189140
modeChar = true;
190-
newStr += str.charAt(i);
191-
continue;
192-
}
193-
if (str.charAt(i) == "[") {
194-
modeArray = true;
195-
newStr += str.charAt(i);
196-
continue;
197-
}
198-
if (str.charAt(i) == "]") {
199-
modeArray = false;
200-
newStr += str.charAt(i);
201-
continue;
202-
}
203-
if (modeQuote || modeArray) {
204-
// In quote, keep all string
205-
newStr += str.charAt(i);
206-
} else {
207-
// Out of quote, process it
208-
if (str.charAt(i).match(/[A-Za-z0-9_]/)) {
209-
modeChar = true;
210-
newStr += str.charAt(i);
211-
} else if (str.charAt(i).match(/[=]/)) {
212-
modeEqual = true;
213-
modeChar = false;
214-
newStr += str.charAt(i);
215-
} else if (modeChar && modeEqual) {
216-
modeChar = false;
217-
modeEqual = false;
218-
newStr += ",";
219-
}
141+
newStr += str.charAt(j);
142+
} else if (str.charAt(j) === "=") {
143+
modeEqual = true;
144+
modeChar = false;
145+
newStr += str.charAt(j);
146+
} else if (modeChar && modeEqual) {
147+
modeChar = false;
148+
modeEqual = false;
149+
newStr += ",";
220150
}
221151
}
152+
}
222153

223-
newStr = newStr.replace(/,$/,"");
224-
225-
return "{% uml " + newStr + " %}";
226-
})
154+
return `{% uml ${newStr.replace(/,$/, "")} %}`;
155+
})
227156
.replace(/```(\x20|\t)*(uml|puml|plantuml)/i, '{% uml %}')
228157
.replace(/```/, '{% enduml %}')
229158
);
@@ -232,9 +161,7 @@ module.exports = {
232161
return page;
233162
},
234163

235-
// This is called when page html generation
236164
"page": function(page) {
237-
// This is called when page html generation
238165
return page;
239166
}
240167
}

0 commit comments

Comments
 (0)