/modules/mod_base/filters/filter_summary.erl

http://github.com/zotonic/zotonic · Erlang · 51 lines · 29 code · 6 blank · 16 comment · 0 complexity · 09a42bb1ab831777c1fb0e53f468deb2 MD5 · raw file

  1. %% @author Arjan Scherpenisse <arjan@scherpenisse.net>
  2. %% @copyright 2010 Arjan Scherpenisse
  3. %% @doc Give a plaintext summary of the resource. Takes either the summary or, if non existent, a part of the body text.
  4. %% Copyright 2010 Arjan Scherpenisse
  5. %%
  6. %% Licensed under the Apache License, Version 2.0 (the "License");
  7. %% you may not use this file except in compliance with the License.
  8. %% You may obtain a copy of the License at
  9. %%
  10. %% http://www.apache.org/licenses/LICENSE-2.0
  11. %%
  12. %% Unless required by applicable law or agreed to in writing, software
  13. %% distributed under the License is distributed on an "AS IS" BASIS,
  14. %% WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. %% See the License for the specific language governing permissions and
  16. %% limitations under the License.
  17. -module(filter_summary).
  18. -export([summary/2, summary/3]).
  19. -include("zotonic.hrl").
  20. summary(undefined, _Context) ->
  21. undefined;
  22. summary(RId, Context) ->
  23. summary(RId, 200, Context).
  24. summary(undefined, _N, _Context) ->
  25. undefined;
  26. summary(RId, N, Context) ->
  27. case m_rsc:rid(RId, Context) of
  28. undefined ->
  29. undefined;
  30. Id ->
  31. S = get_summary_text(Id, Context),
  32. S1 = case z_utils:is_empty(S) of
  33. true ->
  34. Body = m_rsc:p(Id, body, Context),
  35. z_string:trim_left(z_html:strip(z_trans:lookup_fallback(Body, Context)));
  36. false ->
  37. S
  38. end,
  39. z_string:trim(z_string:truncate(S1, z_convert:to_integer(N)))
  40. end.
  41. get_summary_text(Id, Context) ->
  42. case m_rsc:p(Id, summary, Context) of
  43. {trans, _} = T -> z_trans:lookup_fallback(T, Context);
  44. Other -> Other
  45. end.