%%% render module is responsible for rendering Markdown documents parsed %%% by marker:marker function. -module(render). -include_lib("eunit/include/eunit.hrl"). -export([html/1]). %% tags are opening and closing tags for (most) block (non-leaf) elements %% of the parsing tree. tags() -> #{ heading1 => #{open => "

", close => "

"}, heading2 => #{open => "

", close => "

"}, heading3 => #{open => "

", close => "

"}, heading4 => #{open => "

", close => "

"}, heading5 => #{open => "
", close => "
"}, heading6 => #{open => "
", close => "
"}, paragraph => #{open => "

", close => "

"}, horizontal_line => #{open => "
", close => ""}, block_quote => #{open => "
", close => "
"}, bullet_list => #{open => ""}, ordered_list => #{open => "
    ", close => "
"}, list_item => #{open => "
  • ", close => "
  • "}, document => #{open => "
    ", close => "
    "}, soft_break => #{open => "", close => "
    "} }. %% html/1 generates HTML elements from the nodes of the parsing tree %% generated by marker:markdown/1 function. %% %% The process is quite straightforward as the content of the tree nodes %% must be just wrapped with the correct tags. html({str, Text}) -> Text; html({italic, Text}) -> "" ++ Text ++ ""; html({emph, Text}) -> "" ++ Text ++ ""; html({code_fence, Text}) -> "
    " ++ Text ++ "
    "; html({inline_code, Text}) -> "" ++ Text ++ ""; html({Type, Blocks}) -> OpenTag = maps:get(open, maps:get(Type, tags())), CloseTag = maps:get(close, maps:get(Type, tags())), OpenTag ++ lists:flatten(lists:map(fun html/1, Blocks)) ++ CloseTag. html_test() -> ?assertEqual("
    ", html({document, []})), ?assertEqual("

    foo

    ", html({document, [{paragraph, [{str, "foo"}]}]})), ?assertEqual("
    ", html({document, [{bullet_list, [{list_item, []}]}]})).