Przeglądaj źródła

update markdown ref solution and add initial solution

Brandon Kiefer 8 lat temu
rodzic
commit
9f05774b5d

+ 3
- 2
config.json Wyświetl plik

@@ -945,9 +945,10 @@
945 945
       "topics": [
946 946
         "strings",
947 947
         "conditionals",
948
-        "pattern_matching"
948
+        "pattern_matching",
949
+        "refactoring"
949 950
       ],
950
-      "unlocked_by": "word-search",
951
+      "unlocked_by": "scrabble-score",
951 952
       "uuid": "cc18f2e5-4e36-47d6-aa60-8ca5ff54019a"
952 953
     },
953 954
     {

+ 25
- 45
exercises/markdown/.meta/src/reference/java/Markdown.java Wyświetl plik

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

+ 0
- 0
exercises/markdown/src/main/java/.keep Wyświetl plik


+ 83
- 0
exercises/markdown/src/main/java/Markdown.java Wyświetl plik

@@ -0,0 +1,83 @@
1
+class Markdown {
2
+  
3
+    String parse(String markdown) {
4
+
5
+        String[] lines = markdown.split("\n");
6
+
7
+        String result = "";
8
+
9
+        boolean activeList = false;
10
+
11
+        for (int i = 0; i < lines.length; i++) {
12
+
13
+            String theLine = parseHeader(lines[i]);
14
+          
15
+            if (theLine == null) {
16
+              theLine = parseListItem(lines[i]);
17
+            }
18
+    
19
+            if (theLine == null) 
20
+            {
21
+                theLine = parseParagraph(lines[i]);
22
+            }
23
+
24
+            if (theLine.matches("(<li>).*") && !theLine.matches("(<h).*") && !theLine.matches("(<p>).*") && !activeList) {
25
+                activeList = true;
26
+              result = result + "<ul>";
27
+                result = result + theLine;
28
+            } 
29
+            
30
+            else if (!theLine.matches("(<li>).*") && activeList) {
31
+                activeList = false;
32
+                result = result + "</ul>";
33
+                result = result + theLine;
34
+            } else {
35
+              result = result + theLine;
36
+            }
37
+        }
38
+
39
+        if (activeList) {
40
+            result = result + "</ul>";
41
+        }
42
+
43
+        return result;
44
+    }
45
+
46
+    private String parseHeader(String markdown) {
47
+        int count = 0;
48
+
49
+        for (int i = 0; i < markdown.length() && markdown.charAt(i) == '#'; i++) 
50
+        {
51
+            count++;
52
+        }
53
+
54
+        if (count == 0) { return null; }
55
+
56
+        return "<h" + Integer.toString(count) + ">" + markdown.substring(count + 1) + "</h" + Integer.toString(count)+ ">";
57
+    }
58
+
59
+    private String parseListItem(String markdown) {
60
+        if (markdown.startsWith("*")) {
61
+            String skipAsterisk = markdown.substring(2);
62
+            String listItemString = parseSomeSymbols(skipAsterisk);
63
+            return "<li>" + listItemString + "</li>";
64
+        }
65
+
66
+        return null;
67
+    }
68
+
69
+    private String parseParagraph(String markdown) {
70
+        return "<p>" + parseSomeSymbols(markdown) + "</p>";
71
+    }
72
+
73
+    private String parseSomeSymbols(String markdown) {
74
+
75
+        String lookingFor = "__(.+)__";
76
+        String update = "<strong>$1</strong>";
77
+        String workingOn = markdown.replaceAll(lookingFor, update);
78
+
79
+        lookingFor = "_(.+)_";
80
+        update = "<em>$1</em>";
81
+        return workingOn.replaceAll(lookingFor, update);
82
+    }
83
+}

+ 6
- 6
exercises/markdown/src/test/java/MarkdownTest.java Wyświetl plik

@@ -45,7 +45,7 @@ public class MarkdownTest {
45 45
         String input = "This will _be_ __mixed__";
46 46
         String expected = "<p>This will <em>be</em> <strong>mixed</strong></p>";
47 47
 
48
-        assertEquals(expected, markdown.parse(input) );
48
+        assertEquals(expected, markdown.parse(input));
49 49
     }
50 50
 
51 51
     @Ignore("Remove to run test")
@@ -54,7 +54,7 @@ public class MarkdownTest {
54 54
         String input = "# This will be an h1";
55 55
         String expected = "<h1>This will be an h1</h1>";
56 56
 
57
-        assertEquals(expected, markdown.parse(input) );
57
+        assertEquals(expected, markdown.parse(input));
58 58
     }
59 59
 
60 60
     @Ignore("Remove to run test")
@@ -63,7 +63,7 @@ public class MarkdownTest {
63 63
         String input = "## This will be an h2";
64 64
         String expected = "<h2>This will be an h2</h2>";
65 65
 
66
-        assertEquals(expected, markdown.parse(input) );
66
+        assertEquals(expected, markdown.parse(input));
67 67
     }
68 68
 
69 69
     @Ignore("Remove to run test")
@@ -72,7 +72,7 @@ public class MarkdownTest {
72 72
         String input = "###### This will be an h6";
73 73
         String expected = "<h6>This will be an h6</h6>";
74 74
 
75
-        assertEquals(expected, markdown.parse(input) );
75
+        assertEquals(expected, markdown.parse(input));
76 76
     }
77 77
 
78 78
     @Ignore("Remove to run test")
@@ -81,7 +81,7 @@ public class MarkdownTest {
81 81
         String input = "* Item 1\n* Item 2";
82 82
         String expected = "<ul><li>Item 1</li><li>Item 2</li></ul>";
83 83
 
84
-        assertEquals(expected, markdown.parse(input) );
84
+        assertEquals(expected, markdown.parse(input));
85 85
     }
86 86
 
87 87
     @Ignore("Remove to run test")
@@ -90,6 +90,6 @@ public class MarkdownTest {
90 90
         String input = "# Header!\n* __Bold Item__\n* _Italic Item_";
91 91
         String expected = "<h1>Header!</h1><ul><li><strong>Bold Item</strong></li><li><em>Italic Item</em></li></ul>";
92 92
 
93
-        assertEquals(expected, markdown.parse(input) );
93
+        assertEquals(expected, markdown.parse(input));
94 94
     }
95 95
 }