Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 93 lines (88 sloc) 5.55 kB
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
1 -module(jerome_bbcode_scanner).
2
3 -compile(export_all).
4
5 scan(BBCode) ->
6 scan(BBCode, [], {1, 1}, text).
7
8 scan([], Scanned, _, _) ->
9 {ok, lists:reverse(
10 lists:map(fun
11 ({text, Pos, Text}) ->
12 {text, Pos, lists:reverse(Text)};
13 ({url_value, Pos, Value}) ->
14 {url_value, Pos, lists:reverse(Value)};
15 (Token) ->
16 Token
17 end, Scanned))};
18
19 scan("[b]"++T, Scanned, {Row, Column} = Pos, text) ->
20 scan(T, [{open_bold, Pos}|Scanned], {Row, Column + length("[b]")}, text);
21 scan("[/b]"++T, Scanned, {Row, Column} = Pos, text) ->
22 scan(T, [{close_bold, Pos}|Scanned], {Row, Column + length("[/b]")}, text);
23 scan("[i]"++T, Scanned, {Row, Column} = Pos, text) ->
24 scan(T, [{open_italic, Pos}|Scanned], {Row, Column + length("[i]")}, text);
25 scan("[/i]"++T, Scanned, {Row, Column} = Pos, text) ->
26 scan(T, [{close_italic, Pos}|Scanned], {Row, Column + length("[/i]")}, text);
27 scan("[u]"++T, Scanned, {Row, Column} = Pos, text) ->
28 scan(T, [{open_underline, Pos}|Scanned], {Row, Column + length("[u]")}, text);
29 scan("[/u]"++T, Scanned, {Row, Column} = Pos, text) ->
30 scan(T, [{close_underline, Pos}|Scanned], {Row, Column + length("[/u]")}, text);
31 scan("[sub]"++T, Scanned, {Row, Column} = Pos, text) ->
32 scan(T, [{open_subscript, Pos}|Scanned], {Row, Column + length("[sub]")}, text);
33 scan("[/sub]"++T, Scanned, {Row, Column} = Pos, text) ->
34 scan(T, [{close_subscript, Pos}|Scanned], {Row, Column + length("[/sub]")}, text);
35 scan("[sup]"++T, Scanned, {Row, Column} = Pos, text) ->
36 scan(T, [{open_superscript, Pos}|Scanned], {Row, Column + length("[sup]")}, text);
37 scan("[/sup]"++T, Scanned, {Row, Column} = Pos, text) ->
38 scan(T, [{close_superscript, Pos}|Scanned], {Row, Column + length("[/sup]")}, text);
39 scan("[url]"++T, Scanned, {Row, Column} = Pos, text) ->
40 scan(T, [{open_url, Pos}|Scanned], {Row, Column + length("[url]")}, text);
41 scan("[/url]"++T, Scanned, {Row, Column} = Pos, text) ->
42 scan(T, [{close_url, Pos}|Scanned], {Row, Column + length("[/url]")}, text);
43 scan("[url="++T, Scanned, {Row, Column} = Pos, text) ->
44 scan(T, [{open_url_equals, Pos}|Scanned], {Row, Column + length("[url=")}, in_url);
45 scan("]"++T, Scanned, {Row, Column}, in_url) ->
46 scan(T, Scanned, {Row, Column + 1}, text);
47 scan([H|T], [{url_value, UPos, Value}|Scanned], {Row, Column}, in_url) ->
48 scan(T, [{url_value, UPos, [H|Value]}|Scanned], {Row, Column + 1}, in_url);
49 scan([H|T], Scanned, {Row, Column} = Pos, in_url) ->
50 scan(T, [{url_value, Pos, [H]}|Scanned], {Row, Column + 1}, in_url);
6d3a263 @evanmiller Support for in-line and external images.
authored
51 scan("[img]"++T, Scanned, {Row, Column} = Pos, text) ->
52 scan(T, [{open_img, Pos}|Scanned], {Row, Column + length("[img]")}, text);
53 scan("[/img]"++T, Scanned, {Row, Column} = Pos, text) ->
54 scan(T, [{close_img, Pos}|Scanned], {Row, Column + length("[/img]")}, text);
feb181a @evanmiller First cut: R/W Textile and BBCode
authored
55 scan("[quote]"++T, Scanned, {Row, Column} = Pos, text) ->
56 scan(T, [{open_quote, Pos}|Scanned], {Row, Column + length("[quote]")}, text);
57 scan("[/quote]"++T, Scanned, {Row, Column} = Pos, text) ->
58 scan(T, [{close_quote, Pos}|Scanned], {Row, Column + length("[/quote]")}, text);
59 scan("[code]"++T, Scanned, {Row, Column} = Pos, text) ->
60 scan(T, [{open_code, Pos}|Scanned], {Row, Column + length("[code]")}, text);
61 scan("[/code]"++T, Scanned, {Row, Column} = Pos, text) ->
62 scan(T, [{close_code, Pos}|Scanned], {Row, Column + length("[/code]")}, text);
63 scan("[list]"++T, Scanned, {Row, Column} = Pos, text) ->
64 scan(T, [{open_list, Pos}|Scanned], {Row, Column + length("[list]")}, text);
65 scan("[/list]"++T, Scanned, {Row, Column} = Pos, text) ->
66 scan(T, [{close_list, Pos}|Scanned], {Row, Column + length("[/list]")}, text);
67 scan("[*]"++T, Scanned, {Row, Column} = Pos, text) ->
68 scan(T, [{list_item, Pos}|Scanned], {Row, Column + length("[*]")}, text);
69 scan("[table]"++T, Scanned, {Row, Column} = Pos, text) ->
70 scan(T, [{open_table, Pos}|Scanned], {Row, Column + length("[table]")}, text);
71 scan("[/table]"++T, Scanned, {Row, Column} = Pos, text) ->
72 scan(T, [{close_table, Pos}|Scanned], {Row, Column + length("[/table]")}, text);
73 scan("[tr]"++T, Scanned, {Row, Column} = Pos, text) ->
74 scan(T, [{open_table_row, Pos}|Scanned], {Row, Column + length("[tr]")}, text);
75 scan("[/tr]"++T, Scanned, {Row, Column} = Pos, text) ->
76 scan(T, [{close_table_row, Pos}|Scanned], {Row, Column + length("[/tr]")}, text);
77 scan("[td]"++T, Scanned, {Row, Column} = Pos, text) ->
78 scan(T, [{open_table_cell, Pos}|Scanned], {Row, Column + length("[td]")}, text);
79 scan("[/td]"++T, Scanned, {Row, Column} = Pos, text) ->
80 scan(T, [{close_table_row, Pos}|Scanned], {Row, Column + length("[/td]")}, text);
81 scan("\r\n"++T, Scanned, {Row, _Column} = Pos, _) ->
82 scan(T, [{newline, Pos}|Scanned], {Row + 1, 0}, text);
83 scan("\n"++T, Scanned, {Row, _Column} = Pos, _) ->
84 scan(T, [{newline, Pos}|Scanned], {Row + 1, 0}, text);
85 scan([$\ |T], [{_NotText, _Pos} = PrevToken|Scanned], {Row, Column}, text) ->
86 scan(T, [PrevToken|Scanned], {Row, Column + 1}, text);
87 scan([$\ |T], [{NotText, _Pos, _Val} = PrevToken|Scanned], {Row, Column}, text) when NotText =/= text ->
88 scan(T, [PrevToken|Scanned], {Row, Column + 1}, text);
89 scan([H|T], [{text, TPos, Text}|Scanned], {Row, Column}, text) ->
90 scan(T, [{text, TPos, [H|Text]}|Scanned], {Row, Column + 1}, text);
91 scan([H|T], Scanned, {Row, Column} = Pos, text) ->
92 scan(T, [{text, Pos, [H]}|Scanned], {Row, Column + 1}, text).
Something went wrong with that request. Please try again.