forked from test-org-618/quick-labs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuideConverter.java
555 lines (500 loc) · 23 KB
/
GuideConverter.java
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import javax.net.ssl.HttpsURLConnection;
import java.util.Scanner;
public class GuideConverter{
//asks user for the guide name
public static void main(String[] args) throws Exception {
String guideName = args[0];
getMD(guideName);
System.out.println("Guide converted");
System.out.println("Find markdown in instructions/"+guideName+"/"+guideName+".md");
}
// inserts gitclone.aoc from https://github.com/OpenLiberty/guides-common
public static void clone(String guideName){
try{
String addition = "\n# Getting Started\n\nIf a terminal window does not open navigate:\n\n> Terminal -> New Terminal\n\nCheck you are in the **home/project** folder:\n\n```\npwd\n```\n{: codeblock}\n\nThe fastest way to work through this guide is to clone the Git repository and use the projects that are provided inside:\n\n```\ngit clone https://github.com/openliberty/"+guideName+".git\ncd "+ guideName + "\n```\n{: codeblock}\n\nThe **start** directory contains the starting project that you will build upon.\n";
writeToFile(addition,guideName);
}
catch(IOException ex){
System.out.println(ex);
}
}
//inserts ol-kernel-docker-pull.adoc from https://github.com/OpenLiberty/guides-common
public static void kernelPull(String guideName){
try{
String addition = "\nRun the following command to download or update to the latest **openliberty/open-liberty:kernel-java8-openj9-ubi** Docker image:\n\n```\ndocker pull openliberty/open-liberty:kernel-java8-openj9-ubi\n```\n{: codeblock}\n\n";
writeToFile(addition,guideName);
}
catch(IOException ex){
System.out.println(ex);
}
}
//inserts devmode-start.adoc from https://github.com/OpenLiberty/guides-common
public static void devMode(String guideName){
try{
String dev ="When you run Open Liberty in dev mode, the server listens for file changes and automatically recompiles and deploys your updates whenever you save a new change. Run the following goal to start in dev mode:\n\n```\nmvn liberty:dev\n```\n{: codeblock}\n\nAfter you see the following message, your application server in dev mode is ready:\n\n```\nPress the Enter key to run tests on demand.\n```\n\nDev mode holds your command line to listen for file changes. Open another command line to continue, or open the project in your editor.\n";
writeToFile(dev, guideName);
}catch(IOException ex){
System.out.println(ex);
}
}
//inserts twyb-end.adoc from https://github.com/OpenLiberty/guides-common
public static void end(String guideName){
try{
String dev ="After you are finished checking out the application, stop the Open Liberty server by pressing `CTRL+C` in the shell session where you ran the server. Alternatively, you can run the `liberty:stop` goal from the `finish` directory in another shell session:\n\n```\nmvn liberty:stop\n```\n{: codeblock}";
writeToFile(dev, guideName);
}catch(IOException ex){
System.out.println(ex);
}
}
//inserts trywhatyoubuild-intro.adoc from https://github.com/OpenLiberty/guides-common
public static void tryBuildIntro(String guideName){
try{
String dev ="## Try what you’ll build\nThe **finish** directory in the root of this guide contains the finished application. Give it a try before you proceed.\n\nTo try out the application, first navigate to the finish directory and then run the following Maven goal to build the application and run it inside Open Liberty:\n```\ncd finish\nmvn install liberty:start-server\n```\n{: codeblock}\n\n";
writeToFile(dev, guideName);
}catch(IOException ex){
System.out.println(ex);
}
}
//inserts trywhatyoubuild-intro.adoc from https://github.com/OpenLiberty/guides-common
public static void tryBuildEnd(String guideName){
try{
String dev ="After you are done checking out the application, stop the Open Liberty server:\n\n```\nmvn liberty:stop-server\n```\n{: codeblock}";
writeToFile(dev, guideName);
}catch(IOException ex){
System.out.println(ex);
}
}
//inserts devmode-quit from https://github.com/OpenLiberty/guides-common
public static void devEnd(String guideName){
try{
String end ="When you are done checking out the service, exit development mode by pressing `CTRL+C` in the shell session where you ran the server, or by typing `q` and then pressing the `enter/return` key.";
writeToFile(end, guideName);
}catch(IOException ex){
System.out.println(ex);
}
}
//creates command for navigation
public static void start(String guideName){
try{
String startNav = "Navigate to the **start** directory to begin.\n\n```\ncd start\n```\n{: codeblock}\n\n";
writeToFile(startNav, guideName);
}
catch(IOException ex){
System.out.println(ex);
}
}
public static void removeLast(String guideName){
try{
java.io.RandomAccessFile file = new java.io.RandomAccessFile("instructions/"+guideName+"/"+guideName+".md", "rw");
byte b = 0;
long pos = file.length();
while (b != '\n' && --pos >= 0) {
file.seek(pos);
b = file.readByte();
}
file.seek(++pos);
file.setLength(pos);
file.write("".getBytes());
}
catch(IOException ex){
System.out.println(ex);
}
}
//configures instructions to replace file
public static String replace(String inputLine, String guideName){
try{
inputLine = inputLine.replaceAll("#","");
inputLine = inputLine.replaceAll("`","**");
writeToFile("\n> [File -> Open]"+guideName + "/start/" + inputLine.replaceAll("\\*\\*","") +"\n", guideName);
writeToFile("\n",guideName);
writeToFile("```",guideName);
codeSnippet(inputLine.replaceAll("\\*\\*",""), guideName);
String position = "main";
return position;
}
catch(IOException ex){
System.out.println(ex);
return "";
}
}
//configures instructions to udate file
public static String update(String inputLine, String guideName){
try{
inputLine = inputLine.replaceAll("#","");
inputLine = inputLine.replaceAll("`","**");
writeToFile("\n> [File -> Open]"+guideName + "/start/" + inputLine.replaceAll("\\*\\*","") +"\n", guideName);
writeToFile("\n",guideName);
writeToFile("```",guideName);
codeSnippet(inputLine.replaceAll("\\*\\*",""), guideName);
String position = "main";
return position;
}
catch(IOException ex){
System.out.println(ex);
return "";
}
}
//configures instructions to create file
public static String touch(String inputLine, String guideName){
try{
writeToFile("```",guideName);
inputLine = "touch " + inputLine;
inputLine = inputLine.replaceAll("`","");
writeToFile(inputLine,guideName);
writeToFile("```",guideName);
writeToFile("{: codeblock}\n\n",guideName);
writeToFile("> [File -> Open]"+ guideName + "/start/" + inputLine.replaceAll("touch ","") + "\n```\n", guideName);
codeSnippet(inputLine.replaceAll("touch ",""), guideName);
String position = "main";
return position;
}catch(IOException ex){
System.out.println(ex);
return "";
}
}
//configures link
public static void link(String inputLine, String guideName){
try{
System.out.println(inputLine);
String linkParts[] = new String[2];
String findLink[];
String link;
String description;
String formattedLink;
String localhostSplit[];
String findDescription[];
inputLine = inputLine.replaceAll("\\{","");
inputLine = inputLine.replaceAll("\\}","");
linkParts = inputLine.split("\\[");
findDescription = linkParts[1].split("\\^");
description = findDescription[0];
findLink = linkParts[0].split(" ");
link = findLink[findLink.length-1];
if(link.contains("localhost")){
inputLine = inputLine.replaceAll(link+"\\["+description+"\\^\\]", ("\n```\ncurl "+link+"\n```\n{: codeblock}\n\n") );
}
formattedLink = "[" + description + "](" + link + ")";
inputLine = inputLine.replaceAll(link+"\\["+description+"\\^\\]",formattedLink);
writeToFile(inputLine, guideName);
}
catch(IOException ex){
System.out.println(ex);
}
}
// general text configuration
public static void main(String inputLine, String guideName){
try{
if(!inputLine.equals("[.hidden]")){
if(!inputLine.equals("irrelevant")){
if(inputLine.equals("----")){
writeToFile("```",guideName);
}
else{
inputLine = inputLine.replaceAll("`", "**");
inputLine = inputLine.replaceAll("â””â","|");
inputLine = inputLine.replaceAll("”€â”€","__");
inputLine = inputLine.replaceAll("Ã"," |");
inputLine = inputLine.replaceAll("│","");
inputLine = inputLine.replaceAll("’","`");
inputLine = inputLine.replaceAll("\\[hotspot(.*?)\\]","");
if(inputLine.equals("******")){
inputLine="```";
}
if(inputLine.startsWith("== ")){
if(!inputLine.equals("== What you'll learn")){
inputLine = inputLine.replaceAll("==","#");
}
}
if(inputLine.startsWith("==")){
inputLine = inputLine.replaceAll("=","#");
}
writeToFile(inputLine,guideName);
}
}
}
}catch(IOException ex){
System.out.println(ex);
}
}
//inserts code snippet
public static void codeSnippet(String path, String guideName){
try{
String httpsURL = "https://raw.githubusercontent.com/openliberty/"+guideName+"/master/finish/"+path;
String FILENAME = "temp.adoc";
BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME));
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
con.setRequestProperty ( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0" );
InputStream ins = con.getInputStream();
InputStreamReader isr = new InputStreamReader(ins, "Windows-1252");
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null) {
if(!inputLine.replaceAll(" ","").startsWith("/")){
if(!inputLine.startsWith("*")){
if(!inputLine.startsWith(" *")){
if(!inputLine.startsWith("#")){
writeToFile(inputLine,guideName);
}
}
}
}
}
writeToFile("```\n{: codeblock}\n\n",guideName);
}catch(IOException ex){
System.out.println(ex);
}
}
//configures table UNFINISHED
public static String table(String inputLine, String guideName){
try{
if(inputLine.equals("|===")){
return "main";
}
if(inputLine.startsWith(" ")){
inputLine = inputLine.replaceAll(" ","");
writeToFile(inputLine,guideName);
return "main";
}
if(inputLine.startsWith("| *")){
int count = 0;
for(int i =0; i<inputLine.length();i++){
if(inputLine.charAt(i) == '*')
{
count += 1;
}
}
writeToFile(inputLine, guideName);
String split = "";
for(int j = 0; j < count/2;j++){
split = split + "|---";
}
writeToFile(split,guideName);
return "table";
}
writeToFile(inputLine,guideName);
return "table";
}
catch(IOException ex){
return "";
}
}
public static void getMD(String guideName){
try{
//read adoc file from the open liberty guide
String httpsURL = "https://raw.githubusercontent.com/openliberty/"+guideName+"/master/README.adoc";
String FILENAME = "temp.adoc";
BufferedWriter bw = new BufferedWriter(new FileWriter(FILENAME));
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection) myurl.openConnection();
con.setRequestProperty ( "User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:63.0) Gecko/20100101 Firefox/63.0" );
InputStream ins = con.getInputStream();
InputStreamReader isr = new InputStreamReader(ins, "Windows-1252");
BufferedReader in = new BufferedReader(isr);
//initialise variables
String position = "";
String inputLine;
int positionNumber = 0;
//stores the start of irrelevant lines
String[] startingPhrases = {"//",":","[source","NOTE:","include::","[role=","[.tab_","image::"};
//write each line into the file
while ((inputLine = in.readLine()) != null) {
//configure table
if(position.equals("table")){
position = table(inputLine, guideName);
continue;
}
//Skip instructions for windows command
if(position.equals("windows")){
if(positionNumber == 5){
position = "main";
continue;
}
else{
positionNumber += 1;
continue;
}
}
//Current line is an example output of a mvn test
if(position.equals("testBlock")){
if(!inputLine.startsWith("[INFO]")){
writeToFile("```\n",guideName);
position = "main";
}
else{
writeToFile(inputLine,guideName);
}
continue;
}
//Identifies an instruction for windows only and skips the current line
if(inputLine.equals("[.tab_content.windows_section]")){
position = "windows";
positionNumber = 0;
continue;
}
//Identifies that line is the start of an example output of a mvn test
if(inputLine.startsWith("[INFO]")){
position = "testBlock";
writeToFile("```\n"+inputLine,guideName);
continue;
}
//Identifies that line is the start of a code block
if(inputLine.equals("```")){
position = "code";
continue;
}
//Identifies that line is the start of a table
if(inputLine.equals("|===")){
position = "table";
continue;
}
//Skips over lines with ----
if(inputLine.equals("----")){
continue;
}
//Line is part of a code block
if(position.equals("code")){
if(inputLine.equals("----")){
writeToFile("```",guideName);
continue;
}
//Code is a mvn instruction
else if(inputLine.startsWith("mvn")){
//Adds copy button
writeToFile("```\n"+inputLine+"\n```\n{: codeblock}\n\n",guideName);
continue;
}
//Identifies end of code block so carries out formatting
else{
position = "main";
}
}
//Finds title so we skip over irrelevant lines
if(inputLine.startsWith("= ")){
writeToFile(inputLine.replaceAll("=","#"), guideName);
position = "intro";
}
//Identifies another heading after the intro so we stop skipping over lines
if(inputLine.startsWith("== ")){
position = "main";
}
if(inputLine.startsWith("[source")){
removeLast(guideName);
continue;
}
//User is instructed to replace a file
if(inputLine.startsWith("#Replace")){
writeToFile(inputLine.replaceAll("#",""),guideName);
position = "replacePath"; //next lines need configuring so position is changed
continue;
}
//User is instructed to create a file
if(inputLine.startsWith("#Create")){
writeToFile(inputLine.replaceAll("#",""),guideName);
position = "new"; //next lines need configuring so position is changed
continue;
}
//User is instructed to update a file
if(inputLine.startsWith("#Update")){
writeToFile(inputLine.replaceAll("#",""),guideName);
position = "update"; //next lines need configuring so position is changed
continue;
}
//Skips line, resets positionNumber and configures the replacement instructions
if(position.equals("replacePath")){
positionNumber = 0;
position = replace(inputLine, guideName);
continue;
}
//Skips line, resets positionNumber and configures the instructions for a new file
if(position.equals("new")){
positionNumber = 0;
position = touch(inputLine, guideName);
continue;
}
//Skips line, resets positionNumber and configures the instructions for a file update
if(position.equals("update")){
positionNumber = 0;
position = update(inputLine, guideName);
continue;
}
//Identifies a link in the file line and configures it
if(inputLine.contains("^]")){
link(inputLine,guideName);
continue;
}
//identfies if instructions from github.com/OpenLiberty/guides-common need to be inserted
if(inputLine.equals("include::{common-includes}/ol-kernel-docker-pull.adoc[]")){
kernelPull(guideName);
}
if(inputLine.equals("include::{common-includes}/devmode-start.adoc[]")){
devMode(guideName);
}
if(inputLine.equals("include::{common-includes}/twyb-end.adoc[]")){
end(guideName);
}
if(inputLine.equals("include::{common-includes}/gitclone.adoc[]")){
clone(guideName);
}
if(inputLine.equals("include::{common-includes}/devmode-quit.adoc[]")){
devEnd(guideName);
}
if(inputLine.equals("include::{common-includes}/twyb-intro.adoc[]")){
tryBuildIntro(guideName);
}
if(inputLine.equals("include::{common-includes}/twyb-end.adoc[]")){
tryBuildEnd(guideName);
}
//Identifies the start of a table
if(inputLine.startsWith("[cols")){
position = "table";
continue;
}
//Identifies the start of a table
if(inputLine.equals("Navigate to the `start` directory to begin.")){
start(guideName);
continue;
}
//compares line with the irrelevant ones in startingPhrases
for(int i = 0; i < startingPhrases.length; i++){
if(inputLine.startsWith(startingPhrases[i])){
inputLine = "irrelevant";
break;
}
}
//skips line if it is irrelevant
if(inputLine.equals("irrelevant")){
continue;
}
//end of guidee
if(inputLine.startsWith("== Great work!")){
String finish = "# Summary\n\n## Clean up your environment\n\nDelete the **" + guideName + "** project by navigating to the **/home/project/** directory\n\n```\ncd ../..\nrm -r -f " + guideName + "\nrmdir " + guideName + "\n```\n{: codeblock}\n\n";
writeToFile(finish,guideName);
}
//inputLine contains info that needs general configuration and is not a special case
if(position.equals("main")){
main(inputLine, guideName);
}
}
in.close();
bw.close();
}
catch(IOException ex){
System.out.println(ex);
}
}
// append to md file
public static void writeToFile(String str, String guideName)
throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter("instructions/"+guideName+"/"+guideName+".md",true));
writer.append("\n"+str);
writer.close();
}
}