🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of kayhide

kayhide's solution

to Markdown in the Elixir Track

Published at Jun 14 2020 · 0 comments
Instructions
Test suite
Solution

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!

Running tests

Execute the tests with:

$ mix test

Pending tests

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.

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

markdown_test.exs

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

test_helper.exs

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule Functor do
  # f a -> (a -> b) -> f b
  @callback fmap(term, (term -> term)) :: term
end

defmodule Applicative do
  import Functor
  # a -> f a
  @callback pure(term) :: term
  # f a -> f (a -> b) -> f b
  @callback app(term, term) :: term
end

defmodule Alternative do
  import Functor
  # f a
  @callback empty() :: term
  # f a -> f a -> f a
  @callback alt(term, term) :: term

  @doc """
  Implemented in terms of Monad
  """
  # m a -> m [a]
  @spec some(module, term) :: term
  def some(impl, p) do
    p
    |> impl.bind(fn x ->
      impl.alt(impl.fmap(some(impl, p), &[x | &1]), impl.pure([x]))
    end)
  end

  # [m a] -> m a
  @spec asum(module, [term]) :: term
  def asum(impl, ps), do: Enum.reduce(ps, impl.empty(), &impl.alt(&1, &2))
end

defmodule Monad do
  import Applicative
  @callback bind(term, term) :: term
end

defmodule Parser do
  @behaviour Functor
  @behaviour Applicative
  @behaviour Alternative
  @behaviour Monad

  @type t(a) :: %__MODULE__{run: (String.t() -> {a, String.t()} | :fail)}

  defstruct run: &Parser.fail/1

  @spec fail(String.t()) :: :fail
  def fail(_), do: :fail

  @spec run_parser(t(String.t()), String.t()) :: String.t() | :fail
  def run_parser(%Parser{run: run}, str) do
    case run.(str) do
      :fail -> :fail
      {res, _} -> res
    end
  end

  @impl Functor
  @spec fmap(t(a), (a -> b)) :: t(b) when a: term, b: term
  def fmap(%Parser{run: run}, f) do
    %Parser{
      run: fn str ->
        case run.(str) do
          :fail -> :fail
          {y, str_} -> {f.(y), str_}
        end
      end
    }
  end

  @impl Applicative
  def pure(x) do
    %Parser{
      run: fn str -> {x, str} end
    }
  end

  @impl Applicative
  def app(%Parser{run: x}, %Parser{run: f}) do
    %Parser{
      run: fn str -> {f.(x), str} end
    }
  end

  @impl Alternative
  def empty(), do: %Parser{}

  @impl Alternative
  def alt(%Parser{run: p1}, %Parser{run: p2}) do
    %Parser{
      run: fn str ->
        case p1.(str) do
          :fail -> p2.(str)
          res -> res
        end
      end
    }
  end

  @impl Monad
  def bind(%Parser{run: run}, act) do
    %Parser{
      run: fn str ->
        case run.(str) do
          :fail ->
            :fail

          {y, str_} ->
            case act.(y) do
              %Parser{run: run2} -> run2.(str_)
            end
        end
      end
    }
  end
end

defmodule PrimitiveParsers do
  @moduledoc """
  Primitive and helper parsers
  """

  import Alternative
  import Monad
  import Parser

  @spec satisfy((String.t() -> boolean)) :: Parser.t(String.t())
  def satisfy(pred) do
    %Parser{
      run: fn str ->
        {x, xs} = String.split_at(str, 1)
        if pred.(x), do: {x, xs}, else: :fail
      end
    }
  end

  @spec chunk(String.t()) :: Parser.t(String.t())
  def chunk(x) do
    %Parser{
      run: fn str ->
        case String.split_at(str, String.length(x)) do
          {^x, xs} -> {x, xs}
          _ -> :fail
        end
      end
    }
  end

  @spec eof() :: Parser.t(nil)
  def eof() do
    %Parser{
      run: fn
        "" -> {nil, ""}
        _ -> :fail
      end
    }
  end

  @spec symbol(Parser.t(String.t())) :: Parser.t(String.t())
  def symbol(p) do
    p
    |> bind(fn x ->
      some(Parser, chunk(" "))
      |> fmap(fn _ -> x end)
    end)
  end

  @spec eol() :: Parser.t(nil)
  def eol() do
    alt(chunk("\n"), eof())
  end

  @spec between(Parser.t(String.t()), Parser.t(String.t())) :: Parser.t(String.t())
  def between(p1, p2) do
    p1
    |> bind(fn _ ->
      p2
      |> bind(fn x ->
        p1
        |> fmap(fn _ -> x end)
      end)
    end)
  end
end

defmodule Markdown do
  import Alternative
  import Parser
  import PrimitiveParsers

  @doc """
  Parses a given string with Markdown syntax and returns the associated HTML for that string.

  ## Examples

  iex> Markdown.parse("This is a paragraph")
  "<p>This is a paragraph</p>"

  iex> Markdown.parse("#Header!\n* __Bold Item__\n* _Italic Item_")
  "<h1>Header!</h1><ul><li><em>Bold Item</em></li><li><i>Italic Item</i></li></ul>"
  """
  @spec parse(String.t()) :: String.t()
  def parse(m) do
    run_parser(document(), m)
  end

  defp document() do
    some(Parser, asum(Parser, [header(), list(), paragraph()]))
    |> fmap(&Enum.join/1)
  end

  defp header() do
    symbol(some(Parser, chunk("#")))
    |> bind(fn n ->
      line()
      |> fmap(tag("h#{length(n)}"))
    end)
  end

  defp list() do
    some(Parser, list_item())
    |> fmap(&tag("ul").(Enum.join(&1)))
  end

  defp list_item() do
    symbol(chunk("*"))
    |> bind(fn _ ->
      line()
      |> fmap(tag("li"))
    end)
  end

  defp paragraph() do
    line()
    |> fmap(tag("p"))
  end

  defp strong() do
    between(chunk("__"), text())
    |> fmap(tag("strong"))
  end

  defp italic() do
    between(chunk("_"), text())
    |> fmap(tag("em"))
  end

  defp line() do
    some(Parser, asum(Parser, [strong(), italic(), text()]))
    |> bind(fn x ->
      eol()
      |> fmap(fn _ -> x end)
    end)
  end

  defp text() do
    some(Parser, satisfy(&String.match?(&1, ~r/[0-9a-zA-Z !]/)))
    |> fmap(&Enum.join/1)
  end

  @spec tag(String.t()) :: (String.t() -> String.t())
  defp tag(key) do
    fn text ->
      "<#{key}>#{text}</#{key}>"
    end
  end
end

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

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.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?