|
| 1 | +defmodule Markdown do |
| 2 | + @bold_md "__" |
| 3 | + @header_md "#" |
| 4 | + @italic_md "_" |
| 5 | + @list_md "*" |
| 6 | + |
| 7 | + @bold_open_tag "<em>" |
| 8 | + @bold_close_tag "</em>" |
| 9 | + @italic_open_tag "<i>" |
| 10 | + @italic_close_tag "</i>" |
| 11 | + @unordered_list_open_tag "<ul>" |
| 12 | + @unordered_list_close_tag "</ul>" |
| 13 | + @list_item_open_tag "<li>" |
| 14 | + @list_item_close_tag "</li>" |
| 15 | + @paragraph_open_tag "<p>" |
| 16 | + @paragraph_close_tag "</p>" |
| 17 | + @header_partial_open_tag "<h" |
| 18 | + @header_partial_close_tag "</h" |
| 19 | + |
| 20 | + def parse(markdown) do |
| 21 | + String.split(markdown, "\n") |> parse_line |
| 22 | + end |
| 23 | + |
| 24 | + defp parse_line(markdown_split_by_newline) do |
| 25 | + Enum.map_join(markdown_split_by_newline, fn(text_with_md) -> process_md(text_with_md) end) |> patch_line_with_unordered_list_tag |
| 26 | + end |
| 27 | + |
| 28 | + defp process_md(text_with_md) do |
| 29 | + cond do |
| 30 | + String.starts_with?(text_with_md, "#") -> parse_header_md_level(text_with_md) |> enclose_with_header_tag |
| 31 | + String.starts_with?(text_with_md, "*") -> parse_list_md_level(text_with_md) |
| 32 | + true -> String.split(text_with_md) |> enclose_with_paragraph_tag |
| 33 | + end |
| 34 | + end |
| 35 | + |
| 36 | + defp parse_header_md_level(header_md_with_text) do |
| 37 | + [header_md | header_text] = String.split(header_md_with_text) |
| 38 | + {to_string(String.length(header_md)), Enum.join(header_text, " ")} |
| 39 | + end |
| 40 | + |
| 41 | + defp parse_list_md_level(list_md_with_text) do |
| 42 | + list_text = String.trim_leading(list_md_with_text, "* ") |> String.split |
| 43 | + @list_item_open_tag <> join_words_with_tags(list_text) <> @list_item_close_tag |
| 44 | + end |
| 45 | + |
| 46 | + defp enclose_with_header_tag({header_level, header_text_line}) do |
| 47 | + @header_partial_open_tag <> header_level <> ">" <> header_text_line <> @header_partial_close_tag <> header_level <> ">" |
| 48 | + end |
| 49 | + |
| 50 | + defp enclose_with_paragraph_tag(text_with_md) do |
| 51 | + @paragraph_open_tag <> join_words_with_tags(text_with_md) <> @paragraph_close_tag |
| 52 | + end |
| 53 | + |
| 54 | + defp join_words_with_tags(text_with_md) do |
| 55 | + Enum.map_join(text_with_md, " ", fn(word_with_md) -> replace_md_with_tag(word_with_md) end) |
| 56 | + end |
| 57 | + |
| 58 | + defp replace_md_with_tag(word_with_md) do |
| 59 | + replace_prefix_md(word_with_md) |> replace_suffix_md |
| 60 | + end |
| 61 | + |
| 62 | + defp replace_prefix_md(word_with_md) do |
| 63 | + cond do |
| 64 | + word_with_md =~ ~r/^[#{@italic_md}{1}][^#{@italic_md}+]/ -> String.replace_prefix(word_with_md, @italic_md, @italic_open_tag) |
| 65 | + word_with_md =~ ~r/^#{@bold_md}{1}/ -> String.replace_prefix(word_with_md, @bold_md, @bold_open_tag) |
| 66 | + true -> word_with_md |
| 67 | + end |
| 68 | + end |
| 69 | + |
| 70 | + defp replace_suffix_md(word_with_md) do |
| 71 | + cond do |
| 72 | + word_with_md =~ ~r/[^#{@italic_md}{1}][#{@italic_md}{1}]$/ -> String.replace_suffix(word_with_md, @italic_md, @italic_close_tag) |
| 73 | + word_with_md =~ ~r/#{@bold_md}{1}$/ -> String.replace_suffix(word_with_md, @bold_md, @bold_close_tag) |
| 74 | + true -> word_with_md |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + defp patch_line_with_unordered_list_tag(html_line) do |
| 79 | + String.replace(html_line, @list_item_open_tag, @unordered_list_open_tag <> @list_item_open_tag, global: false) |> String.replace_suffix(@list_item_close_tag, @list_item_close_tag <> @unordered_list_close_tag) |
| 80 | + end |
| 81 | +end |
0 commit comments