@@ -25,29 +25,34 @@ interface UrlPathNodes {
25
25
26
26
interface Resource {
27
27
name : string ;
28
- args : Variable [ ] ;
29
- attrs : Variable [ ] ;
28
+ args : variable [ ] ;
29
+ attrs : variable [ ] ;
30
30
url : string ;
31
31
}
32
32
33
- interface Variable {
33
+ interface Snippet {
34
+ name : string ;
35
+ example : string ;
36
+ }
37
+
38
+ interface variable {
34
39
name : string ;
35
40
description : string ;
36
41
}
37
42
38
- const resourcesPath = "terraform-provider-tencentcloud/website/docs/r/" ;
39
- const dataSourcePath = "terraform-provider-tencentcloud/website/docs/d/" ;
40
- const indexPath = "terraform-provider-tencentcloud/website/tencentcloud.erb" ;
43
+ const resourcesPath = "/Users/luoyin/Code/ terraform-provider-tencentcloud/website/docs/r/" ;
44
+ const dataSourcePath = "/Users/luoyin/Code/ terraform-provider-tencentcloud/website/docs/d/" ;
45
+ const indexPath = "/Users/luoyin/Code/ terraform-provider-tencentcloud/website/tencentcloud.erb" ;
41
46
// ================================== FUNCTIONS ==================================
42
47
43
48
function getParsed ( filename : string ) : Promise < cheerio . Root > {
44
49
return new Promise ( ( resolve , reject ) => {
45
- var file = fs . readFileSync ( resourcesPath + filename ) + "" ;
50
+ let file = fs . readFileSync ( resourcesPath + filename ) + "" ;
46
51
marked ( file , ( err , result ) => {
47
52
if ( err ) {
48
53
return reject ( err ) ;
49
54
}
50
- var $ = load ( result ) ;
55
+ let $ = load ( result ) ;
51
56
resolve ( $ ) ;
52
57
} ) ;
53
58
} ) ;
@@ -60,7 +65,7 @@ function getAllParsed(files: string[]): Promise<cheerio.Root>[] {
60
65
}
61
66
62
67
function getNumWithArgumentReference ( $s : cheerio . Root [ ] ) : number {
63
- var result = _ . map ( $s , $ => {
68
+ let result = _ . map ( $s , $ => {
64
69
return $ ( "h2" ) . filter ( ( z , el ) => {
65
70
return $ ( el ) . text ( ) === "Argument Reference" ;
66
71
} ) . length ;
@@ -69,7 +74,7 @@ function getNumWithArgumentReference($s: cheerio.Root[]): number {
69
74
}
70
75
71
76
function getNumWithAttributesReference ( $s : cheerio . Root [ ] ) : number {
72
- var result = _ . map ( $s , $ => {
77
+ let result = _ . map ( $s , $ => {
73
78
return $ ( "h2" ) . filter ( ( z , el ) => {
74
79
return $ ( el ) . text ( ) === "Attributes Reference" ;
75
80
} ) . length ;
@@ -83,19 +88,19 @@ function getNumWithAttributesReference($s: cheerio.Root[]): number {
83
88
* @param {* } $ - The full page as a cheerio object
84
89
*/
85
90
function extractArgumentsContent ( $ : cheerio . Root ) : ArgumentNodes {
86
- var argsH2 = $ ( "h2" ) . filter ( ( z , el ) => {
91
+ let argsH2 = $ ( "h2" ) . filter ( ( z , el ) => {
87
92
return $ ( el ) . text ( ) === "Argument Reference" ;
88
93
} ) ;
89
94
if ( argsH2 . length !== 1 ) {
90
95
throw "Didn't find correct number of h2 > Arguments Reference" ;
91
96
}
92
- var nodes = [ ] ;
93
- var currentNode : any = argsH2 [ 0 ] ;
97
+ let nodes = [ ] ;
98
+ let currentNode : any = argsH2 [ 0 ] ;
94
99
while ( true ) {
95
100
if ( ! ( currentNode . type === "text" && currentNode [ "data" ] === "\n" ) ) {
96
101
nodes . push ( currentNode ) ;
97
102
}
98
- var nextSibling = _ . get ( currentNode , "nextSibling" ) ;
103
+ let nextSibling = _ . get ( currentNode , "nextSibling" ) ;
99
104
if ( ! nextSibling || _ . get ( nextSibling , "name" ) === "h2" ) {
100
105
break ;
101
106
}
@@ -105,21 +110,21 @@ function extractArgumentsContent($: cheerio.Root): ArgumentNodes {
105
110
}
106
111
107
112
function extractAttributesContent ( $ : cheerio . Root ) : AttributeNodes {
108
- var argsH2 = $ ( "h2" ) . filter ( ( z , el ) => {
113
+ let argsH2 = $ ( "h2" ) . filter ( ( z , el ) => {
109
114
return $ ( el ) . text ( ) === "Attribute Reference" || $ ( el ) . text ( ) === "Attributes Reference" ;
110
115
} ) ;
111
116
if ( argsH2 . length !== 1 ) {
112
117
console . error ( `Didn't find any attributes on ${ extractResourceName ( $ ) } ` ) ;
113
118
return { attributeNodes : [ ] } ;
114
119
// throw `Didn't find correct number of h2 > Attributes Reference on ${extractResourceName($)}`;
115
120
}
116
- var nodes = [ ] ;
117
- var currentNode : any = argsH2 [ 0 ] ;
121
+ let nodes = [ ] ;
122
+ let currentNode : any = argsH2 [ 0 ] ;
118
123
while ( true ) {
119
124
if ( ! ( currentNode . type === "text" && currentNode [ "data" ] === "\n" ) ) {
120
125
nodes . push ( currentNode ) ;
121
126
}
122
- var nextSibling = _ . get ( currentNode , "nextSibling" ) ;
127
+ let nextSibling = _ . get ( currentNode , "nextSibling" ) ;
123
128
if ( ! nextSibling || _ . get ( nextSibling , "name" ) === "h2" ) {
124
129
break ;
125
130
}
@@ -128,33 +133,62 @@ function extractAttributesContent($: cheerio.Root): AttributeNodes {
128
133
return { attributeNodes : nodes } ;
129
134
}
130
135
131
- // function extractExampleContents($: cheerio.Root): SnippetNodes {
132
- // var argsH2 = $("h2").filter((z, el) => {
133
- // return $(el).text() == "Example Usage";
134
- // });
135
- // if (argsH2.length != 1) {
136
- // console.error(`Didn't find any example on ${extractResourceName($)}`);
137
- // throw "Didn't find correct number of h2 > Example Usage";
138
- // }
139
- // }
140
-
141
- // function extractExamples(argNodes: SnippetNodes, $: cheerio.Root): Variable[] {
142
- // if (argNodes.snippetNodes.length == 0) return [];
143
-
144
- // let nodes = argNodes.snippetNodes;
145
-
146
- // // Find the first ul
147
- // var firstUl = _.find(nodes, (o: any) => o.name == "ul");
148
- // if (!firstUl) {
149
- // console.error(`Didn't find a UL when searching through snippets on ${extractResourceName($)}`);
150
- // }
151
- // }
152
-
153
- function extractArguments ( argNodes : ArgumentNodes , $ : cheerio . Root ) : Variable [ ] {
136
+ function extractExampleContent ( $ : cheerio . Root ) : string {
137
+ let expH2 = $ ( "h2" ) . filter ( ( z , el ) => {
138
+ return $ ( el ) . text ( ) === "Example Usage" ;
139
+ } ) ;
140
+ if ( expH2 . length !== 1 ) {
141
+ console . error ( `Didn't find any example on ${ extractResourceName ( $ ) } ` ) ;
142
+ return "" ;
143
+ // throw "Didn't find correct number of h2 > Example Usage";
144
+ }
145
+ let content = "This example will be ready later." ;
146
+ let currentNode : any = expH2 [ 0 ] ;
147
+ while ( true ) {
148
+ const nextSibling = _ . get ( currentNode , 'nextSibling' ) ;
149
+ if ( ! nextSibling || _ . get ( nextSibling , 'name' ) === 'h2' ) {
150
+ break ;
151
+ }
152
+
153
+ currentNode = _ . get ( currentNode , 'nextSibling' ) ;
154
+
155
+ // exsit multiple example
156
+ if ( currentNode . type === 'tag' && currentNode . name === 'h3' ) {
157
+ currentNode = _ . get ( currentNode , 'nextSibling' ) ;
158
+ continue ;
159
+ }
160
+
161
+ // only extract the first one
162
+ if ( currentNode . type === 'tag' && currentNode . name === 'pre' ) {
163
+ content = $ ( currentNode ) . text ( ) ;
164
+ break ;
165
+ }
166
+ }
167
+
168
+ return content ;
169
+ }
170
+
171
+ function extractExamples ( expNodes : SnippetNodes , $ : cheerio . Root ) : string {
172
+ let nodes = expNodes . snippetNodes ;
173
+
174
+ // const snippetText = nodes.map((nn) => $(nn).text()).join('');
175
+ const textArray = _ . map ( nodes , node => {
176
+ return $ ( node ) . text ( ) ;
177
+ } ) ;
178
+
179
+ console . debug ( "[DEBUG]#### len:[%d], textArray:[%v]" , textArray . length , textArray ) ;
180
+
181
+ const snippetTexts = textArray . join ( "" ) ;
182
+ console . debug ( "[DEBUG]#### snippetTexts:[%s]" , snippetTexts ) ;
183
+
184
+ return snippetTexts ;
185
+ }
186
+
187
+ function extractArguments ( argNodes : ArgumentNodes , $ : cheerio . Root ) : variable [ ] {
154
188
let nodes = argNodes . argumentNodes ;
155
189
156
190
// Find the first ul
157
- var firstUl = _ . find ( nodes , ( o : any ) => o . name === "ul" ) ;
191
+ let firstUl = _ . find ( nodes , ( o : any ) => o . name === "ul" ) ;
158
192
159
193
if ( ! firstUl ) {
160
194
// throw "Didn't find a UL when searching through arguments";
@@ -165,7 +199,7 @@ function extractArguments(argNodes: ArgumentNodes, $: cheerio.Root): Variable[]
165
199
let text = $ ( li ) . text ( ) ;
166
200
let regex = / ( [ a - z A - Z 0 - 9 _ ] + ) ( .+ ) / ;
167
201
let result = text . match ( regex ) ;
168
- var name , description ;
202
+ let name , description ;
169
203
if ( ! result ) {
170
204
name = text ;
171
205
//console.error(`Didn't find a description for ${text} on ${extractResourceName($)}`);
@@ -178,14 +212,14 @@ function extractArguments(argNodes: ArgumentNodes, $: cheerio.Root): Variable[]
178
212
} ) ;
179
213
}
180
214
181
- function extractAttributes ( argNodes : AttributeNodes , $ : cheerio . Root ) : Variable [ ] {
215
+ function extractAttributes ( argNodes : AttributeNodes , $ : cheerio . Root ) : variable [ ] {
182
216
if ( argNodes . attributeNodes . length === 0 ) {
183
217
return [ ] ;
184
218
}
185
219
186
220
let nodes = argNodes . attributeNodes ;
187
221
// Find the first ul
188
- var firstUl = _ . find ( nodes , ( o : any ) => o . name === "ul" ) ;
222
+ let firstUl = _ . find ( nodes , ( o : any ) => o . name === "ul" ) ;
189
223
if ( ! firstUl ) {
190
224
// console.error(`Didn't find a UL when searching through attributes on ${extractResourceName($)}`);
191
225
return [ ] ;
@@ -194,7 +228,7 @@ function extractAttributes(argNodes: AttributeNodes, $: cheerio.Root): Variable[
194
228
let text = $ ( li ) . text ( ) ;
195
229
let regex = / ( [ a - z A - Z 0 - 9 _ ] + ) ( .+ ) / ;
196
230
let result = text . match ( regex ) ;
197
- var name , description ;
231
+ let name , description ;
198
232
if ( ! result ) {
199
233
name = text ;
200
234
// console.error(`Didn't find a description for ${text} on ${extractResourceName($)}`);
@@ -272,23 +306,52 @@ function extractResourceUrl(html: string): Map<string, string> {
272
306
const files = fs . readdirSync ( resourcesPath ) ;
273
307
const indexHtml = fs . readFileSync ( indexPath , "utf-8" ) ;
274
308
Promise . all ( getAllParsed ( files ) ) . then ( $s => {
275
- const resIndexMap = extractResourceUrl ( indexHtml ) ;
276
- const resources : Resource [ ] = _ . map ( $s , $ => {
277
- const resName = extractResourceName ( $ ) ;
278
- return {
279
- name : resName ,
280
- args : extractArguments ( extractArgumentsContent ( $ ) , $ ) ,
281
- attrs : extractAttributes ( extractAttributesContent ( $ ) , $ ) ,
282
- url : resIndexMap . get ( resName )
283
- } ;
284
- } ) ;
285
- let transformed = _ . transform ( resources , ( result , value , key ) => {
286
- result [ value . name ] = {
287
- args : value . args ,
288
- attrs : value . attrs ,
289
- url : value . url
290
- } ;
291
- } , { } ) ;
309
+ const args = process . argv . slice ( 2 ) ;
310
+ let type = "" ;
311
+ if ( args . length > 0 ) {
312
+ type = args [ 0 ] ;
313
+ }
314
+
315
+ let transformed : any ;
316
+ if ( type === "example" ) {
317
+ // example collection
318
+ const examples : Snippet [ ] = _ . map ( $s , $ => {
319
+ const resName = extractResourceName ( $ ) ;
320
+ return {
321
+ name : resName ,
322
+ example : extractExampleContent ( $ ) ,
323
+ } ;
324
+ } ) ;
325
+ transformed = _ . transform ( examples , ( result , value ) => {
326
+ result [ value . name ] = {
327
+ example : value . example
328
+ } ;
329
+ } , { } ) ;
330
+
331
+ } else {
332
+ // resource collection
333
+ const resIndexMap = extractResourceUrl ( indexHtml ) ;
334
+ const resources : Resource [ ] = _ . map ( $s , $ => {
335
+ const resName = extractResourceName ( $ ) ;
336
+ return {
337
+ name : resName ,
338
+ args : extractArguments ( extractArgumentsContent ( $ ) , $ ) ,
339
+ attrs : extractAttributes ( extractAttributesContent ( $ ) , $ ) ,
340
+ url : resIndexMap . get ( resName )
341
+ } ;
342
+ } ) ;
343
+ transformed = _ . transform ( resources , ( result , value , key ) => {
344
+ result [ value . name ] = {
345
+ args : value . args ,
346
+ attrs : value . attrs ,
347
+ url : value . url
348
+ } ;
349
+ } , { } ) ;
350
+ }
292
351
293
352
console . log ( JSON . stringify ( transformed ) ) ;
294
- } ) ;
353
+ } ) ;
354
+
355
+ function fun ( v : any , i : any , array : any ) : ( value : string , index : number , array : string [ ] ) => void {
356
+ throw new Error ( "Function not implemented." ) ;
357
+ }
0 commit comments