Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions bin/auto-sync.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ line-up
linked-list
list-ops
luhn
markdown
matching-brackets
meetup
micro-blog
Expand Down
20 changes: 9 additions & 11 deletions exercises/practice/markdown/.docs/instructions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

Refactor a Markdown parser.

The markdown exercise is a refactoring exercise. There is code that parses a
given string with [Markdown
syntax](https://guides.github.com/features/mastering-markdown/) and returns the
associated HTML for that string. Even though this code is confusingly written
and hard to follow, somehow it works and all the tests are passing! Your
challenge is to re-write this code to make it easier to read and maintain
while still making sure that all the tests keep passing.

It would be helpful if you made notes of what you did in your refactoring in
comments so reviewers can see that, but it isn't strictly necessary. The most
important thing is to make the code better!
The markdown exercise is a refactoring exercise.
There is code that parses a given string with [Markdown syntax][markdown] and returns the associated HTML for that string.
Even though this code is confusingly written and hard to follow, somehow it works and all the tests are passing!
Your challenge is to re-write this code to make it easier to read and maintain while still making sure that all the tests keep passing.

It would be helpful if you made notes of what you did in your refactoring in comments so reviewers can see that, but it isn't strictly necessary.
The most important thing is to make the code better!

[markdown]: https://docs.github.com/en/get-started/writing-on-github/getting-started-with-writing-and-formatting-on-github/basic-writing-and-formatting-syntax
5 changes: 3 additions & 2 deletions exercises/practice/markdown/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"contributors": [
"arueckauer",
"kytrinyx",
"yisraeldov"
"yisraeldov",
"Narkunan"
],
"files": {
"solution": [
Expand All @@ -18,5 +19,5 @@
".meta/example.php"
]
},
"blurb": "Refactor a Markdown parser"
"blurb": "Refactor a Markdown parser."
}
46 changes: 30 additions & 16 deletions exercises/practice/markdown/.meta/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,64 @@ function parseMarkdown($markdown)
$isInList = false;

