PageRenderTime 14ms CodeModel.GetById 1ms app.highlight 9ms RepoModel.GetById 2ms app.codeStats 0ms

/modules/mod_base/filters/filter_linebreaksbr.erl

https://code.google.com/p/zotonic/
Erlang | 70 lines | 37 code | 8 blank | 25 comment | 0 complexity | 2fc0cdd4d078cfd905db08996c07f501 MD5 | raw file
 1%% @author    Roberto Saccon <rsaccon@gmail.com> [http://rsaccon.com]
 2%% @author    Evan Miller <emmiller@gmail.com>
 3%% @copyright 2008 Roberto Saccon, Evan Miller
 4%% @doc 'linebreaksbr' filter, translate linebreaks into <br/> elements
 5
 6%%% The MIT License
 7%%%
 8%%% Copyright (c) 2007 Roberto Saccon, Evan Miller
 9%%%
10%%% Permission is hereby granted, free of charge, to any person obtaining a copy
11%%% of this software and associated documentation files (the "Software"), to deal
12%%% in the Software without restriction, including without limitation the rights
13%%% to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14%%% copies of the Software, and to permit persons to whom the Software is
15%%% furnished to do so, subject to the following conditions:
16%%%
17%%% The above copyright notice and this permission notice shall be included in
18%%% all copies or substantial portions of the Software.
19%%%
20%%% THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21%%% IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22%%% FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23%%% AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24%%% LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25%%% OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
26%%% THE SOFTWARE.
27
28-module(filter_linebreaksbr).
29-export([linebreaksbr/2]).
30
31-author('rsaccon@gmail.com').
32-author('emmiller@gmail.com').
33
34
35linebreaksbr(undefined, _Context) ->
36    undefined;
37linebreaksbr(Input, _Context) when is_binary(Input) ->
38    linebreaksbr1(Input, 0);
39linebreaksbr(Input, _Context) ->
40    linebreaksbr1(Input, []).
41
42linebreaksbr1(Input, Index) when is_binary(Input) ->
43    Break = <<"<br />">>,
44    case Input of
45        <<Pre:Index/binary, $\r, $\n, Post/binary>> ->
46            process_binary_match(Pre, Break, size(Post), linebreaksbr1(Post, 0));
47        <<Pre:Index/binary, $\n, Post/binary>> ->
48            process_binary_match(Pre, Break, size(Post), linebreaksbr1(Post, 0));
49        <<_:Index/binary, _/binary>> ->
50            linebreaksbr1(Input, Index + 1);
51        _ ->
52            Input
53    end;
54linebreaksbr1([], Acc) ->
55    lists:reverse(Acc);
56linebreaksbr1("\r\n" ++ Rest, Acc) ->
57    linebreaksbr1(Rest, lists:reverse("<br />", Acc));
58linebreaksbr1("\n" ++ Rest, Acc) ->
59    linebreaksbr1(Rest, lists:reverse("<br />", Acc));
60linebreaksbr1([C | Rest], Acc) ->
61    linebreaksbr1(Rest, [C | Acc]).
62
63
64process_binary_match(Pre, Insertion, SizePost, Post) ->
65    case {size(Pre), SizePost} of
66        {0, 0} -> Insertion;
67        {0, _} -> [Insertion, Post];
68        {_, 0} -> [Pre, Insertion];
69        _ -> [Pre, Insertion, Post]
70    end.