Parcourir la source

update markdown ref solution and add initial solution

Brandon Kiefer il y a 8 ans
Parent
révision
9f05774b5d

+ 3
- 2
config.json Voir le fichier

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

+ 25
- 45
exercises/markdown/.meta/src/reference/java/Markdown.java Voir le fichier

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

+ 0
- 0
exercises/markdown/src/main/java/.keep Voir le fichier


+ 83
- 0
exercises/markdown/src/main/java/Markdown.java Voir le fichier

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 Voir le fichier

45
         String input = "This will _be_ __mixed__";
45
         String input = "This will _be_ __mixed__";
46
         String expected = "<p>This will <em>be</em> <strong>mixed</strong></p>";
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
     @Ignore("Remove to run test")
51
     @Ignore("Remove to run test")
54
         String input = "# This will be an h1";
54
         String input = "# This will be an h1";
55
         String expected = "<h1>This will be an h1</h1>";
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
     @Ignore("Remove to run test")
60
     @Ignore("Remove to run test")
63
         String input = "## This will be an h2";
63
         String input = "## This will be an h2";
64
         String expected = "<h2>This will be an h2</h2>";
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
     @Ignore("Remove to run test")
69
     @Ignore("Remove to run test")
72
         String input = "###### This will be an h6";
72
         String input = "###### This will be an h6";
73
         String expected = "<h6>This will be an h6</h6>";
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
     @Ignore("Remove to run test")
78
     @Ignore("Remove to run test")
81
         String input = "* Item 1\n* Item 2";
81
         String input = "* Item 1\n* Item 2";
82
         String expected = "<ul><li>Item 1</li><li>Item 2</li></ul>";
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
     @Ignore("Remove to run test")
87
     @Ignore("Remove to run test")
90
         String input = "# Header!\n* __Bold Item__\n* _Italic Item_";
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>";
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
 }