summaryrefslogtreecommitdiff
path: root/rt/share/html/Elements/MakeClicky
blob: f0aaf0696fa503d4457559f26e9153c4ed51d417 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
%# BEGIN BPS TAGGED BLOCK {{{
%# 
%# COPYRIGHT:
%# 
%# This software is Copyright (c) 1996-2009 Best Practical Solutions, LLC
%#                                          <jesse@bestpractical.com>
%# 
%# (Except where explicitly superseded by other copyright notices)
%# 
%# 
%# LICENSE:
%# 
%# This work is made available to you under the terms of Version 2 of
%# the GNU General Public License. A copy of that license should have
%# been provided with this software, but in any event can be snarfed
%# from www.gnu.org.
%# 
%# This work is distributed in the hope that it will be useful, but
%# WITHOUT ANY WARRANTY; without even the implied warranty of
%# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
%# General Public License for more details.
%# 
%# You should have received a copy of the GNU General Public License
%# along with this program; if not, write to the Free Software
%# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
%# 02110-1301 or visit their web page on the internet at
%# http://www.gnu.org/licenses/old-licenses/gpl-2.0.html.
%# 
%# 
%# CONTRIBUTION SUBMISSION POLICY:
%# 
%# (The following paragraph is not intended to limit the rights granted
%# to you to modify and distribute this software under the terms of
%# the GNU General Public License and is only of importance to you if
%# you choose to contribute your changes and enhancements to the
%# community by submitting them to Best Practical Solutions, LLC.)
%# 
%# By intentionally submitting any modifications, corrections or
%# derivatives to this work, or any other work intended for use with
%# Request Tracker, to Best Practical Solutions, LLC, you confirm that
%# you are the copyright holder for those contributions and you grant
%# Best Practical Solutions,  LLC a nonexclusive, worldwide, irrevocable,
%# royalty-free, perpetual, license to use, copy, create derivative
%# works based on those contributions, and sublicense and distribute
%# those contributions and any derivatives thereof.
%# 
%# END BPS TAGGED BLOCK }}}
<%ONCE>
use Regexp::Common qw(URI);

my %actions = (
    default => sub {
        my %args = @_;
        return $args{value};
    },
    url => sub {
        my %args = @_;
        my $result = qq{[<a target="new" href="$args{value}">}. loc('Open URL') .qq{</a>]};
        return $args{value} . qq{ <span class="clickylink">$result</span>};
    },
    url_overwrite => sub {
        my %args = @_;
        my $result = qq{<a target="new" href="$args{'value'}">};
        #XXX: use spaces here. &shy; <wbr> are not well supported :(
        $args{'value'} =~ s/(\S{30})/$1 /g;
        $result .= qq{$args{'value'}</a>};
        return qq{<span class="clickylink">$result</span>};
    },
);

my @types = (
    {
        name   => "httpurl",
        regex  => qr/$RE{URI}{HTTP}{-keep}{-scheme => 'https?'}/,
        action => "url",
    },
    {
        name   => "httpurl_overwrite",
        regex  => qr/$RE{URI}{HTTP}{-keep}{-scheme => 'https?'}/,
        action => "url_overwrite",
    },
);

my $handle = sub {
    my %args = @_;
    for my $rec( @types ) {
        return $rec->{action}->(
            %args,
            all_matches => [ $args{value}, $1, $2, $3, $4, $5, $6, $7, $8, $9 ],
        ) if $args{value} =~ $rec->{regex};
    }
};

my $escaper = sub {
    my $content = shift;
    RT::Interface::Web::EscapeUTF8( \$content );
    return $content;
};

# Hook to add more Clicky types
# XXX Have to have Page argument, as Mason gets caller wrong in Callback?
# This happens as we are in <%ONCE> block
$m->callback(
    CallbackPage => "/Elements/MakeClicky",
    types        => \@types,
    actions      => \%actions,
    handle       => \$handle,
);


# Filter
my %active;
$active{$_}++ for RT->Config->Get('Active_MakeClicky');
@types = grep $active{$_->{name}}, @types;

# Build up the whole match
my $regexp = join "|", map $_->{regex}, @types;

# Make sure we have a default
$actions{default} ||= sub {};

# Anchor the regexes and look up the actions
foreach my $type ( @types ) {
    $type->{regex}  = qr/^$type->{regex}$/;
    $type->{action} = $actions{$type->{action}} || $actions{default};
}

</%ONCE>
<%ARGS>
$content => undef
$html => undef
</%ARGS>
<%INIT>
return unless defined $$content;
unless ( $regexp ) {
    RT::Interface::Web::EscapeUTF8( $content ) unless $html;
    return;
}

my $pos = 0;
while ( $$content =~ /($regexp)/gsio ) {
    my $match = $1;
    next if $` =~ /href=(?:&quot;|")$/;
    my $skipped_len = pos($$content) - $pos - length($match);
    if ( $skipped_len > 0 ) {
        my $plain;
        if ( $html ) {
            $plain = substr( $$content, $pos, $skipped_len );
        }
        else {
            $plain = $escaper->( substr( $$content, $pos, $skipped_len ) )
        }
        substr( $$content, $pos, $skipped_len ) = $plain;
        $pos += length($plain);
    }
    my $plain = $handle->(
        %ARGS, 
        value => $match,
        all_matches => [ $1, $2, $3, $4, $5, $6, $7, $8, $9 ],
    );
    substr( $$content, $pos, length($match) ) = $plain;
    pos($$content) = ( $pos += length($plain) );

}
substr( $$content, $pos ) = $escaper->( substr( $$content, $pos ) ) unless
($pos == length $$content) || $html;

pos($$content) = 0;

</%INIT>
<%doc>

MakeClicky detects various formats of data in headers and email
messages, and extends them with supporting links.  By default, RT
provides two formats:

 * 'httpurl': detects http:// and https:// URLs and adds '[Open URL]'
   link after the URL.

 * 'httpurl_overwrite': also detects URLs as 'httpurl' format, but
   replace URL with link and *adds spaces* into text if it's longer
   then 30 chars. This allow browser to wrap long URLs and avoid
   horizontal scrolling.

To extend this with your own types of data, use the callback.
It will be provided with:

 * 'types': An array reference of hash references.  Modify this array
    reference to add your own types; the first matching type will be
    used.  Each hashref should contain:
   - 'name': The name of the data format; this is used in the
      configuration file to enable the format.
   - 'regex': A regular expression to match against
   - 'action': The name of the action to run (see "actions", below)

 * 'actions': A hash reference of 'actions'.  Modify this hash
    reference to change or add action types.  Values are subroutine
    references which will get called when needed.  They should return
    the modified string. Note that subroutine must escape HTML.

 * 'handler': A reference to a subroutine reference; modify it if you
    have to. This can be used to add pre- or post-processing around
    all actions.

Read more about writing new actions in docs/extending_clickable_links.pod

</%doc>