diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 000000000..56a2c5ea8 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,35 @@ +# This is a basic workflow to help you get started with Actions + +name: CI + +# 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: | + make test diff --git a/MarkdownParse.java b/MarkdownParse.java index 0534f8140..0e1f040bf 100644 --- a/MarkdownParse.java +++ b/MarkdownParse.java @@ -10,13 +10,44 @@ public static ArrayList getLinks(String markdown) { // find the next [, then find the ], then find the (, then take up to // the next ) int currentIndex = 0; - while(currentIndex < markdown.length()) { + while(markdown.indexOf("[", currentIndex) != -1) { int nextOpenBracket = markdown.indexOf("[", currentIndex); - int nextCloseBracket = markdown.indexOf("]", nextOpenBracket); - int openParen = markdown.indexOf("(", nextCloseBracket); - int closeParen = markdown.indexOf(")", openParen); - toReturn.add(markdown.substring(openParen + 1, closeParen)); - currentIndex = closeParen + 1; + if(nextOpenBracket != 0 && markdown.charAt(nextOpenBracket - 1) == '!'){ + int nextCloseBracket = markdown.indexOf("]", nextOpenBracket); + if(nextCloseBracket == -1){ + nextCloseBracket = nextOpenBracket; + } + int openParen = markdown.indexOf("(", nextCloseBracket); + if(openParen == -1){ + openParen = nextOpenBracket; + } + int closeParen = markdown.indexOf(")", openParen); + if(closeParen == -1){ + closeParen = openParen; + } + currentIndex = closeParen + 1; + } + else{ + int nextCloseBracket = markdown.indexOf("]", nextOpenBracket); + if(nextCloseBracket == -1){ + nextCloseBracket = nextOpenBracket; + } + int openParen = markdown.indexOf("(", nextCloseBracket); + if(openParen == -1){ + openParen = nextOpenBracket; + } + int closeParen = markdown.indexOf(")", openParen); + if(closeParen == -1){ + closeParen = openParen; + } + if(nextCloseBracket != openParen - 1){ + currentIndex = closeParen + 1; + } + else{ + toReturn.add(markdown.substring(openParen + 1, closeParen)); + currentIndex = closeParen + 1; + } + } } return toReturn; } diff --git a/MarkdownParseTest.java b/MarkdownParseTest.java new file mode 100644 index 000000000..d917ed7f5 --- /dev/null +++ b/MarkdownParseTest.java @@ -0,0 +1,51 @@ +/* +import static org.junit.Assert.*; +import org.junit.*; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.List; + +public class MarkdownParseTest{ + String file1; + String file2; + String file3; + @Before + public void setUp() throws IOException{ + file1 = Files.readString(Path.of("test-file.md")); + file2 = Files.readString(Path.of("test-file2.md")); + file3 = Files.readString(Path.of("test-file3.md")); + } + @Test + public void addition(){ + assertEquals(2, 1+1); + } + @Test + public void testGetLinks(){ + assertEquals(List.of("https://something.com", "some-page.html"), MarkdownParse.getLinks(file1)); + assertEquals(List.of("https://something.com", "some-page.html"), MarkdownParse.getLinks(file2)); + assertEquals(List.of(), MarkdownParse.getLinks(file3)); + + } +} +*/ + +import static org.junit.Assert.*; +import org.junit.*; + +import java.io.IOException; +import java.nio.file.*; +import java.util.*; +public class MarkdownParseTest { + + @Test + public void improvmentTest() throws IOException{ + Path fileName = Path.of("test-file-l5.md"); + String contents = Files.readString(fileName); + + assertEquals(List.of("something.com()"), MarkdownParse.getLinks(contents)); + } + + +} diff --git a/bug-maker-1.md b/bug-maker-1.md new file mode 100644 index 000000000..3c71870ae --- /dev/null +++ b/bug-maker-1.md @@ -0,0 +1,2 @@ + +[a link]!](www.link.com/dsajlfldkjsajklf) \ No newline at end of file diff --git a/bug-maker-2.md b/bug-maker-2.md new file mode 100644 index 000000000..18e3cf1db --- /dev/null +++ b/bug-maker-2.md @@ -0,0 +1,3 @@ + + +[a link](www.link.com/)dsajlfldkjsajklf) \ No newline at end of file diff --git a/bug-maker-3.md b/bug-maker-3.md new file mode 100644 index 000000000..bc236d321 --- /dev/null +++ b/bug-maker-3.md @@ -0,0 +1,2 @@ + +[a link \ 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/makefile b/makefile new file mode 100644 index 000000000..40b40cdba --- /dev/null +++ b/makefile @@ -0,0 +1,4 @@ + +test : MarkdownParse.java + javac -cp .:lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar MarkdownParseTest.java + java -cp .:lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar org.junit.runner.JUnitCore MarkdownParseTest \ No newline at end of file diff --git a/test-file-l5.md b/test-file-l5.md new file mode 100644 index 000000000..fed66654e --- /dev/null +++ b/test-file-l5.md @@ -0,0 +1 @@ +[link](something.com())) \ No newline at end of file diff --git a/test-file2.md b/test-file2.md new file mode 100644 index 000000000..24fac4add --- /dev/null +++ b/test-file2.md @@ -0,0 +1,6 @@ +# Title + +[a link!](https://something.com) +[another link!](some-page.html) + +some paragraph text after the links \ No newline at end of file diff --git a/test-file3.md b/test-file3.md new file mode 100644 index 000000000..031b99a6d --- /dev/null +++ b/test-file3.md @@ -0,0 +1,5 @@ +# title + +[] + +more text here \ 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