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