Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

68 lines (60 sloc) 3.098 kb
-module(jerome_html_generator).
-export([generate/1]).
generate(Ast) ->
generate(Ast, []).
generate([], Acc) ->
lists:reverse(Acc);
generate([{text, Text, Properties}|Rest], Acc) ->
generate(Rest, [write_attributed_text(Text, Properties)|Acc]);
generate([{table, Rows}|Rest], Acc) ->
generate(Rest, lists:reverse(["<table>", generate(Rows, []), "</table>"], Acc));
generate([{table_row, Ast}|Rest], Acc) ->
generate(Rest, lists:reverse(["<tr>", generate(Ast, []), "</tr>"], Acc));
generate([{table_cell, Ast}|Rest], Acc) ->
generate(Rest, lists:reverse(["<td>", generate(Ast, []), "</td>"], Acc));
generate([{paragraph, _}|Rest], Acc) ->
generate(Rest, ["<br>"|Acc]);
generate([{list, ListItems}|Rest], Acc) ->
generate(Rest, lists:reverse(["<ul>", generate(ListItems, []), "</ul>"], Acc));
generate([{list_item, ListItem}|Rest], Acc) ->
generate(Rest, lists:reverse(["<li>", generate(ListItem, []), "</li>"], Acc));
generate([{blockquote, Ast}|Rest], Acc) ->
generate(Rest, lists:reverse(["<blockquote>", generate(Ast, []), "</blockquote>"], Acc));
generate([{blockcode, Ast}|Rest], Acc) -> % TODO
generate(Rest, lists:reverse(["<code><pre>", generate(Ast, []), "</pre></code>"], Acc));
generate([{preformatted, Ast}|Rest], Acc) ->
generate(Rest, lists:reverse(["<pre>", generate(Ast, []), "</pre>"], Acc));
generate([{heading, Level, Ast}|Rest], Acc) ->
generate(Rest, lists:reverse(["<h", integer_to_list(Level), ">", generate(Ast, []),
"</h", integer_to_list(Level), ">"], Acc));
generate([{image, ImageURL}|Rest], Acc) when is_list(ImageURL) ->
generate(Rest, lists:reverse(["<img src=\"", ImageURL, "\" />"], Acc));
generate([{image, ImageBinary}|Rest], Acc) when is_binary(ImageBinary) ->
generate(Rest, lists:reverse(["<img src=\"data:", jerome:image_mime_type(ImageBinary),
";base64,", base64:encode_to_string(ImageBinary), "\" />"], Acc)).
write_attributed_text(Text, [bold|Rest]) ->
["<strong>", write_attributed_text(Text, Rest), "</strong>"];
write_attributed_text(Text, [italic|Rest]) ->
["<em>", write_attributed_text(Text, Rest), "</em>"];
write_attributed_text(Text, [underline|Rest]) ->
["<u>", write_attributed_text(Text, Rest), "</u>"];
write_attributed_text(Text, [superscript|Rest]) ->
["<sup>", write_attributed_text(Text, Rest), "</sup>"];
write_attributed_text(Text, [subscript|Rest]) ->
["<sub>", write_attributed_text(Text, Rest), "</sub>"];
write_attributed_text(Text, [{hyperlink, Destination}|Rest]) ->
["<a href=\"", Destination, "\">", write_attributed_text(Text, Rest), "</a>"];
write_attributed_text(Text, []) ->
write_text(Text).
write_text(Text) ->
write_text(Text, []).
write_text([], Acc) ->
lists:reverse(Acc);
write_text([H|T], Acc) when H > 127 ->
write_text(T, lists:reverse("&#"++integer_to_list(H)++";", Acc));
write_text([$<|T], Acc) ->
write_text(T, lists:reverse("&lt;", Acc));
write_text([$>|T], Acc) ->
write_text(T, lists:reverse("&gt;", Acc));
write_text([H|T], Acc) ->
write_text(T, [H|Acc]).
Jump to Line
Something went wrong with that request. Please try again.