diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 000000000..935367546 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,36 @@ +# 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@v3 + + # 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 Run + diff --git a/.gitignore b/.gitignore index a1c2a238a..048f0e2b5 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,7 @@ .mtj.tmp/ # Package Files # -*.jar + *.war *.nar *.ear diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 000000000..65e1ec078 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "makefile.extensionOutputFolder": "./.vscode" +} \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..874289985 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +CLASSPATH = lib\*;. + +TryCommonMark.class: TryCommonMark.java + javac -g -cp "$(CLASSPATH)" TryCommonMark.java + +MarkdownParse.class: MarkdownParse.java + javac MarkdownParse.java + +MarkdownParseTest.class: MarkdownParseTest.java MarkdownParse.class + javac -cp ".;lib\junit-4.13.2.jar;lib\hamcrest-core-1.3.jar" MarkdownParseTest.java + +Run: MarkdownParseTest.class + 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/MarkdownParse.java b/MarkdownParse.java index 5ebf83aba..834aae8a8 100644 --- a/MarkdownParse.java +++ b/MarkdownParse.java @@ -16,6 +16,30 @@ public static ArrayList getLinks(String markdown) { int closeBracket = markdown.indexOf("]", openBracket); int openParen = markdown.indexOf("(", closeBracket); int closeParen = markdown.indexOf(")", openParen); + int exclamation = markdown.indexOf("!", currentIndex); + int backtick = markdown.indexOf("`", currentIndex); + int innerParen = markdown.indexOf("(", openParen+1); + if (innerParen > openParen && innerParen < closeParen){ + closeParen++; + currentIndex = closeParen + 1; + continue; + } + if (exclamation >= currentIndex && exclamation == openBracket - 1){ + currentIndex = closeParen + 1; + continue; + } + if (backtick >= currentIndex && backtick == openBracket - 1){ + currentIndex = closeParen + 1; + continue; + } + if (openBracket == -1 || closeBracket == -1 || + openParen == -1 || closeParen == -1){ + break; + } + if (markdown.charAt(openParen-1) != markdown.charAt(closeBracket)){ + currentIndex = closeParen + 1; + continue; + } toReturn.add(markdown.substring(openParen + 1, closeParen)); currentIndex = closeParen + 1; } @@ -24,7 +48,7 @@ public static ArrayList getLinks(String markdown) { } - public static void main(String[] args) throws IOException { + public static void main(String[] args) throws IOException{ Path fileName = Path.of(args[0]); String content = Files.readString(fileName); ArrayList links = getLinks(content); diff --git a/MarkdownParseTest.java b/MarkdownParseTest.java new file mode 100644 index 000000000..cd17f7249 --- /dev/null +++ b/MarkdownParseTest.java @@ -0,0 +1,118 @@ +import static org.junit.Assert.*; + +import java.io.File; +import java.nio.file.Files; +import java.util.List; +import java.io.IOException; +import java.nio.file.Path; + +import org.junit.*; +public class MarkdownParseTest { + @Test + public void addition() { + assertEquals(2, 1 + 1); + } + + @Test + public void testFile() throws IOException{ + Path fileName = Path.of("test-file.md"); + String content = Files.readString(fileName); + assertEquals(List.of("https://something.com", "some-thing.html"),MarkdownParse.getLinks(content)); + } + + @Test + public void break1() throws IOException{ + Path fileName = Path.of("breaking-test1.md"); + String content = Files.readString(fileName); + assertEquals(List.of(),MarkdownParse.getLinks(content)); + } + + @Test + public void break3() throws IOException{ + Path fileName = Path.of("breaking-test2.md"); + String content = Files.readString(fileName); + assertEquals(List.of("https://something.com", "some-thing.html"),MarkdownParse.getLinks(content)); + } + + @Test + public void break2() throws IOException{ + Path fileName = Path.of("breaking-test3.md"); + String content = Files.readString(fileName); + assertEquals(List.of("https://something.com"),MarkdownParse.getLinks(content)); + } + + @Test + public void testLinks() throws IOException{ + Path fileName = Path.of("test-file.md"); + String content = Files.readString(fileName); + assertEquals(List.of("https://something.com", "some-thing.html"), MarkdownParse.getLinks(content)); + } + @Test + public void testLink2() throws IOException{ + Path fileName = Path.of("test-file2.md"); + String content = Files.readString(fileName); + assertEquals(List.of("https://something.com", "some-page.html"), MarkdownParse.getLinks(content)); + } + @Test + public void testLink3() throws IOException{ + Path fileName = Path.of("test-file3.md"); + String content = Files.readString(fileName); + assertEquals(List.of(), MarkdownParse.getLinks(content)); + } + @Test + public void testLink4() throws IOException{ + Path fileName = Path.of("test-file4.md"); + String content = Files.readString(fileName); + assertEquals(List.of(), MarkdownParse.getLinks(content)); + } + @Test + public void testLink5() throws IOException{ + Path fileName = Path.of("test-file5.md"); + String content = Files.readString(fileName); + assertEquals(List.of(), MarkdownParse.getLinks(content)); + } + @Test + public void testLink6() throws IOException{ + Path fileName = Path.of("test-file6.md"); + String content = Files.readString(fileName); + assertEquals(List.of(), MarkdownParse.getLinks(content)); + } + @Test + public void testLink7() throws IOException{ + Path fileName = Path.of("test-file7.md"); + String content = Files.readString(fileName); + assertEquals(List.of(), MarkdownParse.getLinks(content)); + } + @Test + public void testLink8() throws IOException{ + Path fileName = Path.of("test-file8.md"); + String content = Files.readString(fileName); + assertEquals(List.of("a link on the first line"), MarkdownParse.getLinks(content)); + } + @Test + public void testLink9() throws IOException{ + Path fileName = Path.of("test-file9.md"); + String content = Files.readString(fileName); + assertEquals(List.of(), MarkdownParse.getLinks(content)); + } + @Test + public void testSnippet1() throws IOException{ + Path fileName = Path.of("snippet1.md"); + String content = Files.readString(fileName); + assertEquals(List.of("`google.com", "google.com", "ucsd.edu"), MarkdownParse.getLinks(content)); + } + @Test + public void testSnippet2() throws IOException{ + Path fileName = Path.of("snippet2.md"); + String content = Files.readString(fileName); + assertEquals(List.of("a.com", "a.com(())", "example.com"), MarkdownParse.getLinks(content)); + } + @Test + public void testSnippet3() throws IOException{ + Path fileName = Path.of("snippet3.md"); + String content = Files.readString(fileName); + assertEquals(List.of("https://www.twitter.com", + "https://sites.google.com/eng.ucsd.edu/cse-15l-spring-2022/schedule", + "https://cse.ucsd.edu/"), MarkdownParse.getLinks(content)); + } +} \ No newline at end of file diff --git a/TryCommonMark.java b/TryCommonMark.java new file mode 100644 index 000000000..b091ebb75 --- /dev/null +++ b/TryCommonMark.java @@ -0,0 +1,12 @@ +import org.commonmark.node.*; +import org.commonmark.parser.Parser; +import org.commonmark.renderer.html.HtmlRenderer; + +class TryCommonMark { + public static void main(String[] args) { + Parser parser = Parser.builder().build(); + Node document = parser.parse("This is *Sparta*"); + HtmlRenderer renderer = HtmlRenderer.builder().build(); // "

