forked from ucsd-cse15l-w22/markdown-parse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMarkdownParse.java
More file actions
87 lines (72 loc) · 3.13 KB
/
MarkdownParse.java
File metadata and controls
87 lines (72 loc) · 3.13 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
// 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.nio.file.Path;
import java.util.ArrayList;
import java.util.Stack;
import java.util.HashMap;
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 take up to
// the next )
// verify that the open-close pattern is appropriate
//Test2-file.md
//We are correctly reading opening bracket
//We then read the closing bracket but that takes us to the very end
//Then we look for an open paren but there are no more open paren
//This results in an infinite loop (which is the error we are gett)
Stack<String> parenParity = new Stack<>();
//if we find an open bracket/parenthesis push into stack
//if we find a closing bracket/parenthesis and the top of the stack is
/*
.apng
.avif
.gif
.jpg, .jpeg, .jfif, .pjpeg, .pjp
.png
.svg
.webp
*/
/*
String myStr = "Hello";
System.out.println(myStr.contains("Hel")); // true
System.out.println(myStr.contains("e")); // true
System.out.println(myStr.contains("Hi")); // false
*/
String[] imageExtensions = { ".jpg", ".png", ".jpeg", ".raw", ".jfif", ".pjpeg", ".pjp", ".avif", ".gif", ".svg", ".webp" };
int currentIndex = 0;
int nextOpenBracket = 0;
int nextCloseBracket = markdown.indexOf("]");
int openParen = markdown.indexOf("(");
int closeParen = 0;
while(currentIndex < markdown.length()) {
if (nextCloseBracket > openParen) break;
nextOpenBracket = markdown.indexOf("[", currentIndex);
nextCloseBracket = markdown.indexOf("]", nextOpenBracket);
openParen = markdown.indexOf("(", nextCloseBracket);
closeParen = markdown.indexOf(")", openParen);
if (nextOpenBracket == -1 || nextCloseBracket == -1 || openParen == -1 || closeParen == -1) break;
if(!markdown.substring(openParen+1, closeParen).contains(".")) break;
//run for loop and .contains on the substring?
boolean check = false;
for ( String s : imageExtensions ){
if (markdown.substring(openParen+1, closeParen).contains(s)){
check = true;
break;
}
}
if (check == false && (nextCloseBracket == openParen -1)) {
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);
}
}