Skip to content
This repository was archived by the owner on Nov 9, 2023. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
e2f12fa
Update MarkdownParse.java
mariawang2002826 Jan 20, 2022
450d7c7
create new test file
mariawang2002826 Jan 20, 2022
537f96c
edit files
mariawang2002826 Jan 20, 2022
564d5c3
fixed IndexOutOfBound Exception
mariawang2002826 Jan 20, 2022
a05b1e7
Create test-file-4.md
mariawang2002826 Jan 27, 2022
897934b
Update test-file-4.md
mariawang2002826 Jan 27, 2022
a47aae9
Fix error
mariawang2002826 Jan 27, 2022
69665c8
Create MarkdownParseTest.java
mariawang2002826 Jan 27, 2022
39fb90a
Update MarkdownParseTest.java
mariawang2002826 Jan 27, 2022
9ed9abb
Update MarkdownParseTest.java
mariawang2002826 Jan 28, 2022
502e04b
Fix logical error
mariawang2002826 Jan 28, 2022
0df7287
Update MarkdownParse.java
mariawang2002826 Feb 3, 2022
4971ba9
Create main.yml
mariawang2002826 Feb 3, 2022
b9003a8
Delete main.yml
mariawang2002826 Feb 3, 2022
3af6812
Create workflows
mariawang2002826 Feb 3, 2022
37b61cb
Create main.yml
mariawang2002826 Feb 3, 2022
7a8b46c
Delete .github directory
mariawang2002826 Feb 3, 2022
d4da303
Create workflows
mariawang2002826 Feb 3, 2022
473bb1e
Delete .github directory
mariawang2002826 Feb 3, 2022
fa042b0
Create test-file-5.md
mariawang2002826 Feb 3, 2022
304fafb
Create main.yml
mariawang2002826 Feb 3, 2022
c572377
Update MarkdownParseTest.java
mariawang2002826 Feb 3, 2022
9159012
Merge branch 'main' of https://github.com/mariawang2002826/markdown-p…
mariawang2002826 Feb 3, 2022
846ec8f
Delete .github/workflows directory
mariawang2002826 Feb 3, 2022
4cb9955
Create main.yml
mariawang2002826 Feb 3, 2022
e875bcb
Update main.yml
mariawang2002826 Feb 3, 2022
bda2bfe
Update MarkdownParseTest.java
mariawang2002826 Feb 4, 2022
a233b3c
Merge branch 'main' of https://github.com/mariawang2002826/markdown-p…
mariawang2002826 Feb 4, 2022
2f90341
update test
mariawang2002826 Feb 4, 2022
4d7acfe
Create Makefile
mariawang2002826 Feb 10, 2022
3a7a75c
change to use make
mariawang2002826 Feb 10, 2022
1865c4e
Create mdparse
mariawang2002826 Feb 10, 2022
a3122a5
Merge branch 'main' of https://github.com/mariawang2002826/markdown-p…
mariawang2002826 Feb 10, 2022
033d284
Update mdparse
mariawang2002826 Feb 10, 2022
3d109a2
Update MarkdownParseTest.java
mariawang2002826 Feb 24, 2022
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
36 changes: 36 additions & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
@@ -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
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -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
49 changes: 42 additions & 7 deletions MarkdownParse.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -10,20 +9,56 @@ public static ArrayList<String> 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<String> links = getLinks(contents);
System.out.println(links);
}
}
}
74 changes: 74 additions & 0 deletions MarkdownParseTest.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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<String> 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<String> 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<String> expected = List.of();
try {
assertEquals(expected, MarkdownParse.getLinks(Files.readString(Path.of("test-file-5.md"))));
}
catch (IOException e) {
System.out.println(e.getMessage());
}

}


}
Binary file added lib/hamcrest-core-1.3.jar
Binary file not shown.
Binary file added lib/junit-4.13.2.jar
Binary file not shown.
Binary file added lib/junit-platform-console-standalone-1.8.2.jar
Binary file not shown.
2 changes: 2 additions & 0 deletions mdparse
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
java -cp lib/junit-4.13.2.jar:lib/hamcrest-core-1.3.jar:. MarkdownParse $1
echo $1
4 changes: 4 additions & 0 deletions new-test-file.md
Original file line number Diff line number Diff line change
@@ -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)
2 changes: 2 additions & 0 deletions test-file-3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Test file 3
[Link](https://github.com/mariawang2002826/markdown-parse)
4 changes: 4 additions & 0 deletions test-file-4.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Test file

[a link](https://ucsd-cse15l-w22.github.io/)
![image](test.png)
2 changes: 2 additions & 0 deletions test-file-5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# Test file 5
[[]]
3 changes: 2 additions & 1 deletion test-file.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Title

[a link!](https://something.com)
[another link!](some-page.html)
[another link!](some-page.html)
[]()