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