forked from nidhidhamnani/markdown-parser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MarkdownParse.java
128 lines (109 loc) · 4.75 KB
/
MarkdownParse.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
//https://howtodoinjava.com/java/io/java-read-file-to-string-examples/
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
public class MarkdownParse {
public static ArrayList<String> getLinks(String markdown) {
ArrayList<String> toReturn = new ArrayList<>();
// find the next [, then find the ], then find the (, then read link upto next )
int currentIndex = 0;
while(currentIndex < markdown.length()) {
int openBracket = markdown.indexOf("[", currentIndex);
if (isEscapeCharacter(markdown, openBracket)){
openBracket = markdown.indexOf("[", openBracket);
}
int closeBracket = markdown.indexOf("]", openBracket);
if (isEscapeCharacter(markdown, closeBracket)){
closeBracket = markdown.indexOf("]", closeBracket+1);
}
if(closeBracket+1 == markdown.length() && markdown.indexOf("(",currentIndex) != -1){
closeBracket = -1;
}
int openParen;
if (openBracket == -1 || closeBracket == -1){
openParen = markdown.indexOf("(",currentIndex);
}
else{
openParen = markdown.indexOf("(", closeBracket+1);
}
if (isEscapeCharacter(markdown, openParen)){
openParen = markdown.indexOf("(", openParen+1);
}
int closeParen = -1;
if (openParen != -1){
closeParen = markdown.indexOf(")", openParen);
}
if (isEscapeCharacter(markdown, closeParen)){
closeParen = markdown.indexOf(")", closeParen+1);
}
if (openParen + 1 <= closeParen){
//toReturn.add(markdown.substring(openParen + 1, closeParen));
if (beforNewLine(markdown, closeParen)){
int innercloseParen = getOuterParen(markdown, closeParen);
toReturn.add(trimSpaces(markdown,openParen,innercloseParen));
}
else{
toReturn.add(trimSpaces(markdown,openParen,closeParen));
}
if (isNestedLink(markdown, openBracket,closeBracket,closeParen)){
openParen = markdown.indexOf("(", closeParen);
closeParen = markdown.indexOf(")", openParen);
closeParen = getOuterParen(markdown, closeParen);
//toReturn.add(markdown.substring(openParen + 1, closeParen));
toReturn.add(trimSpaces(markdown,openParen,closeParen));
}
}
else{
break;
}
currentIndex = closeParen + 1;
}
return toReturn;
}
public static boolean isNestedLink(String md, int openBra, int closeBra,int closePar){
openBra = md.indexOf("[",openBra+1);
int newLine = md.indexOf("\n",closeBra);
System.out.println(newLine);
return openBra != -1 && openBra < closeBra && md.substring(closeBra,newLine).contains("]")
&& !isEscapeCharacter(md, openBra);
}
public static int getOuterParen(String md, int closeParen){
int newLine = md.indexOf("\n",closeParen);
/*System.out.println(closeParen+1);
System.out.println(newLine);*/
int outerCloseParen = closeParen;
System.out.println(md.substring(outerCloseParen+1, newLine).contains(")"));
while (md.substring(outerCloseParen+1, newLine).contains(")")){
outerCloseParen = md.indexOf(")", outerCloseParen+1);
//System.out.println(closeParen);
}
return outerCloseParen;
}
private static boolean isEscapeCharacter(String md, int index){
if (index - 1 > -1 && md.charAt(index-1) == '\\'){
//System.out.println("true");
return true;
}
return false;
}
public static String trimSpaces(String md, int openParen, int closeParen){
String toTrim = md.substring(openParen+1, closeParen);
return toTrim.trim();
}
public static boolean beforNewLine(String md, int closeParen){
int newLine = md.indexOf("\n",closeParen);
System.out.println("newline: "+ newLine);
System.out.println("closeParen: " + closeParen);
if (!md.substring(closeParen,newLine).contains("]")){
return true;
}
return false;
}
public static void main(String[] args) throws IOException {
Path fileName = Path.of(args[0]);
String content = Files.readString(fileName);
ArrayList<String> links = getLinks(content);
System.out.println(links);
}
}