PageRenderTime 16ms CodeModel.GetById 9ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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