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 return $this->restricted;
76 return trim($this->meta['og:title'] ?? strip_tags($this->title));
80 return $this->safetitle ?: $this->link;
85 return filemtime($this->page);
89 return date(DATE_ATOM, $this->last);
94 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->page, $ymd);
100 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
106 (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
107 }x', $this->body, $img, PREG_OFFSET_CAPTURE) ) {
108 # strip part after matching divider (image)
109 if (isset($img[1])) {
110 $this->img = $img[1][0];
112 return substr($this->body, 0, $img[0][1]);
119 if ($override = @$this->meta['og:description']) {
120 # prefer specific page description if found in metadata
124 # paragraph contents following the page header if any
126 \G (?> \s+ | <div [^>]*> | \[\[[^]]*\]\] )* <p> \s* (.*?) </p>
127 }sx', $this->body, $bodyp, 0)) {
140 if ($override = @$this->meta['og:image']) {
141 # prefer specific page image if found in metadata
145 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
149 function thumb($size = '300x')
151 if (!$this->image or $this->image[0] !== '/') return;
153 ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
158 function widget($name, $params = [])
160 $path = stream_resolve_include_path("widget/$name.php");
161 if (!file_exists($path)) {
162 return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
167 if (is_array($params)) {
168 $Page->place += $params;
171 foreach (explode(' ', $params) as $param) {
172 if ($set = strpos($param, '=')) {
173 $Page->place[ substr($param, 0, $set) ] = substr($param, $set + 1);
175 elseif (!empty($param)) {
176 $Page->place[] = $param;
182 include "widget/$name.php";
183 return ob_get_clean();
185 catch (Exception $e) {
186 return sprintf('<strong class="warn">%s</strong>',
187 "fout in <em>$name</em>: {$e->getMessage()}"
196 if (!empty($this->place['warn'])) {
197 $warn = '<p class="warn">[[warn]]</p>';
198 if ($offset = strpos($doc, '</h2>')) {
199 $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
202 $doc = $warn . "\n\n" . $doc;
206 # keep either login or logout parts depending on user level
208 $hideclass = $User && property_exists($User, 'login') && $User->login ? 'logout' : 'login';
209 $doc = preg_replace('{\s*<([a-z]+) class="'.$hideclass.'">.*?</\1>}s', '', $doc);
211 return preg_replace_callback(
212 '{ \[\[ ([^] ]+) ([^]]*) \]\] }x',
214 list ($placeholder, $name, $params) = $sub;
215 $html = $this->place[$name] ??
216 $this->widget($name, $params);
217 if (empty($html) or $html[0] != '<') {
218 $html = "<span>$html</span>";
220 $attr = sprintf(' data-dyn="%s"', is_numeric($name) ? '' : $name.$params);
221 # contents with identifier in first tag
222 return preg_replace( '/(?=>)/', $attr, $html, 1);
231 function __construct($path = '.')
233 $this->iterator = new RecursiveCallbackFilterIterator(
234 new RecursiveDirectoryIterator($path),
235 function ($current) {
236 if ($current->getFilename()[0] === '.') {
237 # skip hidden files and directories
240 if ($current->isLink()) {
241 # ignore symlinks, original contents only
244 if ($current->isDir()) {
245 # traverse subdirectories unless untracked in any amount
246 return !file_exists("$current/.gitignore");
249 return preg_match('/(?<!\.inc)\.html$/', $current->getFilename());
256 # order alphabetically by link
257 $dir = iterator_to_array(new RecursiveIteratorIterator($this->iterator));
258 array_walk($dir, function (&$row, $name) {
259 # prepare values for sorting (directory index first)
260 $row = preg_replace('{/index\.html$}', '', $name);