Refactor a Markdown parser.
The markdown exercise is a refactoring exercise. There is code that parses a given string with Markdown syntax 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!
Execute the tests with:
$ elixir markdown_test.exs
In the test suites, all but the first test have been skipped.
Once you get a test passing, you can unskip the next one by
commenting out the relevant @tag :pending
with a #
symbol.
For example:
# @tag :pending
test "shouting" do
assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end
Or, you can enable all the tests by commenting out the
ExUnit.configure
line in the test suite.
# ExUnit.configure exclude: :pending, trace: true
If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
if !System.get_env("EXERCISM_TEST_EXAMPLES") do
Code.load_file("markdown.exs", __DIR__)
end
ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule MarkdownTest do
use ExUnit.Case
# @tag :pending
test "parses normal text as a paragraph" do
input = "This will be a paragraph"
expected = "<p>This will be a paragraph</p>"
assert Markdown.parse(input) == expected
end
@tag :pending
test "parsing italics" do
input = "_This will be italic_"
expected = "<p><em>This will be italic</em></p>"
assert Markdown.parse(input) == expected
end
@tag :pending
test "parsing bold text" do
input = "__This will be bold__"
expected = "<p><strong>This will be bold</strong></p>"
assert Markdown.parse(input) == expected
end
@tag :pending
test "mixed normal, italics and bold text" do
input = "This will _be_ __mixed__"
expected = "<p>This will <em>be</em> <strong>mixed</strong></p>"
assert Markdown.parse(input) == expected
end
@tag :pending
test "with h1 header level" do
input = "# This will be an h1"
expected = "<h1>This will be an h1</h1>"
assert Markdown.parse(input) == expected
end
@tag :pending
test "with h2 header level" do
input = "## This will be an h2"
expected = "<h2>This will be an h2</h2>"
assert Markdown.parse(input) == expected
end
@tag :pending
test "with h6 header level" do
input = "###### This will be an h6"
expected = "<h6>This will be an h6</h6>"
assert Markdown.parse(input) == expected
end
@tag :pending
test "unordered lists" do
input = "* Item 1\n* Item 2"
expected = "<ul><li>Item 1</li><li>Item 2</li></ul>"
assert Markdown.parse(input) == expected
end
@tag :pending
test "with a little bit of everything" do
input = "# Header!\n* __Bold Item__\n* _Italic Item_"
expected =
"<h1>Header!</h1><ul><li><strong>Bold Item</strong></li><li><em>Italic Item</em></li></ul>"
assert Markdown.parse(input) == expected
end
end
defmodule Markdown do
@spec parse(String.t()) :: String.t()
def parse(text) do
if String.contains?(text, "\n") do
text
|> String.split("\n")
|> process()
else
process(text)
end
end
defp process(text) when is_list(text) do
text
|> Enum.map_join(&process/1)
|> String.replace("<li>", "<ul><li>", global: false)
|> String.replace_trailing("</li>", "</li></ul>")
end
defp process(<<"# ", text::binary>>) do
"<h1>#{text}</h1>"
end
defp process(<<"## ", text::binary>>) do
"<h2>#{text}</h2>"
end
defp process(<<"###### ", text::binary>>) do
"<h6>#{text}</h6>"
end
defp process(<<"* ", text::binary>>) do
"<li>#{replace_with_tags(text)}</li>"
end
defp process(text) do
"<p>#{replace_with_tags(text)}</p>"
end
defp replace_with_tags(text) do
text
|> String.split(" ")
|> Enum.map_join(" ", &replace_markdown/1)
end
defp replace_markdown(word) do
cond do
String.contains?(word, "__") ->
word |> String.replace_leading("__", "<strong>") |> String.replace_trailing("__", "</strong>")
String.contains?(word, "_") ->
word |> String.replace_leading("_", "<em>") |> String.replace_trailing("_", "</em>")
true ->
word
end
end
end
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments