merging RT 4.0.6
[freeside.git] / rt / share / html / Elements / MakeClicky
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2012 Best Practical Solutions, LLC
6 %#                                          <sales@bestpractical.com>
7 %#
8 %# (Except where explicitly superseded by other copyright notices)
9 %#
10 %#
11 %# LICENSE:
12 %#
13 %# This work is made available to you under the terms of Version 2 of
14 %# the GNU General Public License. A copy of that license should have
15 %# been provided with this software, but in any event can be snarfed
16 %# from www.gnu.org.
17 %#
18 %# This work is distributed in the hope that it will be useful, but
19 %# WITHOUT ANY WARRANTY; without even the implied warranty of
20 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
21 %# General Public License for more details.
22 %#
23 %# You should have received a copy of the GNU General Public License
24 %# along with this program; if not, write to the Free Software
25 %# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 %# 02110-1301 or visit their web page on the internet at
27 %# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
28 %#
29 %#
30 %# CONTRIBUTION SUBMISSION POLICY:
31 %#
32 %# (The following paragraph is not intended to limit the rights granted
33 %# to you to modify and distribute this software under the terms of
34 %# the GNU General Public License and is only of importance to you if
35 %# you choose to contribute your changes and enhancements to the
36 %# community by submitting them to Best Practical Solutions, LLC.)
37 %#
38 %# By intentionally submitting any modifications, corrections or
39 %# derivatives to this work, or any other work intended for use with
40 %# Request Tracker, to Best Practical Solutions, LLC, you confirm that
41 %# you are the copyright holder for those contributions and you grant
42 %# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
43 %# royalty-free, perpetual, license to use, copy, create derivative
44 %# works based on those contributions, and sublicense and distribute
45 %# those contributions and any derivatives thereof.
46 %#
47 %# END BPS TAGGED BLOCK }}}
48 <%ONCE>
49 use Regexp::Common qw(URI);
50
51 my %actions = (
52     default => sub {
53         my %args = @_;
54         return $args{value};
55     },
56     url => sub {
57         my %args = @_;
58         my $result = qq{[<a target="new" href="$args{value}">}. loc('Open URL') .qq{</a>]};
59         return $args{value} . qq{ <span class="clickylink">$result</span>};
60     },
61     url_overwrite => sub {
62         my %args = @_;
63         my $result = qq{<a target="new" href="$args{'value'}">};
64         $result .= qq{$args{'value'}</a>};
65         return qq{<span class="clickylink">$result</span>};
66     },
67 );
68
69 my @types = (
70     {
71         name   => "httpurl",
72         regex  => qr/$RE{URI}{HTTP}{-keep}{-scheme => 'https?'}(?:#\S+)?/,
73         action => "url",
74     },
75     {
76         name   => "httpurl_overwrite",
77         regex  => qr/$RE{URI}{HTTP}{-keep}{-scheme => 'https?'}(?:#\S+)?/,
78         action => "url_overwrite",
79     },
80 );
81
82 my $handle = sub {
83     my %args = @_;
84     for my $rec( @types ) {
85         return $rec->{action}->(
86             %args,
87             all_matches => [ $args{value}, $1, $2, $3, $4, $5, $6, $7, $8, $9 ],
88         ) if $args{value} =~ $rec->{regex};
89     }
90 };
91
92 my $escaper = sub {
93     my $content = shift;
94     RT::Interface::Web::EscapeUTF8( \$content );
95     return $content;
96 };
97
98 # Hook to add more Clicky types
99 # XXX Have to have Page argument, as Mason gets caller wrong in Callback?
100 # This happens as we are in <%ONCE> block
101 $m->callback(
102     CallbackPage => "/Elements/MakeClicky",
103     types        => \@types,
104     actions      => \%actions,
105     handle       => \$handle,
106 );
107
108
109 # Filter
110 my %active;
111 $active{$_}++ for RT->Config->Get('Active_MakeClicky');
112 @types = grep $active{$_->{name}}, @types;
113
114 # Build up the whole match
115 my $regexp = join "|", map $_->{regex}, @types;
116
117 # Make sure we have a default
118 $actions{default} ||= sub {};
119
120 # Anchor the regexes and look up the actions
121 foreach my $type ( @types ) {
122     $type->{regex}  = qr/^$type->{regex}$/;
123     $type->{action} = $actions{$type->{action}} || $actions{default};
124 }
125
126 </%ONCE>
127 <%ARGS>
128 $content => undef
129 $html => undef
130 </%ARGS>
131 <%INIT>
132 return unless defined $$content;
133 unless ( $regexp ) {
134     RT::Interface::Web::EscapeUTF8( $content ) unless $html;
135     return;
136 }
137
138 my $pos = 0;
139 while ( $$content =~ /($regexp)/gsio ) {
140     my $match = $1;
141     next if $` =~ /href=(?:&quot;|")$/;
142     my $skipped_len = pos($$content) - $pos - length($match);
143     if ( $skipped_len > 0 ) {
144         my $plain;
145         if ( $html ) {
146             $plain = substr( $$content, $pos, $skipped_len );
147         }
148         else {
149             $plain = $escaper->( substr( $$content, $pos, $skipped_len ) )
150         }
151         substr( $$content, $pos, $skipped_len ) = $plain;
152         $pos += length($plain);
153     }
154     my $plain = $handle->(
155         %ARGS, 
156         value => $match,
157         all_matches => [ $1, $2, $3, $4, $5, $6, $7, $8, $9 ],
158     );
159     substr( $$content, $pos, length($match) ) = $plain;
160     pos($$content) = ( $pos += length($plain) );
161
162 }
163 substr( $$content, $pos ) = $escaper->( substr( $$content, $pos ) ) unless
164 ($pos == length $$content) || $html;
165
166 pos($$content) = 0;
167
168 </%INIT>
169 <%doc>
170
171 MakeClicky detects various formats of data in headers and email
172 messages, and extends them with supporting links.  By default, RT
173 provides two formats:
174
175  * 'httpurl': detects http:// and https:// URLs and adds '[Open URL]'
176    link after the URL.
177
178  * 'httpurl_overwrite': also detects URLs as 'httpurl' format, but
179    replace URL with link.
180
181 To extend this with your own types of data, use the callback.
182 It will be provided with:
183
184  * 'types': An array reference of hash references.  Modify this array
185     reference to add your own types; the first matching type will be
186     used.  Each hashref should contain:
187    - 'name': The name of the data format; this is used in the
188       configuration file to enable the format.
189    - 'regex': A regular expression to match against
190    - 'action': The name of the action to run (see "actions", below)
191
192  * 'actions': A hash reference of 'actions'.  Modify this hash
193     reference to change or add action types.  Values are subroutine
194     references which will get called when needed.  They should return
195     the modified string. Note that subroutine must escape HTML.
196
197  * 'handler': A reference to a subroutine reference; modify it if you
198     have to. This can be used to add pre- or post-processing around
199     all actions.
200
201 Read more about writing new actions in docs/extending/clickable_links.pod
202
203 </%doc>