Procházet zdrojové kódy

add markdown exercise to track

Brandon Kiefer před 8 roky
rodič
revize
1c68b5280c

+ 12
- 0
config.json Zobrazit soubor

@@ -941,6 +941,18 @@
941 941
     {
942 942
       "core": false,
943 943
       "difficulty": 7,
944
+      "slug": "markdown",
945
+      "topics": [
946
+        "strings",
947
+        "conditionals",
948
+        "pattern_matching"
949
+      ],
950
+      "unlocked_by": "word-search",
951
+      "uuid": "377fe38b-08ad-4f3a-8118-a43c10f7b9b2"
952
+    },
953
+    {
954
+      "core": false,
955
+      "difficulty": 7,
944 956
       "slug": "poker",
945 957
       "topics": [
946 958
         "games",

+ 116
- 0
exercises/markdown/.meta/src/reference/java/Markdown.java Zobrazit soubor

@@ -0,0 +1,116 @@
1
+class Markdown {
2
+
3
+    String parse(String markdown)
4
+    {
5
+        String[] lines = markdown.split("\n");
6
+        StringBuilder result = new StringBuilder();
7
+        boolean activeList = false;
8
+
9
+        for (int i = 0; i < lines.length; i++)
10
+        {
11
+            String lineResult = parseLine(lines[i]);
12
+
13
+            if(lineResult.matches(".*(<li>).*") && !activeList){
14
+                activeList = true;
15
+                result.append("<ul>");
16
+                result.append(lineResult);
17
+            }
18
+            else if(!lineResult.matches(".*(<li>).*") && activeList){
19
+                activeList = false;
20
+                result.append("</ul>");
21
+                result.append(lineResult);
22
+            }
23
+            else {
24
+                result.append(lineResult);
25
+            }
26
+        }
27
+
28
+        if(activeList){
29
+            result.append("</ul>");
30
+        }
31
+
32
+        return result.toString();
33
+    }
34
+
35
+    private String parseLine(String markdown)
36
+    {
37
+        String result = parseHeader(markdown);
38
+
39
+        if (result == null)
40
+        {
41
+            result = parseListItem(markdown);
42
+        }
43
+
44
+        if (result == null)
45
+        {
46
+            result = parseParagraph(markdown);
47
+        }
48
+
49
+        return result;
50
+    }
51
+
52
+    private String parseHeader(String markdown)
53
+    {
54
+        int count = 0;
55
+
56
+        for (int i = 0; i < markdown.length(); i++)
57
+        {
58
+            if (markdown.charAt(i) == '#')
59
+            {
60
+                count++;
61
+            }
62
+            else
63
+            {
64
+                break;
65
+            }
66
+        }
67
+
68
+        if (count == 0)
69
+        {
70
+            return null;
71
+        }
72
+
73
+        return wrap(markdown.substring(count + 1), "h" + Integer.toString(count));
74
+    }
75
+
76
+    private String parseListItem(String markdown)
77
+    {
78
+        if (markdown.startsWith("*"))
79
+        {
80
+            return wrap(parseText(markdown.substring(2)), "li");
81
+        }
82
+
83
+        return null;
84
+    }
85
+
86
+    private String parseParagraph(String markdown)
87
+    {
88
+        return wrap(parseText(markdown), "p");
89
+    }
90
+
91
+    private String parseText(String markdown)
92
+    {
93
+        return parse_(parse__(markdown));
94
+    }
95
+
96
+    private String parse_(String markdown)
97
+    {
98
+        return parseViaRegex(markdown, "_", "em");
99
+    }
100
+
101
+    private String parse__(String markdown)
102
+    {
103
+        return parseViaRegex(markdown, "__", "strong");
104
+    }
105
+
106
+    private String parseViaRegex(String markdown, String delimiter, String tag)
107
+    {
108
+        String pattern = delimiter + "(.+)" + delimiter;
109
+        String replacement = "<" + tag + ">$1</" + tag + ">";
110
+        return markdown.replaceAll(pattern, replacement);
111
+    }
112
+
113
+    private String wrap(String text, String tag){
114
+        return "<" + tag + ">" + text + "</" + tag + ">";
115
+    }
116
+}

+ 1
- 0
exercises/markdown/.meta/version Zobrazit soubor

@@ -0,0 +1 @@
1
+1.1.0

+ 30
- 0
exercises/markdown/README.md Zobrazit soubor

@@ -0,0 +1,30 @@
1
+# Markdown
2
+
3
+Refactor a Markdown parser.
4
+
5
+The markdown exercise is a refactoring exercise. There is code that parses a
6
+given string with [Markdown
7
+syntax](https://guides.github.com/features/mastering-markdown/) and returns the
8
+associated HTML for that string. Even though this code is confusingly written
9
+and hard to follow, somehow it works and all the tests are passing! Your
10
+challenge is to re-write this code to make it easier to read and maintain
11
+while still making sure that all the tests keep passing.
12
+
13
+It would be helpful if you made notes of what you did in your refactoring in
14
+comments so reviewers can see that, but it isn't strictly necessary. The most
15
+important thing is to make the code better!
16
+
17
+# Running the tests
18
+
19
+You can run all the tests for an exercise by entering
20
+
21
+```sh
22
+$ gradle test
23
+```
24
+
25
+in your terminal.
26
+
27
+
28
+## Submitting Incomplete Solutions
29
+
30
+It's possible to submit an incomplete solution so you can see how others have completed the exercise.

+ 18
- 0
exercises/markdown/build.gradle Zobrazit soubor

@@ -0,0 +1,18 @@
1
+apply plugin: "java"
2
+apply plugin: "eclipse"
3
+apply plugin: "idea"
4
+
5
+repositories {
6
+    mavenCentral()
7
+}
8
+
9
+dependencies {
10
+    testCompile "junit:junit:4.12"
11
+}
12
+
13
+test {
14
+    testLogging {
15
+        exceptionFormat = 'full'
16
+        events = ["passed", "failed", "skipped"]
17
+    }
18
+}

+ 0
- 0
exercises/markdown/src/main/java/.keep Zobrazit soubor


+ 95
- 0
exercises/markdown/src/test/java/MarkdownTest.java Zobrazit soubor

@@ -0,0 +1,95 @@
1
+import org.junit.Before;
2
+import org.junit.Ignore;
3
+import org.junit.Test;
4
+
5
+import static org.junit.Assert.assertEquals;
6
+
7
+public class MarkdownTest {
8
+
9
+    private Markdown markdown;
10
+
11
+    @Before
12
+    public void setup() {
13
+        markdown = new Markdown();
14
+    }
15
+
16
+    @Test
17
+    public void normalTextAsAParagraph() {
18
+        String input = "This will be a paragraph";
19
+        String expected = "<p>This will be a paragraph</p>";
20
+
21
+        assertEquals(expected, markdown.parse(input));
22
+    }
23
+
24
+    @Ignore("Remove to run test")
25
+    @Test
26
+    public void italics() {
27
+        String input = "_This will be italic_";
28
+        String expected = "<p><em>This will be italic</em></p>";
29
+
30
+        assertEquals(expected, markdown.parse(input));
31
+    }
32
+
33
+    @Ignore("Remove to run test")
34
+    @Test
35
+    public void boldText() {
36
+        String input = "__This will be bold__";
37
+        String expected = "<p><strong>This will be bold</strong></p>";
38
+
39
+        assertEquals(expected, markdown.parse(input));
40
+    }
41
+
42
+    @Ignore("Remove to run test")
43
+    @Test
44
+    public void normalItalicsAndBoldText() {
45
+        String input = "This will _be_ __mixed__";
46
+        String expected = "<p>This will <em>be</em> <strong>mixed</strong></p>";
47
+
48
+        assertEquals(expected, markdown.parse(input) );
49
+    }
50
+
51
+    @Ignore("Remove to run test")
52
+    @Test
53
+    public void withH1HeaderLevel() {
54
+        String input = "# This will be an h1";
55
+        String expected = "<h1>This will be an h1</h1>";
56
+
57
+        assertEquals(expected, markdown.parse(input) );
58
+    }
59
+
60
+    @Ignore("Remove to run test")
61
+    @Test
62
+    public void withH2HeaderLevel() {
63
+        String input = "## This will be an h2";
64
+        String expected = "<h2>This will be an h2</h2>";
65
+
66
+        assertEquals(expected, markdown.parse(input) );
67
+    }
68
+
69
+    @Ignore("Remove to run test")
70
+    @Test
71
+    public void withH6HeaderLevel() {
72
+        String input = "###### This will be an h6";
73
+        String expected = "<h6>This will be an h6</h6>";
74
+
75
+        assertEquals(expected, markdown.parse(input) );
76
+    }
77
+
78
+    @Ignore("Remove to run test")
79
+    @Test
80
+    public void unorderedLists() {
81
+        String input = "* Item 1\n* Item 2";
82
+        String expected = "<ul><li>Item 1</li><li>Item 2</li></ul>";
83
+
84
+        assertEquals(expected, markdown.parse(input) );
85
+    }
86
+
87
+    @Ignore("Remove to run test")
88
+    @Test
89
+    public void aLittleBitOfEverything() {
90
+        String input = "# Header!\n* __Bold Item__\n* _Italic Item_";
91
+        String expected = "<h1>Header!</h1><ul><li><strong>Bold Item</strong></li><li><em>Italic Item</em></li></ul>";
92
+
93
+        assertEquals(expected, markdown.parse(input) );
94
+    }
95
+}

+ 1
- 0
exercises/settings.gradle Zobrazit soubor

@@ -39,6 +39,7 @@ include 'largest-series-product'
39 39
 include 'linked-list'
40 40
 include 'list-ops'
41 41
 include 'luhn'
42
+include 'markdown'
42 43
 include 'matrix'
43 44
 include 'meetup'
44 45
 include 'minesweeper'