4 public $raw, $title, $body; # file contents
5 public $meta = []; # head metadata properties
6 public $place = []; # template variables replaced in render()
8 function __construct($path)
10 $this->page = preg_replace('{^\.(?:/|$)}', '', $path);
11 $this->link = preg_replace('{(?:(?:/|^)index)?\.html$}', '', $this->page);
12 $this->raw($this->page);
17 if (!file_exists($page)) {
20 $this->raw = file_get_contents($page);
23 \G <meta \s+ property="( [^"]+ )" \s+ content="( [^"]* )" > \s*
24 }x', $this->raw, $meta)) {
25 $matchlen = array_sum(array_map('strlen', $meta[0]));
26 $this->raw = substr($this->raw, $matchlen); # delete matched contents
27 $this->meta = array_combine($meta[1], $meta[2]); # [property => content]
30 // find significant contents
31 $this->body = preg_replace('{<aside\b.*?</aside>}s', '', $this->raw);
32 if (preg_match('{<h2>(.*?)</h2>\s*(.*)}s', $this->body, $titlematch)) {
33 list (, $this->title, $this->body) = $titlematch;
41 return $this->$col = $this->$col(); # run method and cache
48 $this->restricted = FALSE;
50 if (file_exists("$path/.private")) {
51 $this->restricted = $path;
54 if (file_exists("$path/index.php")) {
58 $up = strrpos($path, '/');
59 $this->path = substr($path, $up) . $this->path;
60 $path = substr($path, 0, $up);
71 if (empty($this->handler)) {
76 $res = include "./{$this->handler}/index.php";
83 return $this->restricted;
88 return trim($this->meta['og:title'] ?? strip_tags($this->title));
92 return $this->safetitle ?: $this->link;
97 return filemtime($this->page);
101 return date(DATE_ATOM, $this->last);
106 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->page, $ymd);
112 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
118 (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
119 }x', $this->body, $img, PREG_OFFSET_CAPTURE) ) {
120 # strip part after matching divider (image)
121 if (isset($img[1])) {
122 $this->img = $img[1][0];
124 return substr($this->body, 0, $img[0][1]);
131 if ($override = @$this->meta['og:description']) {
132 # prefer specific page description if found in metadata
136 # paragraph contents following the page header if any
138 \G (?> \s+ | <div [^>]*> | \[\[[^]]*\]\] )* <p> \s* (.*?) </p>
139 }sx', $this->body, $bodyp, 0)) {
152 if ($override = @$this->meta['og:image']) {
153 # prefer specific page image if found in metadata
157 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
161 function thumb($size = '300x')
163 if (!$this->image or $this->image[0] !== '/') return;
164 if (preg_match('{^/thumb/\D}', $this->image)) {
165 return ltrim($this->image, '/');
168 ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
173 function widget($name, $params = [])
175 $path = stream_resolve_include_path("widget/$name.php");
176 if (!file_exists($path)) {
177 return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
182 if (is_array($params)) {
183 $Page->place += $params;
186 foreach (explode(' ', $params) as $param) {
187 if ($set = strpos($param, '=')) {
188 $Page->place[ substr($param, 0, $set) ] = substr($param, $set + 1);
190 elseif (!empty($param)) {
191 $Page->place[] = $param;
197 include "widget/$name.php";
198 return ob_get_clean();
200 catch (Exception $e) {
201 return sprintf('<strong class="warn">%s</strong>',
202 "fout in <em>$name</em>: {$e->getMessage()}"
211 if (!empty($this->place['warn'])) {
212 $warn = '<p class="warn">[[warn]]</p>';
213 if ($offset = strpos($doc, '</h2>')) {
214 $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
217 $doc = $warn . "\n\n" . $doc;
221 # keep either login or logout parts depending on user level
223 $hideclass = $User && property_exists($User, 'login') && $User->login ? 'logout' : 'login';
224 $doc = preg_replace('{\s*<([a-z]+) class="'.$hideclass.'">.*?</\1>}s', '', $doc);
226 return preg_replace_callback(
227 '{ \[\[ ([^] ]+) ([^]]*) \]\] }x',
229 list ($placeholder, $name, $params) = $sub;
230 $html = $this->place[$name] ??
231 $this->widget($name, $params);
232 if (empty($html) or $html[0] != '<') {
233 $html = "<span>$html</span>";
235 $attr = sprintf(' data-dyn="%s"', is_numeric($name) ? '' : $name.$params);
236 # contents with identifier in first tag
237 return preg_replace( '/(?=>)/', $attr, $html, 1);
246 public $handlers = [];
248 function __construct($path = '.')
250 $this->iterator = new RecursiveCallbackFilterIterator(
251 new RecursiveDirectoryIterator($path),
252 function ($current) {
253 if ($current->getFilename()[0] === '.') {
254 # skip hidden files and directories
257 if (file_exists($current->getFilename() . '/index.php')) {
258 # contents better provided by handler code
259 $this->handlers[ $current->getPathname() ] = $current;
262 if ($current->isLink()) {
263 # ignore symlinks, original contents only
266 if ($current->isDir()) {
267 # traverse subdirectories unless untracked in any amount
268 return !file_exists("$current/.gitignore");
271 return preg_match('/(?<!\.inc)\.html$/', $current->getFilename());
278 # order alphabetically by link
279 $dir = iterator_to_array(new RecursiveIteratorIterator($this->iterator));
280 array_walk($dir, function (&$row, $name) {
281 # prepare values for sorting (directory index first)
282 $row = preg_replace('{/index\.html$}', '', $name);