diff --git a/sources/AppBundle/Text/Truncator.php b/sources/AppBundle/Text/Truncator.php new file mode 100644 index 000000000..36cbd91c9 --- /dev/null +++ b/sources/AppBundle/Text/Truncator.php @@ -0,0 +1,31 @@ + des talks et aux meta descriptions. + */ +final class Truncator +{ + private const string ELLIPSIS = '…'; + + public static function truncate(string $text, int $maxLength): string + { + if (mb_strlen($text) <= $maxLength) { + return $text; + } + + // Un caractère de plus que la place disponible : s'il s'agit d'un espace, le dernier mot est entier. + $truncated = mb_substr($text, 0, $maxLength - mb_strlen(self::ELLIPSIS) + 1); + $lastSpace = mb_strrpos($truncated, ' '); + $truncated = false !== $lastSpace && $lastSpace > 0 + ? mb_substr($truncated, 0, $lastSpace) + : mb_substr($truncated, 0, -1); + + // Pas d'espace ni de ponctuation devant « … ». En mode /u : rtrim() couperait un caractère multi-octet. + return (string) preg_replace('/[\s,;:.\-–—]+$/u', '', $truncated) . self::ELLIPSIS; + } +} diff --git a/sources/AppBundle/Twig/MetaDescriptionExtension.php b/sources/AppBundle/Twig/MetaDescriptionExtension.php index b7f736b34..81c7759e9 100644 --- a/sources/AppBundle/Twig/MetaDescriptionExtension.php +++ b/sources/AppBundle/Twig/MetaDescriptionExtension.php @@ -4,6 +4,7 @@ namespace AppBundle\Twig; +use AppBundle\Text\Truncator; use Twig\Extension\AbstractExtension; use Twig\TwigFilter; @@ -16,8 +17,6 @@ final class MetaDescriptionExtension extends AbstractExtension { public const int MAX_LENGTH = 155; - private const string ELLIPSIS = '…'; - #[\Override] public function getFilters(): array { @@ -35,18 +34,6 @@ public function metaDescription(string|\Stringable|null $content, int $maxLength $text = html_entity_decode(strip_tags((string) $content), ENT_QUOTES | ENT_HTML5, 'UTF-8'); $text = trim((string) preg_replace('/\s+/u', ' ', $text)); - if (mb_strlen($text) <= $maxLength) { - return $text; - } - - $truncated = mb_substr($text, 0, $maxLength - mb_strlen(self::ELLIPSIS) + 1); - $lastSpace = mb_strrpos($truncated, ' '); - if ($lastSpace !== false) { - $truncated = mb_substr($truncated, 0, $lastSpace); - } else { - $truncated = mb_substr($truncated, 0, -1); - } - - return rtrim($truncated, ' ,;:.-–—') . self::ELLIPSIS; + return Truncator::truncate($text, $maxLength); } } diff --git a/sources/AppBundle/Twig/TalkPageTitleExtension.php b/sources/AppBundle/Twig/TalkPageTitleExtension.php new file mode 100644 index 000000000..e672af71c --- /dev/null +++ b/sources/AppBundle/Twig/TalkPageTitleExtension.php @@ -0,0 +1,46 @@ + d'une page talk en restant sous la limite affichée par les moteurs de recherche. + */ +final class TalkPageTitleExtension extends AbstractExtension +{ + public const int MAX_LENGTH = 65; + + private const string SEPARATOR = ' – '; + + #[\Override] + public function getFunctions(): array + { + return [ + new TwigFunction('talk_page_title', $this->talkPageTitle(...)), + ]; + } + + /** + * « {titre} – {événement} » si l'ensemble tient dans la limite, sinon le titre seul, + * tronqué sur une frontière de mot avec « … » s'il la dépasse encore. + */ + public function talkPageTitle(string $talkTitle, string $eventTitle): string + { + $talkTitle = trim($talkTitle); + $eventTitle = trim($eventTitle); + + if ($eventTitle !== '') { + $withEvent = $talkTitle . self::SEPARATOR . $eventTitle; + if (mb_strlen($withEvent) <= self::MAX_LENGTH) { + return $withEvent; + } + } + + return Truncator::truncate($talkTitle, self::MAX_LENGTH); + } +} diff --git a/templates/site/talks/show.html.twig b/templates/site/talks/show.html.twig index 375d75846..8ac98549f 100644 --- a/templates/site/talks/show.html.twig +++ b/templates/site/talks/show.html.twig @@ -7,7 +7,7 @@ { name: talk.title, url: url('talks_show', {id: talk.id, slug: talk.slug}) }, ] %} -{% block title %}{{ talk.title }} - {{ event.title }}{% endblock %} +{% block title %}{{ talk_page_title(talk.title, event.title) }}{% endblock %} {% block description %}{{ talk.description|meta_description }}{% endblock %} diff --git a/tests/behat/features/PublicSite/Talks.feature b/tests/behat/features/PublicSite/Talks.feature index b8b79b04a..2934816b3 100644 --- a/tests/behat/features/PublicSite/Talks.feature +++ b/tests/behat/features/PublicSite/Talks.feature @@ -396,3 +396,13 @@ Feature: Site Public - Talks Then the page should have a meta description When I go to "/talks/evenements/" Then the page should have a meta description + + @reloadDbWithTestData + Scenario: Le title d'un talk au titre court contient l'évènement + When I go to "/talks/1-jouons-tous-ensemble-a-un-petit-jeu" + Then the response should contain "Jouons tous ensemble à un petit jeu – forum" + + @reloadDbWithTestData + Scenario: Le title d'un talk au titre long se limite au titre + When I go to "/talks/2-rest-ou-graphql-exemples-illustres-avec-symfony-et-api-platform" + Then the response should contain "REST ou GraphQL ? Exemples illustrés avec Symfony et API Platform" diff --git a/tests/unit/AppBundle/Text/TruncatorTest.php b/tests/unit/AppBundle/Text/TruncatorTest.php new file mode 100644 index 000000000..6b753cfbe --- /dev/null +++ b/tests/unit/AppBundle/Text/TruncatorTest.php @@ -0,0 +1,33 @@ + ['Une conférence', 14, 'Une conférence']; + yield 'coupe dans un mot : on recule au mot précédent' => ['Une conférence', 10, 'Une…']; + yield 'coupe juste avant un espace : le dernier mot est gardé' => ['Une conférence sur PHP', 15, 'Une conférence…']; + yield 'ponctuation retirée avant « … »' => ['Une conférence, puis la suite', 20, 'Une conférence…']; + yield 'tiret retiré avant « … »' => ['Une conférence – puis la suite', 20, 'Une conférence…']; + yield 'mot unique trop long : coupe franche' => [str_repeat('é', 20), 10, str_repeat('é', 9) . '…']; + // « ē » s'écrit C4 93 en UTF-8 ; 0x93 est aussi le dernier octet de « – ». + yield 'caractère multi-octet en fin de coupe conservé' => ['Talk sur Dē et la suite', 12, 'Talk sur Dē…']; + } +} diff --git a/tests/unit/AppBundle/Twig/TalkPageTitleExtensionTest.php b/tests/unit/AppBundle/Twig/TalkPageTitleExtensionTest.php new file mode 100644 index 000000000..87fbe73f2 --- /dev/null +++ b/tests/unit/AppBundle/Twig/TalkPageTitleExtensionTest.php @@ -0,0 +1,79 @@ +talkPageTitle($talkTitle, $eventTitle); + + self::assertSame($expected, $title); + self::assertLessThanOrEqual(TalkPageTitleExtension::MAX_LENGTH, mb_strlen($title)); + } + + public static function titles(): \Generator + { + yield 'titre court : avec l\'événement' => [ + 'Jouons tous ensemble à un petit jeu', + 'Forum PHP 2019', + 'Jouons tous ensemble à un petit jeu – Forum PHP 2019', + ]; + + // 49 + 3 + 13 = 65 caractères, accents compris + yield 'exactement 65 caractères avec l\'événement' => [ + 'Révolutionons PHP : les évolutions à venir en été', + 'AFUP Day 2024', + 'Révolutionons PHP : les évolutions à venir en été – AFUP Day 2024', + ]; + + yield 'un caractère de trop avec l\'événement : titre seul' => [ + 'Révolutionons PHP : les évolutions à venir en étés', + 'AFUP Day 2024', + 'Révolutionons PHP : les évolutions à venir en étés', + ]; + + yield 'titre seul de 65 caractères : non tronqué' => [ + 'REST ou GraphQL ? Exemples illustrés avec Symfony et API Platform', + 'Forum PHP 2019', + 'REST ou GraphQL ? Exemples illustrés avec Symfony et API Platform', + ]; + + yield 'titre trop long : tronqué sur une frontière de mot' => [ + 'REST ou GraphQL ? Exemples illustrés avec Symfony et API Platformé', + 'Forum PHP 2019', + 'REST ou GraphQL ? Exemples illustrés avec Symfony et API…', + ]; + + yield 'la coupe tombe juste avant un espace : le dernier mot est gardé' => [ + 'Ééééé ééééé ééééé ééééé ééééé ééééé ééééé ééééé ééééé ééééé éééé ééééé', + 'Forum PHP 2019', + 'Ééééé ééééé ééééé ééééé ééééé ééééé ééééé ééééé ééééé ééééé éééé…', + ]; + + yield 'ponctuation retirée avant les points de suspension' => [ + 'Un titre vraiment très long pour un talk, avec une virgule : puis la suite', + 'Forum PHP 2019', + 'Un titre vraiment très long pour un talk, avec une virgule…', + ]; + + yield 'mot unique trop long : coupe franche' => [ + str_repeat('é', 80), + 'Forum PHP 2019', + str_repeat('é', 64) . '…', + ]; + + yield 'espaces en bordure ignorés' => [ + ' Jouons tous ensemble ', + ' forum ', + 'Jouons tous ensemble – forum', + ]; + } +}