Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 68 lines (60 sloc) 3.098 kb
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
1 -module(jerome_html_generator).
2
3 -export([generate/1]).
4
5
6 generate(Ast) ->
7 generate(Ast, []).
8
9 generate([], Acc) ->
10 lists:reverse(Acc);
11 generate([{text, Text, Properties}|Rest], Acc) ->
12 generate(Rest, [write_attributed_text(Text, Properties)|Acc]);
13 generate([{table, Rows}|Rest], Acc) ->
14 generate(Rest, lists:reverse(["<table>", generate(Rows, []), "</table>"], Acc));
15 generate([{table_row, Ast}|Rest], Acc) ->
16 generate(Rest, lists:reverse(["<tr>", generate(Ast, []), "</tr>"], Acc));
17 generate([{table_cell, Ast}|Rest], Acc) ->
18 generate(Rest, lists:reverse(["<td>", generate(Ast, []), "</td>"], Acc));
19 generate([{paragraph, _}|Rest], Acc) ->
20 generate(Rest, ["<br>"|Acc]);
21 generate([{list, ListItems}|Rest], Acc) ->
22 generate(Rest, lists:reverse(["<ul>", generate(ListItems, []), "</ul>"], Acc));
23 generate([{list_item, ListItem}|Rest], Acc) ->
24 generate(Rest, lists:reverse(["<li>", generate(ListItem, []), "</li>"], Acc));
25 generate([{blockquote, Ast}|Rest], Acc) ->
26 generate(Rest, lists:reverse(["<blockquote>", generate(Ast, []), "</blockquote>"], Acc));
27 generate([{blockcode, Ast}|Rest], Acc) -> % TODO
28 generate(Rest, lists:reverse(["<code><pre>", generate(Ast, []), "</pre></code>"], Acc));
29 generate([{preformatted, Ast}|Rest], Acc) ->
30 generate(Rest, lists:reverse(["<pre>", generate(Ast, []), "</pre>"], Acc));
31 generate([{heading, Level, Ast}|Rest], Acc) ->
6d3a263 @evanmiller Support for in-line and external images.
authored
32 generate(Rest, lists:reverse(["<h", integer_to_list(Level), ">", generate(Ast, []),
33 "</h", integer_to_list(Level), ">"], Acc));
34 generate([{image, ImageURL}|Rest], Acc) when is_list(ImageURL) ->
35 generate(Rest, lists:reverse(["<img src=\"", ImageURL, "\" />"], Acc));
36 generate([{image, ImageBinary}|Rest], Acc) when is_binary(ImageBinary) ->
37 generate(Rest, lists:reverse(["<img src=\"data:", jerome:image_mime_type(ImageBinary),
38 ";base64,", base64:encode_to_string(ImageBinary), "\" />"], Acc)).
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
39
40 write_attributed_text(Text, [bold|Rest]) ->
41 ["<strong>", write_attributed_text(Text, Rest), "</strong>"];
42 write_attributed_text(Text, [italic|Rest]) ->
43 ["<em>", write_attributed_text(Text, Rest), "</em>"];
41ee5fa HTML parser
Evan Miller authored
44 write_attributed_text(Text, [underline|Rest]) ->
45 ["<u>", write_attributed_text(Text, Rest), "</u>"];
46 write_attributed_text(Text, [superscript|Rest]) ->
47 ["<sup>", write_attributed_text(Text, Rest), "</sup>"];
48 write_attributed_text(Text, [subscript|Rest]) ->
49 ["<sub>", write_attributed_text(Text, Rest), "</sub>"];
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
50 write_attributed_text(Text, [{hyperlink, Destination}|Rest]) ->
51 ["<a href=\"", Destination, "\">", write_attributed_text(Text, Rest), "</a>"];
52 write_attributed_text(Text, []) ->
53 write_text(Text).
54
55 write_text(Text) ->
56 write_text(Text, []).
57
58 write_text([], Acc) ->
59 lists:reverse(Acc);
60 write_text([H|T], Acc) when H > 127 ->
61 write_text(T, lists:reverse("&#"++integer_to_list(H)++";", Acc));
41ee5fa HTML parser
Evan Miller authored
62 write_text([$<|T], Acc) ->
63 write_text(T, lists:reverse("&lt;", Acc));
64 write_text([$>|T], Acc) ->
65 write_text(T, lists:reverse("&gt;", Acc));
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
66 write_text([H|T], Acc) ->
67 write_text(T, [H|Acc]).
Something went wrong with that request. Please try again.