From d6d5085a36a158b1cd9020a60d94f10c6cfc86ea Mon Sep 17 00:00:00 2001
From: Guillaume Rousse <guillaume.rousse@renater.fr>
Date: Mon, 7 May 2018 14:25:59 +0200
Subject: [PATCH] templates simplification: chomp by default

---
 lib/AccountManager/App.pm                 |  7 ++--
 lib/AccountManager/Tools.pm               |  2 ++
 templates/accounts/accounts.php.tt2       | 44 +++++++++++------------
 templates/web/en/create_accounts.tt2.html |  8 ++---
 templates/web/fr/create_accounts.tt2.html |  8 ++---
 5 files changed, 37 insertions(+), 32 deletions(-)

diff --git a/lib/AccountManager/App.pm b/lib/AccountManager/App.pm
index 0c769a5..294b257 100644
--- a/lib/AccountManager/App.pm
+++ b/lib/AccountManager/App.pm
@@ -10,6 +10,7 @@ use HTTP::AcceptLanguage;
 use Log::Any::Adapter;
 use List::MoreUtils qw(uniq);
 use Template;
+use Template::Constants qw(:chomp);
 use Text::CSV;
 
 use AccountManager::Account;
@@ -179,7 +180,8 @@ sub respond {
 
     ## Parse template
     my $tt2 = Template->new({
-        ENCODING => 'utf8',
+        ENCODING     => 'utf8',
+        PRE_CHOMP    => CHOMP_ONE,
         INCLUDE_PATH => $self->{configuration}->{setup}->{templates_dir} . "/web/$lang"
     });
 
@@ -480,7 +482,8 @@ sub req_complete_challenge {
     my $lang = HTTP::AcceptLanguage->new($ENV{HTTP_ACCEPT_LANGUAGE})->match(qw/en fr/) || 'en';
 
     my $tt2 = Template->new({
-        ENCODING => 'utf8',
+        ENCODING     => 'utf8',
+        PRE_CHOMP    => CHOMP_ONE,
         INCLUDE_PATH => $self->{configuration}->{setup}->{templates_dir} . "/mail/$lang"
     });
     my $template = 'send_authentication_token.tt2.eml';
diff --git a/lib/AccountManager/Tools.pm b/lib/AccountManager/Tools.pm
index 126b7c4..08287ed 100644
--- a/lib/AccountManager/Tools.pm
+++ b/lib/AccountManager/Tools.pm
@@ -10,6 +10,7 @@ use List::Util qw(shuffle);
 use List::MoreUtils qw(pairwise);
 use MIME::Base64;
 use Template;
+use Template::Constants qw(:chomp);
 use Template::Stash;
 
 sub encrypt {
@@ -109,6 +110,7 @@ sub update_ssp_authsources {
 
     my $tt2 = Template->new({
         ENCODING     => 'utf8',
+        PRE_CHOMP    => CHOMP_ONE,
         INCLUDE_PATH => $templates_dir . '/accounts'
     });
     my $template = 'accounts.php.tt2';
diff --git a/templates/accounts/accounts.php.tt2 b/templates/accounts/accounts.php.tt2
index 568ef2d..1f04f5a 100644
--- a/templates/accounts/accounts.php.tt2
+++ b/templates/accounts/accounts.php.tt2
@@ -3,41 +3,41 @@
 $validTestAccounts = array (
     'authcrypt:Hash',
 
-[% FOREACH account IN accounts -%]
+[% FOREACH account IN accounts %]
     'user[% account.id() %]:{SHA256}[% account.password_hash() %]=' => array(
         'internal_uid'               => array([% account.internal_uid().quote %]),
         'associatedSP'               => array([% account.associatedSP().quote %]),
-    [% IF account.cn() -%]
+    [% IF account.cn() %]
         'cn'                         => array([% account.cn().quote %]),
-    [% END -%]
-    [% IF account.sn() -%]
+    [% END %]
+    [% IF account.sn() %]
         'sn'                         => array([% account.sn().quote %]),
-    [% END -%]
-    [% IF account.displayName() -%]
+    [% END %]
+    [% IF account.displayName() %]
         'displayName'                => array([% account.displayName().quote %]),
-    [% END -%]
-    [% IF account.givenName() -%]
+    [% END %]
+    [% IF account.givenName() %]
         'givenName'                  => array([% account.givenName().quote %]),
-    [% END -%]
-    [% IF account.mail() -%]
+    [% END %]
+    [% IF account.mail() %]
         'mail'                       => array([% account.mail().quote %]),
-    [% END -%]
-    [% IF account.eduPersonAffiliation() -%]
+    [% END %]
+    [% IF account.eduPersonAffiliation() %]
         'eduPersonAffiliation'       => array([% account.eduPersonAffiliation().quote.join(', ') %]),
-    [% END -%]
-    [% IF account.eduPersonScopedAffiliation() -%]
+    [% END %]
+    [% IF account.eduPersonScopedAffiliation() %]
         'eduPersonScopedAffiliation' => array([% account.eduPersonScopedAffiliation().quote.join(', ') %]),
-    [% END -%]
-    [% IF account.eduPersonPrincipalName() -%]
+    [% END %]
+    [% IF account.eduPersonPrincipalName() %]
         'eduPersonPrincipalName'     => array([% account.eduPersonPrincipalName().quote %]),
-    [% END -%]
-    [% IF account.schacHomeOrganization() -%]
+    [% END %]
+    [% IF account.schacHomeOrganization() %]
         'schacHomeOrganization'      => array([% account.schacHomeOrganization().quote %]),
-    [% END -%]
-    [% IF account.schacHomeOrganizationType() -%]
+    [% END %]
+    [% IF account.schacHomeOrganizationType() %]
         'schacHomeOrganizationType'  => array([% account.schacHomeOrganizationType().quote %]),
-    [% END -%]
+    [% END %]
     ),
 
-[% END -%]
+[% END %]
 );
diff --git a/templates/web/en/create_accounts.tt2.html b/templates/web/en/create_accounts.tt2.html
index 39a9603..82c73df 100644
--- a/templates/web/en/create_accounts.tt2.html
+++ b/templates/web/en/create_accounts.tt2.html
@@ -49,14 +49,14 @@ provider.</p>
         <div>
             <table>
                 <caption>List of user attributes</caption>
-    [% FOREACH attribute IN [ 'cn', 'displayName', 'givenName', 'sn', 'mail', 'schacHomeOrganization', 'schacHomeOrganizationType', 'eduPersonPrincipalName' ] -%]
+    [% FOREACH attribute IN [ 'cn', 'displayName', 'givenName', 'sn', 'mail', 'schacHomeOrganization', 'schacHomeOrganizationType', 'eduPersonPrincipalName' ] %]
                 <tr>
-                    <th>[% attribute %]</th><td> [% account.$attribute() %]</td>
+                    <th>[% attribute %]</th><td>[% account.$attribute() %]</td>
                 </tr>
     [% END %]
-    [% FOREACH attribute IN [ 'eduPersonAffiliation', 'eduPersonScopedAffiliation' ] -%]
+    [% FOREACH attribute IN [ 'eduPersonAffiliation', 'eduPersonScopedAffiliation' ] %]
                 <tr>
-                    <th>[% attribute %]</th><td> [% account.$attribute().join(', ') %]</td>
+                    <th>[% attribute %]</th><td>[% account.$attribute().join(', ') %]</td>
                 </tr>
     [% END %]
                 <tr>
diff --git a/templates/web/fr/create_accounts.tt2.html b/templates/web/fr/create_accounts.tt2.html
index 162a628..b8bb637 100644
--- a/templates/web/fr/create_accounts.tt2.html
+++ b/templates/web/fr/create_accounts.tt2.html
@@ -51,14 +51,14 @@ service fédéré. Pour le faire, sélectionnez <strong>[% idp_displayname
         <div>
             <table>
                 <caption>Liste des attributs utilisateur</caption>
-    [% FOREACH attribute IN [ 'cn', 'displayName', 'givenName', 'sn', 'mail', 'schacHomeOrganization', 'schacHomeOrganizationType', 'eduPersonPrincipalName' ] -%]
+    [% FOREACH attribute IN [ 'cn', 'displayName', 'givenName', 'sn', 'mail', 'schacHomeOrganization', 'schacHomeOrganizationType', 'eduPersonPrincipalName' ] %]
                 <tr>
-                    <th>[% attribute %]</th><td> [% account.$attribute() %]</td>
+                    <th>[% attribute %]</th><td>[% account.$attribute() %]</td>
                 </tr>
     [% END %]
-    [% FOREACH attribute IN [ 'eduPersonAffiliation', 'eduPersonScopedAffiliation' ] -%]
+    [% FOREACH attribute IN [ 'eduPersonAffiliation', 'eduPersonScopedAffiliation' ] %]
                 <tr>
-                    <th>[% attribute %]</th><td> [% account.$attribute().join(', ') %]</td>
+                    <th>[% attribute %]</th><td>[% account.$attribute().join(', ') %]</td>
                 </tr>
     [% END %]
                 <tr>
-- 
GitLab