forked from ucsd-cse15l-w22/markdown-parse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Markdown.java
28 lines (27 loc) · 1.09 KB
/
Markdown.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
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
public class Markdown {
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 nextCloseBracket = markdown.indexOf("]", nextOpenBracket);
int openParen = markdown.indexOf("(", currentIndex);
int closeParen = markdown.indexOf(")", openParen);
toReturn.add(markdown.substring(openParen + 1, closeParen));
currentIndex = closeParen + 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);
}
}