Skip to content
Open

pull #27

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 80 additions & 5 deletions MarkdownParse.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,108 @@
// //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<String> getLinks(String markdown) {
// ArrayList<String> 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<String> getLinks(String markdown) {
ArrayList<String> 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);
ArrayList<String> links = getLinks(content);
System.out.println(links);
}
}

// public static void main(String[] args) throws IOException {
// Path fileName = Path.of(args[0]);
// String content = Files.readString(fileName);
// ArrayList<String> links = getLinks(content);
// System.out.println(links);
// }
// }
81 changes: 81 additions & 0 deletions MarkdownParseTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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<String> 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<String> expected = new ArrayList<>();
expected.add("'google.com");
MarkdownParse snippet1 = new MarkdownParse();
Path fileName = Path.of("snippet1.md");
String content = Files.readString(fileName);
ArrayList<String> actual = snippet1.getLinks(fileName);
assertEquals(expected,actual);
}
@Test
public void testSnippet2() throws IOException{
ArrayList<String> 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<String> actual = snippet2.getLinks(fileName);
assertEquals(expected,actual);
}
@Test
public void testSnippet3() throws IOException{
ArrayList<String> 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<String> actual = snippet3.getLinks(fileName);
assertEquals(expected,actual);
}
}
Binary file added images/java_bug.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions snippet1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
`[a link`](url.com)

[another link](`google.com)`

[`cod[e`](google.com)

[`code]`](ucsd.edu)
5 changes: 5 additions & 0 deletions snippet2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[a [nested link](a.com)](b.com)

[a nested parenthesized url](a.com(()))

[some escaped \[ brackets \]](example.com)
24 changes: 24 additions & 0 deletions snippet3.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion test-file.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Title

[link1](https://something.com)
[link2](some-thing.html)
[link2](some-thing.html)
1 change: 1 addition & 0 deletions test-file1.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Link](somethingtem t.com)
1 change: 1 addition & 0 deletions test-file2.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Link](somethingting.com)
1 change: 1 addition & 0 deletions test-file3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[Link]( )
3 changes: 3 additions & 0 deletions test-file4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# title

[]link goes here!
7 changes: 7 additions & 0 deletions test-file5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# title

[stuff]

paragraph

(page.com)
3 changes: 3 additions & 0 deletions test-file6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# title

![link](page.com)
1 change: 1 addition & 0 deletions test-file7.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
)[
2 changes: 2 additions & 0 deletions test-file8.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[](a link on the first line)
[