forked from ucsd-cse15l-w22/markdown-parse
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMarkdownParse.java
More file actions
93 lines (92 loc) · 3.34 KB
/
Copy pathMarkdownParse.java
File metadata and controls
93 lines (92 loc) · 3.34 KB
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
// File reading code from https://howtodoinjava.com/java/io/java-read-file-to-string-examples/
import java.io.IOException;
import java.nio.file.Files;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.HashMap;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
public class MarkdownParse {
static int totalCount;
public static Map<String, List<String>> getLinks(File dirOrFile) throws IOException {
Map<String, List<String>> result = new HashMap<>();
if(dirOrFile.isDirectory()) {
for(File f: dirOrFile.listFiles()) {
result.putAll(getLinks(f));
}
return result;
}
else {
Path p = dirOrFile.toPath();
int lastDot = p.toString().lastIndexOf(".");
if(lastDot == -1 || !p.toString().substring(lastDot).equals(".md")) {
return result;
}
ArrayList<String> links = getLinks(Files.readString(p));
result.put(dirOrFile.getPath(), links);
return result;
}
}
public static ArrayList<String> getLinks(String markdown) {
if(markdown.contains("]") && markdown.contains("[") &&
markdown.contains(")") && markdown.contains("(")) {
MarkdownParse.totalCount += 1;
}
ArrayList<String> toReturn = new ArrayList<>();
// find the next [, then find the ], then find the (, then take up to
// the next )
ArrayList<Integer> periodList = new ArrayList<Integer>();
int currentIndex = 0;
while(currentIndex < markdown.length()) {
int nextPeriodIndex = markdown.indexOf(".", currentIndex + 1);
if(nextPeriodIndex != -1) {
periodList.add(nextPeriodIndex);
currentIndex = nextPeriodIndex;
} else {
break;
}
}
for(int periodIndex: periodList) {
int startIndex = periodIndex;
int endIndex = periodIndex;
ArrayList<Character> stopCharacters = new ArrayList<Character>();
stopCharacters.add('(');
stopCharacters.add(')');
stopCharacters.add('[');
stopCharacters.add(']');
stopCharacters.add('\n');
stopCharacters.add(' ');
while(startIndex >= 0) {
if(stopCharacters.contains(markdown.charAt(startIndex))) {
startIndex++;
break;
}
startIndex--;
}
while(endIndex < markdown.length()) {
if(stopCharacters.contains(markdown.charAt(endIndex))) {
endIndex--;
break;
}
endIndex++;
}
if(endIndex == markdown.length()) {
endIndex--;
}
if(startIndex < 0) {
startIndex = 0;
}
toReturn.add(markdown.substring(startIndex, endIndex + 1));
}
return toReturn;
}
public static void main(String[] args) throws IOException {
Path fileName = Path.of(args[0]);
File file = new File(fileName.toString());
MarkdownParse.totalCount = 0;
Map<String, List<String>> links = getLinks(file);
System.out.println(links);
}
}