page: prefer meta description as article teaser
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 27 Jun 2019 22:17:10 +0000 (00:17 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 12 Jul 2019 02:18:16 +0000 (04:18 +0200)
article.inc.php

index 336511e87069e59174a80c73d84898562dabe431..e3a3ee824d1949a7dea0292c86c25a3e143c33be 100644 (file)
@@ -82,8 +82,20 @@ class ArchiveArticle
        }
        function teaser()
        {
-               if (preg_match('{<p>(.*?)</p>}s', $this->story, $bodyp)) {
-                       return $bodyp[1];
+               if (preg_match('{
+                       <meta \s+ name="description" [^>]* content="([^">]*)"
+               }x', $this->preface, $meta)) {
+                       # prefer specific page description if found (assume before title)
+                       #TODO: strip from body contents
+                       return $meta[1];
+               }
+               if (preg_match('{
+                       </h2> (?: \s+ | <p\sclass="nav\b.*?</p> | <div[^>]*> )* <p> (.*?) </p>
+               }sx', $this->raw, $bodyp, PREG_OFFSET_CAPTURE)) {
+                       # fallback paragraph contents following the page header
+                       if ($bodyp[1][1] < 256) {
+                               return $bodyp[1][0];
+                       }
                }
        }