This repository has been archived by the owner on Nov 9, 2023. It is now read-only.
forked from ucsd-cse15l-w22/markdown-parse
-
Notifications
You must be signed in to change notification settings - Fork 1
/
MarkdownParse.java
91 lines (86 loc) · 3.34 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
// File reading code from https://howtodoinjava.com/java/io/java-read-file-to-string-examples/
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MarkdownParse {
static int findCloseParen(String markdown, int openParen) {
int closeParen = openParen + 1;
int openParenCount = 1;
while (openParenCount > 0 && closeParen < markdown.length()) {
if (markdown.charAt(closeParen) == '(') {
openParenCount++;
} else if (markdown.charAt(closeParen) == ')') {
openParenCount--;
}
closeParen++;
}
if(openParenCount == 0) {
return closeParen - 1;
}
else {
return -1;
}
}
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) {
ArrayList<String> toReturn = new ArrayList<>();
// find the next [, then find the ], then find the (, then take up to
// the next )
int currentIndex = 0;
while(currentIndex < markdown.length()) {
int nextOpenBracket = markdown.indexOf("[", currentIndex);
int nextCodeBlock = markdown.indexOf("\n```");
if(nextCodeBlock < nextOpenBracket && nextCodeBlock != -1) {
int endOfCodeBlock = markdown.indexOf("\n```");
currentIndex = endOfCodeBlock + 1;
continue;
}
int nextCloseBracket = markdown.indexOf("]", nextOpenBracket);
int openParen = markdown.indexOf("(", nextCloseBracket);
// The close paren we need may not be the next one in the file
int closeParen = findCloseParen(markdown, openParen);
if(nextOpenBracket == -1 || nextCloseBracket == -1
|| closeParen == -1 || openParen == -1) {
return toReturn;
}
String potentialLink = markdown.substring(openParen + 1, closeParen).trim();
if(potentialLink.indexOf(" ") == -1 && potentialLink.indexOf("\n") == -1) {
toReturn.add(potentialLink);
currentIndex = closeParen + 1;
}
else {
currentIndex = currentIndex + 1;
}
}
return toReturn;
}
public static void main(String[] args) throws IOException {
Path fileName = Path.of(args[0]);
String contents = Files.readString(fileName);
ArrayList<String> links = getLinks(contents);
System.out.println(links);
}
}