This is Sparta

\n" + System.out.println(renderer.render(document)); + } +} diff --git a/breaking-test1.md b/breaking-test1.md new file mode 100644 index 000000000..1fc8c054f --- /dev/null +++ b/breaking-test1.md @@ -0,0 +1 @@ +# Breaking test diff --git a/breaking-test2.md b/breaking-test2.md new file mode 100644 index 000000000..ca42df3e0 --- /dev/null +++ b/breaking-test2.md @@ -0,0 +1,5 @@ +# Breaking test 2 + +[link1](https://something.com) +[link2](some-thing.html) +![image](https://www.snopes.com/tachyon/2018/07/poop_emoji.jpg) diff --git a/breaking-test3.md b/breaking-test3.md new file mode 100644 index 000000000..5d5936565 --- /dev/null +++ b/breaking-test3.md @@ -0,0 +1,4 @@ +# Breaking test 3 + +[link1](https://something.com) +][)( diff --git a/lib/commonmark-0.19.0.jar b/lib/commonmark-0.19.0.jar new file mode 100644 index 000000000..7f251a7f7 Binary files /dev/null and b/lib/commonmark-0.19.0.jar differ 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/snippet1.md b/snippet1.md new file mode 100644 index 000000000..19e9961bd --- /dev/null +++ b/snippet1.md @@ -0,0 +1,7 @@ +`[a link`](url.com) + +[another link](`google.com)` + +[`cod[e`](google.com) + +[`code]`](ucsd.edu) diff --git a/snippet2.md b/snippet2.md new file mode 100644 index 000000000..6513a2174 --- /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) diff --git a/snippet3.md b/snippet3.md new file mode 100644 index 000000000..8a95dc68e --- /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 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-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 diff --git a/test-file9.md b/test-file9.md new file mode 100644 index 000000000..70f862dd6 --- /dev/null +++ b/test-file9.md @@ -0,0 +1 @@ +[](()) \ No newline at end of file