X-Git-Url: http://git.shiar.nl/minimedit.git/blobdiff_plain/aa4a9fd115357f8d8781220e1f1345df774d6893..f065f4739ae477d8877805463017f2e7b15e2db5:/article.inc.php diff --git a/article.inc.php b/article.inc.php index dd3d5b2..c630898 100644 --- a/article.inc.php +++ b/article.inc.php @@ -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,13 @@ class ArchiveArticle $this->meta = array_combine($meta[1], $meta[2]); # [property => content] } - @list ($this->preface, $this->title, $this->body) = - preg_split('{

(.*?)

\s*}s', $this->raw, 2, PREG_SPLIT_DELIM_CAPTURE); + // find significant contents + $this->body = preg_replace('{}s', '', $this->raw); + if (preg_match('{

(.*?)

\s*(.*)}s', $this->body, $titlematch)) { + list (, $this->title, $this->body) = $titlematch; + } + + return $this->raw; } function __get($col) @@ -74,6 +79,12 @@ class ArchiveArticle return; } + function restricted() + { + $this->handler; + return $this->restricted; + } + function safetitle() { return trim($this->meta['og:title'] ?? strip_tags($this->title)); @@ -128,19 +139,10 @@ class ArchiveArticle return $override; } + # paragraph contents following the page header if any if (preg_match('{ - (?: \s+ | | ]*> )*

\s* (.*?)

- }sx', $this->raw, $bodyp, PREG_OFFSET_CAPTURE)) { - # fallback paragraph contents following the page header - if ($bodyp[1][1] < 512) { - return $bodyp[1][0]; - } - } - - # starting paragraph for documents without title (assumed simple/partial) - if (strpos($this->raw, ']*> \s* )*

\s* (.*?)

- }sx', $this->raw, $bodyp)) { + \G (?> \s+ |
]*> | \[\[[^]]*\]\] )*

\s* (.*?)

+ }sx', $this->body, $bodyp, 0)) { return $bodyp[1]; } } @@ -170,6 +172,74 @@ class ArchiveArticle $this->image ); } + + function widget($name, $params = []) + { + $path = stream_resolve_include_path("widget/$name.php"); + if (!file_exists($path)) { + return ''.$name.' ontbreekt'; + } + + 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('%s', + "fout in $name: {$e->getMessage()}" + ); + } + } + + function render($blocks = []) + { + $doc = ob_get_clean(); + + if (!empty($blocks['warn'])) { + $warn = '

[[warn]]

'; + if ($offset = strpos($doc, '')) { + $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0); + } + else { + $doc = $warn . "\n\n" . $doc; + } + } + + # keep either login or logout parts depending on user level + global $User; + $hideclass = $User && property_exists($User, 'login') && $User->login ? 'logout' : 'login'; + $doc = preg_replace('{\s*<([a-z]+) class="'.$hideclass.'">.*?}s', '', $doc); + + return preg_replace_callback( + '{ \[\[ ([^] ]+) ([^]]*) \]\] }x', + function ($sub) use ($blocks) { + list ($placeholder, $name, $params) = $sub; + $html = $blocks[$name] ?? + $this->widget($name, explode(' ', $params)); + if (empty($html) or $html[0] != '<') { + $html = "$html"; + } + $attr = sprintf(' data-dyn="%s"', is_numeric($name) ? '' : $name.$params); + # contents with identifier in first tag + return preg_replace( '/(?=>)/', $attr, $html, 1); + }, + $doc + ); + } } class PageSearch