Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 63 lines (50 sloc) 2.487 kB
4af4b7b @evanmiller First commit
authored
1 -module(jerome).
2
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
3 -include("jerome.hrl").
4
4af4b7b @evanmiller First commit
authored
5 -compile(export_all).
6
7 % Jerome - a rich-text reader/writer
8
6d3a263 @evanmiller Support for in-line and external images.
authored
9 parse(Path, Format) ->
10 parse(Path, Format, fun(Img) -> {ok, Img} end).
11
12 parse(Path, Format, ImageFun) when is_list(Path) ->
4af4b7b @evanmiller First commit
authored
13 {ok, Binary} = file:read_file(Path),
6d3a263 @evanmiller Support for in-line and external images.
authored
14 parse(Binary, Format, ImageFun);
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
15
6d3a263 @evanmiller Support for in-line and external images.
authored
16 parse(Binary, Format, ImageFun) when is_binary(Binary) ->
4af4b7b @evanmiller First commit
authored
17 case Format of
6d3a263 @evanmiller Support for in-line and external images.
authored
18 bbcode -> jerome_bbcode_consumer:consume(Binary, ImageFun);
19 rtf -> jerome_rtf_consumer:consume(Binary, ImageFun);
20 textile -> jerome_textile_consumer:consume(Binary, ImageFun)
4af4b7b @evanmiller First commit
authored
21 end.
22
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
23 generate(Ast, Format) ->
24 case Format of
6d3a263 @evanmiller Support for in-line and external images.
authored
25 bbcode -> jerome_bbcode_generator:generate(Ast);
26 html -> jerome_html_generator:generate(Ast);
27 rtf -> jerome_rtf_generator:generate(Ast);
28 textile -> jerome_textile_generator:generate(Ast)
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
29 end.
30
31 consolidate(List) ->
32 consolidate(List, []).
33
34 consolidate([], Acc) ->
35 lists:reverse(Acc);
36 consolidate([{text, Text2, Props1}|T], [{text, Text1, Props1}|Acc]) ->
37 consolidate(T, [{text, Text1 ++ Text2, Props1}|Acc]);
38 consolidate([H|T], Acc) ->
39 consolidate(T, [H|Acc]).
40
41 text_properties(#jerome_ctx{ italic = true } = Ctx) ->
42 [italic] ++ text_properties(Ctx#jerome_ctx{ italic = false });
43 text_properties(#jerome_ctx{ bold = true } = Ctx) ->
44 [bold] ++ text_properties(Ctx#jerome_ctx{ bold = false });
45 text_properties(#jerome_ctx{ underline = true } = Ctx) ->
46 [underline] ++ text_properties(Ctx#jerome_ctx{ underline = false });
47 text_properties(#jerome_ctx{ hyperlink = Link } = Ctx) when Link =/= undefined ->
48 [{hyperlink, Link}] ++ text_properties(Ctx#jerome_ctx{ hyperlink = undefined });
49 text_properties(#jerome_ctx{ superscript = true } = Ctx) ->
50 [superscript] ++ text_properties(Ctx#jerome_ctx{ superscript = false });
51 text_properties(#jerome_ctx{ subscript = true } = Ctx) ->
52 [subscript] ++ text_properties(Ctx#jerome_ctx{ subscript = false });
53 text_properties(_) -> [].
54
6d3a263 @evanmiller Support for in-line and external images.
authored
55 image_mime_type(<<137, $P, $N, $G,$\r, $\n, 26, $\n, _/binary>>) -> "image/png";
56 image_mime_type(<<16#FF, 16#D8, 16#FF, 16#E0, _/binary>>) -> "image/jpeg";
57 image_mime_type(<<16#FF, 16#D8, 16#FF, 16#E1, _/binary>>) -> "image/jpeg";
58 image_mime_type(<<$G, $I, $F, $8, $7, $a>>) -> "image/gif";
59 image_mime_type(<<$G, $I, $F, $8, $9, $a>>) -> "image/gif";
60 image_mime_type(<<(16#4949):16, (42):16/little, _/binary>>) -> "image/tiff";
61 image_mime_type(<<(16#4D4D):16, (42):16/big, _/binary>>) -> "image/tiff".
62
Something went wrong with that request. Please try again.