diff options
Diffstat (limited to 'perllib/FixMyStreet/Template.pm')
-rw-r--r-- | perllib/FixMyStreet/Template.pm | 50 |
1 files changed, 49 insertions, 1 deletions
diff --git a/perllib/FixMyStreet/Template.pm b/perllib/FixMyStreet/Template.pm index 35efcc1cf..bc087de9a 100644 --- a/perllib/FixMyStreet/Template.pm +++ b/perllib/FixMyStreet/Template.pm @@ -238,7 +238,55 @@ sub email_sanitize_html : Fn('email_sanitize_html') { my $staff = $extra->{is_superuser} || $extra->{is_body_user}; - return FixMyStreet::App::View::Web::_staff_html_markup($text, $staff); + return _staff_html_markup($text, $staff); +} + +sub _staff_html_markup { + my ( $text, $staff ) = @_; + unless ($staff) { + return html_paragraph(add_links($text)); + } + + $text = sanitize($text); + + # Apply Markdown-style italics + $text =~ s{\*(\S.*?\S)\*}{<i>$1</i>}; + + # Mark safe so add_links doesn't escape everything. + $text = FixMyStreet::Template::SafeString->new($text); + + $text = add_links($text); + + # If the update already has block-level elements then don't wrap + # individual lines in <p> elements, as we assume the user knows what + # they're doing. + unless ($text =~ /<(p|ol|ul)>/) { + $text = html_paragraph($text); + } + + return $text; +} + +=head2 add_links + + [% text | add_links | html_para %] + +Add some links to some text (and thus HTML-escapes the other text). + +=cut + +sub add_links { + my $text = shift; + $text = conditional_escape($text); + $text =~ s/\r//g; + $text =~ s{(?<!["'])(https?://)([^\s]+)}{"<a href=\"$1$2\">$1" . _space_slash($2) . '</a>'}ge; + return FixMyStreet::Template::SafeString->new($text); +} + +sub _space_slash { + my $t = shift; + $t =~ s{/(?!$)}{/ }g; + return $t; } 1; |