diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 000000000..45de81855 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,36 @@ +# This is a basic workflow to help you get started with Actions + +name: Markdown JUnit test + +# Controls when the workflow will run +on: + # Triggers the workflow on push or pull request events but only for the main branch + push: + branches: [ main ] + pull_request: + branches: [ main ] + + # Allows you to run this workflow manually from the Actions tab + workflow_dispatch: + +# A workflow run is made up of one or more jobs that can run sequentially or in parallel +jobs: + # This workflow contains a single job called "build" + build: + # The type of runner that the job will run on + runs-on: ubuntu-latest + + # Steps represent a sequence of tasks that will be executed as part of the job + steps: + # Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it + - uses: actions/checkout@v2 + + # Runs a single command using the runners shell + - name: Run a one-line script + run: echo Hello, world! + + # Runs a set of commands using the runners shell + - name: Run a multi-line script + run: | + echo The directory is: $GITHUB_WORKSPACE + make test diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..af37212a8 --- /dev/null +++ b/Makefile @@ -0,0 +1,6 @@ +MarkdownParse.class: MarkdownParse.java + javac MarkdownParse.java +MarkdownParseTest.class: MarkdownParseTest.java + javac -cp .:lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar MarkdownParseTest.java +test: MarkdownParse.class MarkdownParseTest.class + java -cp .:lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar org.junit.runner.JUnitCore MarkdownParseTest diff --git a/MarkdownParse.java b/MarkdownParse.java index 0534f8140..0b854508e 100644 --- a/MarkdownParse.java +++ b/MarkdownParse.java @@ -1,4 +1,3 @@ -// 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; @@ -10,20 +9,56 @@ public static ArrayList getLinks(String markdown) { // find the next [, then find the ], then find the (, then take up to // the next ) int currentIndex = 0; + int lastClosedParen = markdown.lastIndexOf(")"); while(currentIndex < markdown.length()) { int nextOpenBracket = markdown.indexOf("[", currentIndex); + + boolean isImage = false; + + if (markdown.indexOf("!", nextOpenBracket-1) == nextOpenBracket - 1) isImage = true; + + + if (nextOpenBracket == 0) isImage = false; + int nextCloseBracket = markdown.indexOf("]", nextOpenBracket); - int openParen = markdown.indexOf("(", nextCloseBracket); + int openParen = markdown.indexOf("(", currentIndex); int closeParen = markdown.indexOf(")", openParen); - toReturn.add(markdown.substring(openParen + 1, closeParen)); - currentIndex = closeParen + 1; + + if(nextOpenBracket == -1 || nextCloseBracket == -1 || openParen == -1 || closeParen == -1) { + break; + } + + if (openParen - nextCloseBracket > 2) { + currentIndex = markdown.indexOf("[", currentIndex + 1); + + if (currentIndex == lastClosedParen) { + break; + } else if (currentIndex < 0) { + break; + } + continue; + } + + + + if(markdown.substring(nextOpenBracket-1, nextOpenBracket).equals("!")) { + currentIndex = closeParen + 1; + continue; + } + + if (!isImage) toReturn.add(markdown.substring(openParen + 1, closeParen)); + if (closeParen > currentIndex){ + currentIndex = closeParen + 1; + }else { + break; + } } return toReturn; } public static void main(String[] args) throws IOException { - Path fileName = Path.of(args[0]); - String contents = Files.readString(fileName); + Path fileName = Path.of(args[0]); + String contents = Files.readString(fileName); ArrayList links = getLinks(contents); System.out.println(links); } -} \ No newline at end of file +} diff --git a/MarkdownParseTest.java b/MarkdownParseTest.java new file mode 100644 index 000000000..8238cb732 --- /dev/null +++ b/MarkdownParseTest.java @@ -0,0 +1,74 @@ +import static org.junit.Assert.*; + +import java.beans.Transient; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.List; + +import org.junit.*; + +public class MarkdownParseTest { + @Test + public void addition() { + assertEquals(2, 1 + 1); + } + + @Test + public void testGetlink() { + List expected = List.of("https://something.com", "some-page.html", ""); + try { + assertEquals(expected, MarkdownParse.getLinks(Files.readString(Path.of("test-file.md")))); + } + catch (IOException e) { + System.out.println(e.getMessage()); + } + } + + @Test + public void testGetlink1() { + List expected = List.of("https://www.nelink.com"); + try { + assertEquals(expected, MarkdownParse.getLinks(Files.readString(Path.of("new-test-file.md")))); + } + catch (IOException e) { + System.out.println(e.getMessage()); + } + } + + @Test + public void testGetlink2() { + List expected = List.of("https://github.com/mariawang2002826/markdown-parse"); + try { + assertEquals(expected, MarkdownParse.getLinks(Files.readString(Path.of("test-file-3.md")))); + } + catch (IOException e) { + System.out.println(e.getMessage()); + } + } + + @Test + public void testGetlink3() { + List expected = List.of("https://ucsd-cse15l-w22.github.io/"); + try { + assertEquals(expected, MarkdownParse.getLinks(Files.readString(Path.of("test-file-4.md")))); + } + catch (IOException e) { + System.out.println(e.getMessage()); + } + + } + @Test + public void testGetlink4() { + List expected = List.of(); + try { + assertEquals(expected, MarkdownParse.getLinks(Files.readString(Path.of("test-file-5.md")))); + } + catch (IOException e) { + System.out.println(e.getMessage()); + } + + } + + +} \ No newline at end of file diff --git a/lib/hamcrest-core-1.3.jar b/lib/hamcrest-core-1.3.jar new file mode 100644 index 000000000..9d5fe16e3 Binary files /dev/null and b/lib/hamcrest-core-1.3.jar differ diff --git a/lib/junit-4.13.2.jar b/lib/junit-4.13.2.jar new file mode 100644 index 000000000..6da55d8b8 Binary files /dev/null and b/lib/junit-4.13.2.jar differ diff --git a/lib/junit-platform-console-standalone-1.8.2.jar b/lib/junit-platform-console-standalone-1.8.2.jar new file mode 100644 index 000000000..d6dc60c53 Binary files /dev/null and b/lib/junit-platform-console-standalone-1.8.2.jar differ diff --git a/mdparse b/mdparse new file mode 100644 index 000000000..8177ee12f --- /dev/null +++ b/mdparse @@ -0,0 +1,2 @@ +java -cp lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar:. MarkdownParse $1 +echo $1 \ No newline at end of file diff --git a/new-test-file.md b/new-test-file.md new file mode 100644 index 000000000..47be34f7e --- /dev/null +++ b/new-test-file.md @@ -0,0 +1,4 @@ +# New test file + +[Link](https://www.nelink.com) +![Image](https://animals.net/wp-content/uploads/2019/01/Water-Dragon-5.jpg) diff --git a/test-file-3.md b/test-file-3.md new file mode 100644 index 000000000..40b1a0376 --- /dev/null +++ b/test-file-3.md @@ -0,0 +1,2 @@ +# Test file 3 +[Link](https://github.com/mariawang2002826/markdown-parse) diff --git a/test-file-4.md b/test-file-4.md new file mode 100644 index 000000000..ec79f9e4f --- /dev/null +++ b/test-file-4.md @@ -0,0 +1,4 @@ +# Test file + +[a link](https://ucsd-cse15l-w22.github.io/) +![image](test.png) \ No newline at end of file diff --git a/test-file-5.md b/test-file-5.md new file mode 100644 index 000000000..c255697db --- /dev/null +++ b/test-file-5.md @@ -0,0 +1,2 @@ +# Test file 5 +[[]] \ No newline at end of file diff --git a/test-file.md b/test-file.md index 783aa4cae..b647224d2 100644 --- a/test-file.md +++ b/test-file.md @@ -1,4 +1,5 @@ # Title [a link!](https://something.com) -[another link!](some-page.html) \ No newline at end of file +[another link!](some-page.html) +[]() \ No newline at end of file