diff options
author | M Somerville <matthew-github@dracos.co.uk> | 2020-10-01 11:34:22 +0100 |
---|---|---|
committer | M Somerville <matthew-github@dracos.co.uk> | 2020-10-01 11:35:21 +0100 |
commit | 598bb52cb46aaca98e85328d34b58bbcb7581d71 (patch) | |
tree | 80999d1c192a4f98c601ce17bc7c6e50da13057d | |
parent | 57f4048942a8caaf702f47373fabdfa31932659e (diff) |
Move some templating functions to ::Template.
Most of the functions called by them are in that module, so seems
easiest to keep them together, plus this fixes lack of Web module
import in the Template module.
-rw-r--r-- | perllib/FixMyStreet/App/View/Web.pm | 39 | ||||
-rw-r--r-- | perllib/FixMyStreet/Cobrand/Zurich.pm | 3 | ||||
-rw-r--r-- | perllib/FixMyStreet/Template.pm | 50 |
3 files changed, 53 insertions, 39 deletions
diff --git a/perllib/FixMyStreet/App/View/Web.pm b/perllib/FixMyStreet/App/View/Web.pm index 41444fdd4..8d3d53d0d 100644 --- a/perllib/FixMyStreet/App/View/Web.pm +++ b/perllib/FixMyStreet/App/View/Web.pm @@ -98,16 +98,7 @@ Add some links to some text (and thus HTML-escapes the other text). sub add_links { my $text = shift; - $text = FixMyStreet::Template::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; + return FixMyStreet::Template::add_links($text); } =head2 staff_html_markup_factory @@ -126,36 +117,10 @@ sub staff_html_markup_factory { return sub { my $text = shift; - return _staff_html_markup($text, $staff); + return FixMyStreet::Template::_staff_html_markup($text, $staff); } } -sub _staff_html_markup { - my ( $text, $staff ) = @_; - unless ($staff) { - return FixMyStreet::Template::html_paragraph(add_links($text)); - } - - $text = FixMyStreet::Template::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 = FixMyStreet::Template::html_paragraph($text); - } - - return $text; -} - =head2 escape_js Used to escape strings that are going to be put inside JavaScript. diff --git a/perllib/FixMyStreet/Cobrand/Zurich.pm b/perllib/FixMyStreet/Cobrand/Zurich.pm index 4b893af40..c7b9f70ee 100644 --- a/perllib/FixMyStreet/Cobrand/Zurich.pm +++ b/perllib/FixMyStreet/Cobrand/Zurich.pm @@ -10,6 +10,7 @@ use DateTime::Format::Pg; use Try::Tiny; use FixMyStreet::Geocode::Zurich; +use FixMyStreet::Template; use FixMyStreet::WorkingDays; use strict; @@ -178,7 +179,7 @@ sub updates_as_hashref { $hashref->{update_pp} = $self->prettify_dt( $problem->lastupdate ); if ( $problem->state ne 'external' ) { - $hashref->{details} = FixMyStreet::App::View::Web::add_links( + $hashref->{details} = FixMyStreet::Template::add_links( $problem->get_extra_metadata('public_response') || '' ); } else { $hashref->{details} = sprintf( _('Assigned to %s'), $problem->body->name ); 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; |