diff --git a/MarkdownParse.java b/MarkdownParse.java index 5ebf83aba..dc20d0fad 100644 --- a/MarkdownParse.java +++ b/MarkdownParse.java @@ -1,29 +1,96 @@ +// //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.Scanner; + +// public class MarkdownParse { + +// public static ArrayList getLinks(String markdown) { +// ArrayList toReturn = new ArrayList<>(); +// // find the next [, then find the ], then find the (, then read link upto next ) +// int currentIndex = 0; +// Scanner scnr = new Scanner(markdown); + +// while(scnr.hasNextLine()) { +// int openBracket = markdown.indexOf("[", currentIndex); +// int closeBracket = markdown.indexOf("]", openBracket); +// int openParen = markdown.indexOf("(", closeBracket); +// int closeParen = markdown.indexOf(")", openParen); + +// if (openBracket < 0 || closeBracket < 0 || openParen < 0 || closeParen < 0) { +// break; +// } + +// toReturn.add(markdown.substring(openParen - 1, closeParen)); +// currentIndex = closeParen + 1; +// scnr.nextLine(); +// } +// scnr.close(); + +// return toReturn; +// } //https://howtodoinjava.com/java/io/java-read-file-to-string-examples/ +//https://github.com/natsukiromero/markdown-parser/blob/main/MarkdownParse.java import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.ArrayList; +import java.util.Scanner; public class MarkdownParse { public static ArrayList getLinks(String markdown) { ArrayList toReturn = new ArrayList<>(); - // find the next [, then find the ], then find the (, then read link upto next ) + Scanner scnr = new Scanner(markdown); + // find the next [, then find the ], then find the (, then read link up + // to next ) int currentIndex = 0; - while(currentIndex < markdown.length()) { + while(scnr.hasNextLine()) { int openBracket = markdown.indexOf("[", currentIndex); int closeBracket = markdown.indexOf("]", openBracket); int openParen = markdown.indexOf("(", closeBracket); int closeParen = markdown.indexOf(")", openParen); - toReturn.add(markdown.substring(openParen + 1, closeParen)); + + // to handle case where one of them loops over the file again + if (openBracket < 0 || closeBracket < 0 || openParen < 0 || + closeParen < 0) { + break; + } + //check to make sure link is not an image + Boolean isImage = false; + if(openBracket != 0) { + String type = markdown.substring(openBracket - 1, openBracket); + isImage = type.equals("!"); + } + + //check that link follows format []() + int format = openParen - closeBracket; + Boolean linkFollowsFormat = true; + if(format != 1) { + linkFollowsFormat = false; + } + + // check that link is a valid link + String link = markdown.substring(openParen + 1, closeParen); + Boolean linkHasSpace = link.contains(" "); + Boolean linkHasBrackets = link.contains("["); + if(isImage == false && linkHasSpace == false && linkFollowsFormat == + true && !(link.isEmpty()) && linkHasBrackets == false) { + + toReturn.add(link); + } + currentIndex = closeParen + 1; + scnr.nextLine(); } - + scnr.close(); return toReturn; } - public static void main(String[] args) throws IOException { Path fileName = Path.of(args[0]); String content = Files.readString(fileName); @@ -31,3 +98,11 @@ public static void main(String[] args) throws IOException { System.out.println(links); } } + +// public static void main(String[] args) throws IOException { +// Path fileName = Path.of(args[0]); +// String content = Files.readString(fileName); +// ArrayList links = getLinks(content); +// System.out.println(links); +// } +// } diff --git a/MarkdownParseTest.java b/MarkdownParseTest.java new file mode 100644 index 000000000..a4a3d5d77 --- /dev/null +++ b/MarkdownParseTest.java @@ -0,0 +1,81 @@ +import static org.junit.Assert.*; +import org.junit.*; +import java.nio.file.Files; +import java.nio.file.Path; +import java.io.IOException; +import java.util.ArrayList; + + +public class MarkdownParseTest { + + @Test + public void addition(){ + assertEquals(2,1+1); + } + + @Test + public void testgetLinks() throws IOException { + ArrayList toReturn = new ArrayList<>(); + toReturn.add("https://something.com"); + toReturn.add("some-thing.html"); + Path fileName = Path.of("test-file.md"); + String content = Files.readString(fileName); + assertEquals(toReturn, MarkdownParse.getLinks(content)); + } + @Test + public void test1getLinks() throws IOException { + ArrayList toReturn = new ArrayList<>(); + toReturn.add("somethingtem t.com"); + Path fileName = Path.of("test-file1.md"); + String content = Files.readString(fileName); + assertEquals(toReturn, MarkdownParse.getLinks(content)); + } + @Test + public void test2getLinks() throws IOException { + ArrayList toReturn = new ArrayList<>(); + toReturn.add("somethingting.com"); + Path fileName = Path.of("test-file2.md"); + String content = Files.readString(fileName); + assertEquals(toReturn, MarkdownParse.getLinks(content)); + } + @Test + public void test3getLinks() throws IOException { + ArrayList toReturn = new ArrayList<>(); + toReturn.add(" "); + Path fileName = Path.of("test-file3.md"); + String content = Files.readString(fileName); + assertEquals(toReturn, MarkdownParse.getLinks(content)); + } + @Test + public void testSnippet1() throws IOException{ + ArrayList expected = new ArrayList<>(); + expected.add("'google.com"); + MarkdownParse snippet1 = new MarkdownParse(); + Path fileName = Path.of("snippet1.md"); + String content = Files.readString(fileName); + ArrayList actual = snippet1.getLinks(fileName); + assertEquals(expected,actual); + } + @Test + public void testSnippet2() throws IOException{ + ArrayList expected = new ArrayList<>(); + expected.add("a.com"); + expected.add("a,com(())"); + expected.add("example.com"); + MarkdownParse snippet2 = new MarkdownParse(); + Path fileName = Path.of("snippet2.md"); + String content = Files.readString(fileName); + ArrayList actual = snippet2.getLinks(fileName); + assertEquals(expected,actual); + } + @Test + public void testSnippet3() throws IOException{ + ArrayList expected = new ArrayList<>(); + expected.add("https://sites.google.com/eng.ucsd.edu/cse-15l-spring-2022/schedule"); + MarkdownParse snippet3 = new MarkdownParse(); + Path fileName = Path.of("snippet3.md"); + String content = Files.readString(fileName); + ArrayList actual = snippet3.getLinks(fileName); + assertEquals(expected,actual); + } +} diff --git a/images/java_bug.png b/images/java_bug.png new file mode 100644 index 000000000..7ee775f8f Binary files /dev/null and b/images/java_bug.png differ diff --git a/snippet1.md b/snippet1.md new file mode 100644 index 000000000..1019d16bb --- /dev/null +++ b/snippet1.md @@ -0,0 +1,7 @@ +`[a link`](url.com) + +[another link](`google.com)` + +[`cod[e`](google.com) + +[`code]`](ucsd.edu) \ No newline at end of file diff --git a/snippet2.md b/snippet2.md new file mode 100644 index 000000000..3fb8eb27e --- /dev/null +++ b/snippet2.md @@ -0,0 +1,5 @@ +[a [nested link](a.com)](b.com) + +[a nested parenthesized url](a.com(())) + +[some escaped \[ brackets \]](example.com) \ No newline at end of file diff --git a/snippet3.md b/snippet3.md new file mode 100644 index 000000000..940b2cab9 --- /dev/null +++ b/snippet3.md @@ -0,0 +1,24 @@ +[this title text is really long and takes up more than +one line + +and has some line breaks]( + https://www.twitter.com +) + +[this title text is really long and takes up more than +one line]( +https://sites.google.com/eng.ucsd.edu/cse-15l-spring-2022/schedule +) + + +[this link doesn't have a closing parenthesis](github.com + +And there's still some more text after that. + +[this link doesn't have a closing parenthesis for a while](https://cse.ucsd.edu/ + + + +) + +And then there's more text \ No newline at end of file diff --git a/test-file.md b/test-file.md index 0f9fbc913..40760ea0d 100644 --- a/test-file.md +++ b/test-file.md @@ -1,4 +1,4 @@ # Title [link1](https://something.com) -[link2](some-thing.html) +[link2](some-thing.html) \ No newline at end of file diff --git a/test-file1.md b/test-file1.md new file mode 100644 index 000000000..f8f746343 --- /dev/null +++ b/test-file1.md @@ -0,0 +1 @@ +[Link](somethingtem t.com) diff --git a/test-file2.md b/test-file2.md new file mode 100644 index 000000000..e088392df --- /dev/null +++ b/test-file2.md @@ -0,0 +1 @@ +[Link](somethingting.com) \ No newline at end of file diff --git a/test-file3.md b/test-file3.md new file mode 100644 index 000000000..8b4ec7fe5 --- /dev/null +++ b/test-file3.md @@ -0,0 +1 @@ +[Link]( ) \ No newline at end of file diff --git a/test-file4.md b/test-file4.md new file mode 100644 index 000000000..083728b57 --- /dev/null +++ b/test-file4.md @@ -0,0 +1,3 @@ +# title + +[]link goes here! diff --git a/test-file5.md b/test-file5.md new file mode 100644 index 000000000..03d95aa8f --- /dev/null +++ b/test-file5.md @@ -0,0 +1,7 @@ +# title + +[stuff] + +paragraph + +(page.com) \ No newline at end of file diff --git a/test-file6.md b/test-file6.md new file mode 100644 index 000000000..18e1f6ff3 --- /dev/null +++ b/test-file6.md @@ -0,0 +1,3 @@ +# title + +![link](page.com) \ No newline at end of file diff --git a/test-file7.md b/test-file7.md new file mode 100644 index 000000000..23f9c68a7 --- /dev/null +++ b/test-file7.md @@ -0,0 +1 @@ +)[ \ No newline at end of file diff --git a/test-file8.md b/test-file8.md new file mode 100644 index 000000000..8ed07127a --- /dev/null +++ b/test-file8.md @@ -0,0 +1,2 @@ +[](a link on the first line) +[ \ No newline at end of file