1
1
/*jshint esversion: 8 */
2
2
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' ) ;
7
6
8
- var nailgunRunning = false ;
7
+ let nailgunRunning = false ;
9
8
10
9
function processBlock ( blk ) {
11
- var deferred = Q . defer ( ) ;
10
+ return new Promise ( ( resolve , reject ) => {
11
+ const book = this ;
12
12
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
+ }
21
19
22
- var config ;
20
+ let config = blk . kwargs . config || this . book . config . get ( 'pluginsConfig.uml' , { } ) ;
23
21
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" ;
29
25
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
+ }
39
31
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, "&" )
44
+ . replace ( / < / g, "<" )
45
+ . replace ( / > / g, ">" )
46
+ . replace ( / " / g, """ )
47
+ . replace ( / ' / g, "'" )
48
+ . replace ( / \n / g, "<br>" )
49
+ . replace ( / \t / g, " " )
50
+ . replace ( / / g, " " ) ;
51
+ } else if ( config . format === 'svg' ) {
52
+ result = buffer . toString ( config . charset ) ;
48
53
} else {
49
- config . include = includePath + ':' + cwdPath ;
54
+ result = `<img src="data:image/png;base64, ${ buffer . toString ( "base64" ) } ">` ;
50
55
}
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, "&" )
66
- . replace ( / < / g, "<" )
67
- . replace ( / > / g, ">" )
68
- . replace ( / " / g, """ )
69
- . replace ( / ' / g, "'" )
70
- . replace ( / \n / g, "<br>" )
71
- . replace ( / \t / g, " " )
72
- . replace ( / / g, " " ) ;
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
+ } ) ;
79
58
80
- deferred . resolve ( result ) ;
59
+ gen . out . on ( 'error' , error => {
60
+ reject ( error ) ;
61
+ } ) ;
81
62
} ) ;
82
-
83
- return deferred . promise ;
84
63
}
85
64
86
65
module . exports = {
@@ -90,12 +69,6 @@ module.exports = {
90
69
}
91
70
} ,
92
71
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
99
72
"init" : function ( ) {
100
73
if ( ! Object . keys ( this . book . config . get ( 'pluginsConfig.uml' , { } ) ) . length ) {
101
74
this . book . config . set ( 'pluginsConfig.uml' , {
@@ -106,124 +79,80 @@ module.exports = {
106
79
} ) ;
107
80
}
108
81
109
- var config = this . book . config . get ( 'pluginsConfig.uml' , { } ) ;
82
+ const config = this . book . config . get ( 'pluginsConfig.uml' , { } ) ;
110
83
111
84
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' ;
128
86
}
129
87
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 ;
139
91
140
92
this . book . config . set ( 'pluginsConfig.uml' , config ) ;
141
93
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 ( ( ) => {
145
96
nailgunRunning = true ;
146
97
} ) ;
147
98
}
148
99
} ,
149
100
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 ( ) { } ,
159
102
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 ( ) { } ,
162
104
163
- // This is called before parsing documents
164
105
"page:before" : function ( page ) {
165
- // Get all code texts
166
- umls = page . content . match ( / ` ` ` ( \x20 | \t ) * ( u m l | p u m l | p l a n t u m l ) ( ( .* [ \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 ) * ( u m l | p u m l | p l a n t u m l ) ( ( .* [ \r \n ] + ) + ?) ? ` ` ` / igm) ;
107
+ if ( Array . isArray ( umls ) ) {
108
+ for ( let i = 0 ; i < umls . length ; i ++ ) {
170
109
page . content = page . content . replace (
171
110
umls [ i ] ,
172
- // Parameter parser for user argument to gitbook argument
173
- umls [ i ] . replace ( / ` ` ` ( \x20 | \t ) * ( u m l | p u m l | p l a n t u m l ) [ \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 ) * ( u m l | p u m l | p l a n t u m l ) / 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 ) * ( u m l | p u m l | p l a n t u m l ) [ \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 ) * ( u m l | p u m l | p l a n t u m l ) / 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 - Z a - z 0 - 9 _ ] / . test ( str . charAt ( j ) ) ) {
189
140
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 - Z a - z 0 - 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 += "," ;
220
150
}
221
151
}
152
+ }
222
153
223
- newStr = newStr . replace ( / , $ / , "" ) ;
224
-
225
- return "{% uml " + newStr + " %}" ;
226
- } )
154
+ return `{% uml ${ newStr . replace ( / , $ / , "" ) } %}` ;
155
+ } )
227
156
. replace ( / ` ` ` ( \x20 | \t ) * ( u m l | p u m l | p l a n t u m l ) / i, '{% uml %}' )
228
157
. replace ( / ` ` ` / , '{% enduml %}' )
229
158
) ;
@@ -232,9 +161,7 @@ module.exports = {
232
161
return page ;
233
162
} ,
234
163
235
- // This is called when page html generation
236
164
"page" : function ( page ) {
237
- // This is called when page html generation
238
165
return page ;
239
166
}
240
167
}
0 commit comments