Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 59 lines (52 sloc) 2.601 kb
feb181a2 »
2011-06-09 First cut: R/W Textile and BBCode
1 -module(jerome_rtf_generator).
2
3 -export([generate/1]).
4
5 generate(Ast) ->
6 ["{\\rtf1\\ansi\\ansicpg1252\n",
7 "{\\fonttbl\\f0\\fswiss\\fcharset0 Helvetica;}\n",
8 "{\\colortbl;\\red255\\green255\\blue255;}\n",
9 "\\pard\\pardirnatural\\uc0\n", generate(Ast, []), "}"].
10
11 generate([], Acc) ->
12 lists:reverse(Acc);
13 generate([{text, Text, Properties}|Rest], Acc) ->
14 generate(Rest, [write_attributed_text(Text, Properties)|Acc]);
15 generate([{table, Rows}|Rest], Acc) ->
16 generate(Rest, [generate(Rows, [])|Acc]);
17 generate([{table_row, Ast}|Rest], Acc) ->
18 generate(Rest, lists:reverse(["\\trowd ", generate(Ast, []), "\\row\n"], Acc));
19 generate([{table_cell, Ast}|Rest], Acc) ->
20 generate(Rest, lists:reverse(["\\pard\\intbl ", generate(Ast, []), "\\cell\n"], Acc));
21 generate([{list, ListItems}|Rest], Acc) ->
22 generate(Rest, [generate(ListItems, [])|Acc]);
23 generate([{list_item, ListItem}|Rest], Acc) ->
24 generate(Rest, lists:reverse(["{\\listtext\\uc0\\u9642}", generate(ListItem, [])], Acc));
25 generate([{paragraph, _}|Rest], Acc) ->
6d3a263f »
2011-06-14 Support for in-line and external images.
26 generate(Rest, ["\\\n"|Acc]);
27 generate([{image, ImageBinary}|Rest], Acc) when is_binary(ImageBinary) ->
28 {PictType, Width, Height} = image_info(ImageBinary),
29 generate(Rest, lists:reverse(["{\\*\\shppict {\\pict ",
30 "\\picw", integer_to_list(Width), "\\pich", integer_to_list(Height),
31 PictType, "\\bin", integer_to_list(byte_size(ImageBinary)), " ",
32 ImageBinary, "}}"], Acc)).
33
25dbad17 » Evan Miller
2011-06-25 Bug-fixes in RTF image processing
34 image_info(<<137, $P, $N, $G, $\r, $\n, 26, $\n, _Length:32, $I, $H, $D, $R, Width:32, Height:32, _/binary>>) ->
6d3a263f »
2011-06-14 Support for in-line and external images.
35 {"\\pngblip", Width, Height}.
feb181a2 »
2011-06-09 First cut: R/W Textile and BBCode
36
37 write_attributed_text(Text, [bold|Rest]) ->
38 ["\\b ", write_attributed_text(Text, Rest), "\\b0 "];
39 write_attributed_text(Text, [italic|Rest]) ->
40 ["\\i ", write_attributed_text(Text, Rest), "\\i0 "];
41 write_attributed_text(Text, [superscript|Rest]) ->
42 ["\\super ", write_attributed_text(Text, Rest), "\\nosupersub "];
43 write_attributed_text(Text, [subscript|Rest]) ->
44 ["\\sub ", write_attributed_text(Text, Rest), "\\nosupersub "];
45 write_attributed_text(Text, [{hyperlink, Destination}|Rest]) ->
46 ["{\\field{\\*\\fldinst{HYPERLINK \"", Destination, "\"}}{\\fldrslt ",
47 write_attributed_text(Text, Rest), "}}"];
48 write_attributed_text(Text, []) ->
49 write_text(Text).
50
51 write_text(Text) ->
52 write_text(Text, []).
53
54 write_text([], Acc) ->
55 lists:reverse(Acc);
56 write_text([H|T], Acc) when H > 127 ->
57 write_text(T, lists:reverse("\\u"++integer_to_list(H), Acc));
58 write_text([H|T], Acc) ->
59 write_text(T, [H|Acc]).
Something went wrong with that request. Please try again.