diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 000000000..8f4cd2b9a --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,29 @@ +# 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 MarkdownParseTest.class diff --git a/.gitignore b/.gitignore index a1c2a238a..9ca780152 100644 --- a/.gitignore +++ b/.gitignore @@ -11,7 +11,6 @@ .mtj.tmp/ # Package Files # -*.jar *.war *.nar *.ear diff --git a/Junit Command b/Junit Command new file mode 100644 index 000000000..bde353e85 --- /dev/null +++ b/Junit Command @@ -0,0 +1,7 @@ +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 + + +linux and mac +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/Makefile b/Makefile new file mode 100644 index 000000000..afc13de72 --- /dev/null +++ b/Makefile @@ -0,0 +1,13 @@ +CLASSPATH = lib/*:. +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 +Test: MarkdownParseTest.class MarkdownParse.class + java -cp .:lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar org.junit.runner.JUnitCore MarkdownParseTest + +TryCommonMark.class: TryCommonMark.java + javac -g -cp $(CLASSPATH) TryCommonMark.java + +TestTryCommonMark : TryCommonMark.class + java -cp lib/*:. TryCommonMark \ No newline at end of file diff --git a/MarkdownParse.java b/MarkdownParse.java index 5ebf83aba..2b1ce0d59 100644 --- a/MarkdownParse.java +++ b/MarkdownParse.java @@ -16,10 +16,37 @@ public static ArrayList getLinks(String markdown) { int closeBracket = markdown.indexOf("]", openBracket); int openParen = markdown.indexOf("(", closeBracket); int closeParen = markdown.indexOf(")", openParen); - toReturn.add(markdown.substring(openParen + 1, closeParen)); + + int openBrackPrevIndex = openBracket-1; + // System.out.println(markdown.substring(openParen + 1, closeParen)); + // check if escape character + if (openBracket == -1 || closeBracket ==-1 || openParen == -1 || closeParen == -1){ + break; + } + + if (openBrackPrevIndex >=0 && ( + markdown.substring(openBrackPrevIndex, openBracket).equals("!") || markdown.substring(openBrackPrevIndex, openBracket).equals("\\"))) { + // System.out.println("image"); + // Math.max(openBracket,closeBracket, openParen,closePare/n); + } + else if (openBracket + 1 == closeBracket) {} + else if (openParen + 1 == closeParen) { + currentIndex = closeParen + 1; + continue;} + else if (openBracket == 0) { + System.out.println("zero"); + toReturn.add(markdown.substring(openParen + 1, closeParen)); + } + else if (closeBracket + 1 == openParen) { + // System.out.println("]( next to eachother"); + toReturn.add(markdown.substring(openParen + 1, closeParen)); + } + + // Cases gets triggered falsly. ONe is that it might trigger another case while it isn't a link. Faulty currentIndex = closeParen + 1; + // System.out.println(currentIndex); } - + System.out.println(toReturn.toString()); return toReturn; } @@ -30,4 +57,5 @@ public static void main(String[] args) throws IOException { ArrayList links = getLinks(content); System.out.println(links); } + //Testing purpose 11111111 } diff --git a/MarkdownParseTest.java b/MarkdownParseTest.java new file mode 100644 index 000000000..8bb2506ea --- /dev/null +++ b/MarkdownParseTest.java @@ -0,0 +1,125 @@ +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; + +public class MarkdownParseTest { + @Test + public void addition() { + assertEquals(2, 1 + 1); + } + + public String readFile(String file) throws IOException { + String fullPath = file; + Path fileName = Path.of(fullPath); + String content = Files.readString(fileName); + return content; + } + + @Test + public void testProvidedTestFile() throws IOException { + String content = readFile("test-file.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {"https://something.com", "some-thing.html"}; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testTest2File() throws IOException { + String content = readFile("test2.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[0]; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testTestNoLink() throws IOException { + String content = readFile("testNoLink.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[0]; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testTestImageAndLink() throws IOException { + String content = readFile("testImageAndLink.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {"shouldbeidentified.com"}; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testProvidedTestFile2() throws IOException { + String content = readFile("test-file2.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {"https://something.com", "some-page.html"}; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testProvidedTestFile3() throws IOException { + String content = readFile("test-file3.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {}; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testProvidedTestFile4() throws IOException { + String content = readFile("test-file4.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {}; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testProvidedTestFile5() throws IOException { + String content = readFile("test-file5.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {}; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testProvidedTestFile6() throws IOException { + String content = readFile("test-file6.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {}; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testProvidedTestFile7() throws IOException { + String content = readFile("test-file7.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {}; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testProvidedTestFile8() throws IOException { + String content = readFile("test-file8.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {}; + assertArrayEquals(expected, links.toArray()); + } + + @Test + public void testProvidedTestEmptyParen() throws IOException { + String content = readFile("testNoLinkInParen.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {}; + assertArrayEquals(expected, links.toArray()); + } + @Test + public void testProvidedTest9() throws IOException { + String content = readFile("test-file9.md"); + ArrayList links = MarkdownParse.getLinks(content); + String[] expected = new String[] {}; + assertArrayEquals(expected, links.toArray()); + } + //Helloooo +} diff --git a/TryCommonMark.java b/TryCommonMark.java new file mode 100644 index 000000000..75fda8ecc --- /dev/null +++ b/TryCommonMark.java @@ -0,0 +1,38 @@ +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(); + String content = renderer.render(document); // "