foreach ($lines as &$line) {
if (preg_match('/^######(.*)/', $line, $matches)) {
if (preg_match('/^####### (.*)/', $line, $matches)) {
$line = "<p>" . trim($matches[0]) . "</p>";
} elseif (preg_match('/^###### (.*)/', $line, $matches)) {
$line = "<h6>" . trim($matches[1]) . "</h6>";
} elseif (preg_match('/^##(.*)/', $line, $matches)) {
} elseif (preg_match('/^##### (.*)/', $line, $matches)) {
$line = "<h5>" . trim($matches[1]) . "</h5>";
} elseif (preg_match('/^#### (.*)/', $line, $matches)) {
$line = "<h4>" . trim($matches[1]) . "</h4>";
} elseif (preg_match('/^### (.*)/', $line, $matches)) {
$line = "<h3>" . trim($matches[1]) . "</h3>";
} elseif (preg_match('/^## (.*)/', $line, $matches)) {
$line = "<h2>" . trim($matches[1]) . "</h2>";
} elseif (preg_match('/^#(.*)/', $line, $matches)) {
} elseif (preg_match('/^# (.*)/', $line, $matches)) {
$line = "<h1>" . trim($matches[1]) . "</h1>";
}

if (preg_match('/\*(.*)/', $line, $matches)) {
if (preg_match('/^\s*\*(.*)/', $line, $matches)) {
if (!$isInList) {
$isInList = true;
$isBold = false;
$isItalic = false;
if (preg_match('/(.*)__(.*)__(.*)/', $matches[1], $matches2)) {
$matches[1] = $matches2[1] . '<em>' . $matches2[2] . '</em>' . $matches2[3];
$matches[1] = $matches2[1] . '<strong>' . $matches2[2] . '</strong>' . $matches2[3];
$isBold = true;
}

if (preg_match('/(.*)_(.*)_(.*)/', $matches[1], $matches3)) {
$matches[1] = $matches3[1] . '<i>' . $matches3[2] . '</i>' . $matches3[3];
$matches[1] = $matches3[1] . '<em>' . $matches3[2] . '</em>' . $matches3[3];
$isItalic = true;
}

if ($isItalic || $isBold) {
$line = "<ul><li>" . trim($matches[1]) . "</li>";
} else {
$line = "<ul><li><p>" . trim($matches[1]) . "</p></li>";
$line = "<ul>";
$line .= "<li>";
$line .= trim($matches[1]);
$line .= "</li>";
}
} else {
$isBold = false;
$isItalic = false;
if (preg_match('/(.*)__(.*)__(.*)/', $matches[1], $matches2)) {
$matches[1] = $matches2[1] . '<em>' . $matches2[2] . '</em>' . $matches2[3];
$matches[1] = $matches2[1] . '<strong>' . $matches2[2] . '</strong>' . $matches2[3];
$isBold = true;
}

if (preg_match('/(.*)_(.*)_(.*)/', $matches[1], $matches3)) {
$matches[1] = $matches3[1] . '<i>' . $matches3[2] . '</i>' . $matches3[3];
$matches[1] = $matches3[1] . '<em>' . $matches3[2] . '</em>' . $matches3[3];
$isItalic = true;
}

if ($isItalic || $isBold) {
$line = "<li>" . trim($matches[1]) . "</li>";
} else {
$line = "<li><p>" . trim($matches[1]) . "</p></li>";
$line = "<li>";
$line .= trim($matches[1]);
$line .= "</li>";
}
}
} else {
Expand All @@ -86,23 +99,24 @@ function parseMarkdown($markdown)
}

if (!preg_match('/<h|<ul|<p|<li/', $line)) {
$line = "<p>$line</p>";
if (preg_match('/^<\/ul>(.*)/', $line, $matches)) {
$line = "</ul><p>" . trim($matches[1]) . "</p>";
} else {
$line = "<p>$line</p>";
}
}

if (preg_match('/(.*)__(.*)__(.*)/', $line, $matches)) {
$line = $matches[1] . '<em>' . $matches[2] . '</em>' . $matches[3];
$line = $matches[1] . '<strong>' . $matches[2] . '</strong>' . $matches[3];
}

if (preg_match('/(.*)_(.*)_(.*)/', $line, $matches)) {
$line = $matches[1] . '<i>' . $matches[2] . '</i>' . $matches[3];
$line = $matches[1] . '<em>' . $matches[2] . '</em>' . $matches[3];
}
}

$html = join($lines);

if ($isInList) {
$html .= '</ul>';
}

return $html;
}
30 changes: 27 additions & 3 deletions exercises/practice/markdown/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[e75c8103-a6b8-45d9-84ad-e68520545f6e]
description = "parses normal text as a paragraph"
Expand All @@ -20,9 +27,26 @@ description = "with h1 header level"
[d0f7a31f-6935-44ac-8a9a-1e8ab16af77f]
description = "with h2 header level"

[9df3f500-0622-4696-81a7-d5babd9b5f49]
description = "with h3 header level"

[50862777-a5e8-42e9-a3b8-4ba6fcd0ed03]
description = "with h4 header level"

[ee1c23ac-4c86-4f2a-8b9c-403548d4ab82]
description = "with h5 header level"

[13b5f410-33f5-44f0-a6a7-cfd4ab74b5d5]
description = "with h6 header level"

[6dca5d10-5c22-4e2a-ac2b-bd6f21e61939]
description = "with h7 header level"
include = false

[81c0c4db-435e-4d77-860d-45afacdad810]
description = "h7 header level is a paragraph"
reimplements = "6dca5d10-5c22-4e2a-ac2b-bd6f21e61939"

[25288a2b-8edc-45db-84cf-0b6c6ee034d6]
description = "unordered lists"

Expand Down
43 changes: 30 additions & 13 deletions exercises/practice/markdown/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,51 +31,64 @@ function parseMarkdown($markdown)
$isInList = false;

foreach ($lines as &$line) {
if (preg_match('/^######(.*)/', $line, $matches)) {
if (preg_match('/^####### (.*)/', $line, $matches)) {
$line = "<p>" . trim($matches[0]) . "</p>";
} elseif (preg_match('/^###### (.*)/', $line, $matches)) {
$line = "<h6>" . trim($matches[1]) . "</h6>";
} elseif (preg_match('/^##(.*)/', $line, $matches)) {
} elseif (preg_match('/^##### (.*)/', $line, $matches)) {
$line = "<h5>" . trim($matches[1]) . "</h5>";
} elseif (preg_match('/^#### (.*)/', $line, $matches)) {
$line = "<h4>" . trim($matches[1]) . "</h4>";
} elseif (preg_match('/^### (.*)/', $line, $matches)) {
$line = "<h3>" . trim($matches[1]) . "</h3>";
} elseif (preg_match('/^## (.*)/', $line, $matches)) {
$line = "<h2>" . trim($matches[1]) . "</h2>";
} elseif (preg_match('/^#(.*)/', $line, $matches)) {
} elseif (preg_match('/^# (.*)/', $line, $matches)) {
$line = "<h1>" . trim($matches[1]) . "</h1>";
}

if (preg_match('/\*(.*)/', $line, $matches)) {
if (preg_match('/^\s*\*(.*)/', $line, $matches)) {
if (!$isInList) {
$isInList = true;
$isBold = false;
$isItalic = false;
if (preg_match('/(.*)__(.*)__(.*)/', $matches[1], $matches2)) {
$matches[1] = $matches2[1] . '<em>' . $matches2[2] . '</em>' . $matches2[3];
$matches[1] = $matches2[1] . '<strong>' . $matches2[2] . '</strong>' . $matches2[3];
$isBold = true;
}

if (preg_match('/(.*)_(.*)_(.*)/', $matches[1], $matches3)) {
$matches[1] = $matches3[1] . '<i>' . $matches3[2] . '</i>' . $matches3[3];
$matches[1] = $matches3[1] . '<em>' . $matches3[2] . '</em>' . $matches3[3];
$isItalic = true;
}

if ($isItalic || $isBold) {
$line = "<ul><li>" . trim($matches[1]) . "</li>";
} else {
$line = "<ul><li><p>" . trim($matches[1]) . "</p></li>";
$line = "<ul>";
$line .= "<li>";
$line .= trim($matches[1]);
$line .= "</li>";
}
} else {
$isBold = false;
$isItalic = false;
if (preg_match('/(.*)__(.*)__(.*)/', $matches[1], $matches2)) {
$matches[1] = $matches2[1] . '<em>' . $matches2[2] . '</em>' . $matches2[3];
$matches[1] = $matches2[1] . '<strong>' . $matches2[2] . '</strong>' . $matches2[3];
$isBold = true;
}

if (preg_match('/(.*)_(.*)_(.*)/', $matches[1], $matches3)) {
$matches[1] = $matches3[1] . '<i>' . $matches3[2] . '</i>' . $matches3[3];
$matches[1] = $matches3[1] . '<em>' . $matches3[2] . '</em>' . $matches3[3];
$isItalic = true;
}

if ($isItalic || $isBold) {
$line = "<li>" . trim($matches[1]) . "</li>";
} else {
$line = "<li><p>" . trim($matches[1]) . "</p></li>";
$line = "<li>";
$line .= trim($matches[1]);
$line .= "</li>";
}
}
} else {
Expand All @@ -86,15 +99,19 @@ function parseMarkdown($markdown)
}

if (!preg_match('/<h|<ul|<p|<li/', $line)) {
$line = "<p>$line</p>";
if (preg_match('/^<\/ul>(.*)/', $line, $matches)) {
$line = "</ul><p>" . trim($matches[1]) . "</p>";
} else {
$line = "<p>$line</p>";
}
}

if (preg_match('/(.*)__(.*)__(.*)/', $line, $matches)) {
$line = $matches[1] . '<em>' . $matches[2] . '</em>' . $matches[3];
$line = $matches[1] . '<strong>' . $matches[2] . '</strong>' . $matches[3];
}

if (preg_match('/(.*)_(.*)_(.*)/', $line, $matches)) {
$line = $matches[1] . '<i>' . $matches[2] . '</i>' . $matches[3];
$line = $matches[1] . '<em>' . $matches[2] . '</em>' . $matches[3];
}
}
$html = join($lines);
Expand Down
Loading
Loading