]> git.uio.no Git - usit-rt.git/blob - share/html/Elements/MakeClicky
Upgrade to 4.0.13
[usit-rt.git] / share / html / Elements / MakeClicky
1 %# BEGIN BPS TAGGED BLOCK {{{
2 %#
3 %# COPYRIGHT:
4 %#
5 %# This software is Copyright (c) 1996-2013 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 $escaper = sub {
52     my $content = shift;
53     RT::Interface::Web::EscapeUTF8( \$content );
54     return $content;
55 };
56
57 my %actions = (
58     default => sub {
59         my %args = @_;
60         return $escaper->($args{value});
61     },
62     url => sub {
63         my %args = @_;
64         $args{value} = $escaper->($args{value});
65         my $result = qq{[<a target="new" href="$args{value}">}. loc('Open URL') .qq{</a>]};
66         return $args{value} . qq{ <span class="clickylink">$result</span>};
67     },
68     url_overwrite => sub {
69         my %args = @_;
70         $args{value} = $escaper->($args{value});
71         my $result = qq{<a target="new" href="$args{value}">$args{value}</a>};
72         return qq{<span class="clickylink">$result</span>};
73     },
74 );
75
76 my @types = (
77     {
78         name   => "httpurl",
79         regex  => qr/$RE{URI}{HTTP}{-keep}{-scheme => 'https?'}(?:#\S+)?/,
80         action => "url",
81     },
82     {
83         name   => "httpurl_overwrite",
84         regex  => qr/$RE{URI}{HTTP}{-keep}{-scheme => 'https?'}(?:#\S+)?/,
85         action => "url_overwrite",
86     },
87 );
88
89 my $handle = sub {
90     my %args = @_;
91     for my $rec( @types ) {
92         return $rec->{action}->(
93             %args,
94             all_matches => [ $args{value}, $1, $2, $3, $4, $5, $6, $7, $8, $9 ],
95         ) if $args{value} =~ $rec->{regex};
96     }
97 };
98
99 # Hook to add more Clicky types
100 # XXX Have to have Page argument, as Mason gets caller wrong in Callback?
101 # This happens as we are in <%ONCE> block
102 $m->callback(
103     CallbackPage => "/Elements/MakeClicky",
104     types        => \@types,
105     actions      => \%actions,
106     handle       => \$handle,
107 );
108
109
110 # Filter
111 my %active;
112 $active{$_}++ for RT->Config->Get('Active_MakeClicky');
113 @types = grep $active{$_->{name}}, @types;
114
115 # Build up the whole match
116 my $regexp = join "|", map $_->{regex}, @types;
117
118 # Make sure we have a default
119 $actions{default} ||= sub {};
120
121 # Anchor the regexes and look up the actions
122 foreach my $type ( @types ) {
123     $type->{regex}  = qr/^$type->{regex}$/;
124     $type->{action} = $actions{$type->{action}} || $actions{default};
125 }
126
127 </%ONCE>
128 <%ARGS>
129 $content => undef
130 $html => undef
131 </%ARGS>
132 <%INIT>
133 return unless defined $$content;
134 unless ( $regexp ) {
135     RT::Interface::Web::EscapeUTF8( $content ) unless $html;
136     return;
137 }
138
139 my $pos = 0;
140 while ( $$content =~ /($regexp)/gsio ) {
141     my $match = $1;
142     next if $` =~ /href=(?:&quot;|")$/;
143     my $skipped_len = pos($$content) - $pos - length($match);
144     if ( $skipped_len > 0 ) {
145         my $plain;
146         if ( $html ) {
147             $plain = substr( $$content, $pos, $skipped_len );
148         }
149         else {
150             $plain = $escaper->( substr( $$content, $pos, $skipped_len ) )
151         }
152         substr( $$content, $pos, $skipped_len ) = $plain;
153         $pos += length($plain);
154     }
155     my $plain = $handle->(
156         %ARGS, 
157         value => $match,
158         all_matches => [ $1, $2, $3, $4, $5, $6, $7, $8, $9 ],
159     );
160     substr( $$content, $pos, length($match) ) = $plain;
161     pos($$content) = ( $pos += length($plain) );
162
163 }
164 substr( $$content, $pos ) = $escaper->( substr( $$content, $pos ) ) unless
165 ($pos == length $$content) || $html;
166
167 pos($$content) = 0;
168
169 </%INIT>
170 <%doc>
171
172 MakeClicky detects various formats of data in headers and email
173 messages, and extends them with supporting links.  By default, RT
174 provides two formats:
175
176  * 'httpurl': detects http:// and https:// URLs and adds '[Open URL]'
177    link after the URL.
178
179  * 'httpurl_overwrite': also detects URLs as 'httpurl' format, but
180    replace URL with link.
181
182 To extend this with your own types of data, use the callback.
183 It will be provided with:
184
185  * 'types': An array reference of hash references.  Modify this array
186     reference to add your own types; the first matching type will be
187     used.  Each hashref should contain:
188    - 'name': The name of the data format; this is used in the
189       configuration file to enable the format.
190    - 'regex': A regular expression to match against
191    - 'action': The name of the action to run (see "actions", below)
192
193  * 'actions': A hash reference of 'actions'.  Modify this hash
194     reference to change or add action types.  Values are subroutine
195     references which will get called when needed.  They should return
196     the modified string. Note that subroutine must escape HTML.
197
198  * 'handler': A reference to a subroutine reference; modify it if you
199     have to. This can be used to add pre- or post-processing around
200     all actions.
201
202 Read more about writing new actions in docs/extending/clickable_links.pod
203
204 </%doc>