page: move placeholder_include() to widget method
[minimedit.git] / article.inc.php
index 98cd475b22194095fca6924c9ef4570b6eaed041..b676dd0c5e34537bb17732e13dd5a6b5f35628ff 100644 (file)
@@ -16,8 +16,8 @@ function showdate($parts)
 
 class ArchiveArticle
 {
-       public $raw, $preface, $title, $body;
-       public $meta = [];
+       public $raw, $title, $body; # file contents
+       public $meta = [];  # head metadata properties
 
        function __construct($path)
        {
@@ -41,8 +41,11 @@ class ArchiveArticle
                        $this->meta = array_combine($meta[1], $meta[2]); # [property => content]
                }
 
-               @list ($this->preface, $this->title, $this->body) =
-                       preg_split('{<h2>(.*?)</h2>\s*}s', $this->raw, 2, PREG_SPLIT_DELIM_CAPTURE);
+               // find significant contents
+               $this->body = preg_replace('{<aside\b.*?</aside>}s', '', $this->raw);
+               if (preg_match('{<h2>(.*?)</h2>\s*(.*)}s', $this->body, $titlematch)) {
+                       list (, $this->title, $this->body) = $titlematch;
+               }
        }
 
        function __get($col)
@@ -74,6 +77,12 @@ class ArchiveArticle
                return;
        }
 
+       function restricted()
+       {
+               $this->handler;
+               return $this->restricted;
+       }
+
        function safetitle()
        {
                return trim($this->meta['og:title'] ?? strip_tags($this->title));
@@ -129,11 +138,9 @@ class ArchiveArticle
                }
 
                # paragraph contents following the page header if any
-               $offset = strpos($this->raw, '</h2>');
-               $offset = $offset ? $offset + 5 : 0;
                if (preg_match('{
-                       \G (?> \s+ | <aside\b.*?</aside> | <div [^>]*> | \[\[[^]]*\]\] )* <p> \s* (.*?) </p>
-               }sx', $this->raw, $bodyp, 0, $offset)) {
+                       \G (?> \s+ | <div [^>]*> | \[\[[^]]*\]\] )* <p> \s* (.*?) </p>
+               }sx', $this->body, $bodyp, 0)) {
                        return $bodyp[1];
                }
        }
@@ -163,6 +170,38 @@ class ArchiveArticle
                        $this->image
                );
        }
+
+       function widget($name, $params = [])
+       {
+               $path = stream_resolve_include_path("widget/$name.php");
+               if (!file_exists($path)) {
+                       return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
+               }
+
+               ob_start();
+               $Page = clone $this;
+               $Page->handler = $Page->handler . $Page->path; // .= with explicit getter
+               $Page->path = '';
+               $Place = $GLOBALS['Place'];
+               foreach ($params as $param) {
+                       if ($set = strpos($param, '=')) {
+                               $Place[ substr($param, 0, $set) ] = substr($param, $set + 1);
+                       }
+                       elseif (!empty($param)) {
+                               $Page->path .= '/'.$param;
+                       }
+               }
+               $Page->link .= $Page->path;
+               try {
+                       include "widget/$name.php";
+                       return ob_get_clean();
+               }
+               catch (Exception $e) {
+                       return sprintf('<strong class="warn">%s</strong>',
+                               "fout in <em>$name</em>: {$e->getMessage()}"
+                       );
+               }
+       }
 }
 
 class PageSearch