This is Sparta

\n" + // System.out.println(content); + + // # this part actually does the computation + Node node = parser.parse("Example\n=======\n\nSome more text"); + WordCountVisitor visitor = new WordCountVisitor(); + node.accept(visitor); + // visitor.wordCount; // 4 + System.out.println(visitor.wordCount); + } + + +} + +// # this class can be defined anywhere in the file +class WordCountVisitor extends AbstractVisitor { + int wordCount = 0; + + @Override + public void visit(Text text) { + // This is called for all Text nodes. Override other visit methods for other node types. + + // Count words (this is just an example, don't actually do it this way for various reasons). + wordCount += text.getLiteral().split("\\W+").length; + + // Descend into children (could be omitted in this case because Text nodes don't have children). + visitChildren(text); + } +} 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/mdparse b/mdparse new file mode 100644 index 000000000..35ed54add --- /dev/null +++ b/mdparse @@ -0,0 +1 @@ +java -cp lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar:. MarkdownParse $1 \ 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-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..f5e0f0736 --- /dev/null +++ b/test-file9.md @@ -0,0 +1 @@ +\[an-escaped-link](test) diff --git a/test2.md b/test2.md new file mode 100644 index 000000000..795238328 --- /dev/null +++ b/test2.md @@ -0,0 +1 @@ +# Hi ![Google](https://www.google.com/logos/doodles/2021/doodle-champion-island-games-begin-6753651837108462.2-2xa.gif) \ No newline at end of file diff --git a/testImageAndLink.md b/testImageAndLink.md new file mode 100644 index 000000000..0de496648 --- /dev/null +++ b/testImageAndLink.md @@ -0,0 +1,3 @@ +![setting Icon]( https://www.google.com/logos/doodles/2017/celebrating-50-years-of-kids-coding-5745168905928704.4-lawcta.gif) + +[This is an URL Link](shouldbeidentified.com) diff --git a/testNoLink.md b/testNoLink.md new file mode 100644 index 000000000..a936a6357 --- /dev/null +++ b/testNoLink.md @@ -0,0 +1 @@ +(pretend)[link \ No newline at end of file diff --git a/testNoLinkInParen.md b/testNoLinkInParen.md new file mode 100644 index 000000000..e6f60c6d2 --- /dev/null +++ b/testNoLinkInParen.md @@ -0,0 +1 @@ +[link empty]() \ No newline at end of file