From d6b111ffc861c4abb783f75fdfcfea5d48dcece5 Mon Sep 17 00:00:00 2001 From: Terence Ong Date: Wed, 8 Apr 2026 10:44:01 +0700 Subject: [PATCH] fix: add fallback to strip nbsp-separated publisher names from excerpts Google News RSS excerpts use double non-breaking spaces followed by the publisher name (e.g. "...habitat News-Medical"). When the source field is "news.google.com" rather than the actual publisher, the exact-match logic can't strip it. This adds a regex fallback that catches trailing nbsp-separated capitalized names. Co-Authored-By: Claude Opus 4.6 --- includes/class-peptide-news-rest-api.php | 11 ++++++++++- .../includes/class-peptide-news-rest-api.php | 11 ++++++++++- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/includes/class-peptide-news-rest-api.php b/includes/class-peptide-news-rest-api.php index d53c7af..90c7dc9 100644 --- a/includes/class-peptide-news-rest-api.php +++ b/includes/class-peptide-news-rest-api.php @@ -236,7 +236,7 @@ class Peptide_News_Rest_API { } } - // Fallback: strip trailing " - Short Publisher Name" patterns (1-5 words). + // Fallback 1: strip trailing " - Short Publisher Name" patterns (1-5 words). // Only strips if the part after the separator looks like a publisher name // (starts with uppercase, no sentence-ending punctuation). $pattern = '/\s+[-\x{2013}\x{2014}|]\s+[A-Z][A-Za-z0-9\s.\'\-]{0,50}$/u'; @@ -245,6 +245,15 @@ class Peptide_News_Rest_API { return rtrim( $candidate ); } + // Fallback 2: strip trailing nbsp-separated publisher names. + // Google News RSS excerpts use double non-breaking space + publisher name + // when the source field doesn't match (e.g., source = "news.google.com"). + $nbsp_pattern = '/(?:\x{00A0}{1,2}|\s{2,})[A-Z][A-Za-z0-9\s.\'\-]{0,50}$/u'; + $candidate = preg_replace( $nbsp_pattern, '', $text ); + if ( $candidate !== $text && strlen( $candidate ) > strlen( $text ) * 0.4 ) { + return rtrim( $candidate ); + } + return $text; } diff --git a/peptide-news-plugin/includes/class-peptide-news-rest-api.php b/peptide-news-plugin/includes/class-peptide-news-rest-api.php index b5db289..407dbeb 100644 --- a/peptide-news-plugin/includes/class-peptide-news-rest-api.php +++ b/peptide-news-plugin/includes/class-peptide-news-rest-api.php @@ -230,7 +230,7 @@ class Peptide_News_Rest_API { } } - // Fallback: strip trailing " - Short Publisher Name" patterns (1-5 words). + // Fallback 1: strip trailing " - Short Publisher Name" patterns (1-5 words). // Only strips if the part after the separator looks like a publisher name // (starts with uppercase, no sentence-ending punctuation). $pattern = '/\s+[-–—|]\s+[A-Z][A-Za-z0-9\s.\'-]{0,50}$/u'; @@ -239,6 +239,15 @@ class Peptide_News_Rest_API { return rtrim( $candidate ); } + // Fallback 2: strip trailing nbsp-separated publisher names. + // Google News RSS excerpts use double non-breaking space + publisher name + // when the source field doesn't match (e.g., source = "news.google.com"). + $nbsp_pattern = '/(?:\x{00A0}{1,2}|\s{2,})[A-Z][A-Za-z0-9\s.\'\-]{0,50}$/u'; + $candidate = preg_replace( $nbsp_pattern, '', $text ); + if ( $candidate !== $text && strlen( $candidate ) > strlen( $text ) * 0.4 ) { + return rtrim( $candidate ); + } + return $text; }