4 'januari', 'februari', 'maart', 'april', 'mei', 'juni',
5 'juli', 'augustus', 'september', 'oktober', 'november', 'december',
8 function showdate($parts)
11 return implode(' ', array_filter([
12 intval(@$parts[2]), $parts[1] > 0 ? $monthname[intval($parts[1])] : '', $parts[0],
13 count($parts) > 5 ? "$parts[3]:$parts[4]" : '',
19 public $raw, $title, $body; # file contents
20 public $meta = []; # head metadata properties
21 public $place = []; # template variables replaced in render()
23 function __construct($path)
25 $this->page = preg_replace('{^\.(?:/|$)}', '', $path);
26 $this->link = preg_replace('{(?:(?:/|^)index)?\.html$}', '', $this->page);
27 $this->raw($this->page);
32 if (!file_exists($page)) {
35 $this->raw = file_get_contents($page);
38 \G <meta \s+ property="( [^"]+ )" \s+ content="( [^"]* )" > \s*
39 }x', $this->raw, $meta)) {
40 $matchlen = array_sum(array_map('strlen', $meta[0]));
41 $this->raw = substr($this->raw, $matchlen); # delete matched contents
42 $this->meta = array_combine($meta[1], $meta[2]); # [property => content]
45 // find significant contents
46 $this->body = preg_replace('{<aside\b.*?</aside>}s', '', $this->raw);
47 if (preg_match('{<h2>(.*?)</h2>\s*(.*)}s', $this->body, $titlematch)) {
48 list (, $this->title, $this->body) = $titlematch;
56 return $this->$col = $this->$col(); # run method and cache
63 $this->restricted = FALSE;
65 if (file_exists("$path/.private")) {
66 $this->restricted = $path;
69 if (file_exists("$path/index.php")) {
73 $up = strrpos($path, '/');
74 $this->path = substr($path, $up) . $this->path;
75 $path = substr($path, 0, $up);
86 return $this->restricted;
91 return trim($this->meta['og:title'] ?? strip_tags($this->title));
95 return $this->safetitle ?: $this->link;
100 return filemtime($this->page);
104 return date(DATE_ATOM, $this->last);
109 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->page, $ymd);
115 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
119 return showdate($this->dateparts);
125 (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
126 }x', $this->body, $img, PREG_OFFSET_CAPTURE) ) {
127 # strip part after matching divider (image)
128 if (isset($img[1])) {
129 $this->img = $img[1][0];
131 return substr($this->body, 0, $img[0][1]);
138 if ($override = @$this->meta['og:description']) {
139 # prefer specific page description if found in metadata
143 # paragraph contents following the page header if any
145 \G (?> \s+ | <div [^>]*> | \[\[[^]]*\]\] )* <p> \s* (.*?) </p>
146 }sx', $this->body, $bodyp, 0)) {
159 if ($override = @$this->meta['og:image']) {
160 # prefer specific page image if found in metadata
164 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
168 function thumb($size = '300x')
170 if (!$this->image or $this->image[0] !== '/') return;
172 ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
177 function widget($name, $params = [])
179 $path = stream_resolve_include_path("widget/$name.php");
180 if (!file_exists($path)) {
181 return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
186 $Page->handler = $Page->handler . $Page->path; // .= with explicit getter
188 foreach ($params as $param) {
189 if ($set = strpos($param, '=')) {
190 $Page->place[ substr($param, 0, $set) ] = substr($param, $set + 1);
192 elseif (!empty($param)) {
193 $Page->path .= '/'.$param;
196 $Page->link .= $Page->path;
198 include "widget/$name.php";
199 return ob_get_clean();
201 catch (Exception $e) {
202 return sprintf('<strong class="warn">%s</strong>',
203 "fout in <em>$name</em>: {$e->getMessage()}"
212 if (!empty($this->place['warn'])) {
213 $warn = '<p class="warn">[[warn]]</p>';
214 if ($offset = strpos($doc, '</h2>')) {
215 $doc = substr_replace($doc, "\n\n".$warn, $offset + 5, 0);
218 $doc = $warn . "\n\n" . $doc;
222 # keep either login or logout parts depending on user level
224 $hideclass = $User && property_exists($User, 'login') && $User->login ? 'logout' : 'login';
225 $doc = preg_replace('{\s*<([a-z]+) class="'.$hideclass.'">.*?</\1>}s', '', $doc);
227 return preg_replace_callback(
228 '{ \[\[ ([^] ]+) ([^]]*) \]\] }x',
230 list ($placeholder, $name, $params) = $sub;
231 $html = $this->place[$name] ??
232 $this->widget($name, explode(' ', $params));
233 if (empty($html) or $html[0] != '<') {
234 $html = "<span>$html</span>";
236 $attr = sprintf(' data-dyn="%s"', is_numeric($name) ? '' : $name.$params);
237 # contents with identifier in first tag
238 return preg_replace( '/(?=>)/', $attr, $html, 1);
247 function __construct($path = '.')
249 $this->iterator = new RecursiveCallbackFilterIterator(
250 new RecursiveDirectoryIterator($path),
251 function ($current) {
252 if ($current->getFilename()[0] === '.') {
253 # skip hidden files and directories
256 if ($current->isLink()) {
257 # ignore symlinks, original contents only
260 if ($current->isDir()) {
261 # traverse subdirectories unless untracked in any amount
262 return !file_exists("$current/.gitignore");
265 return preg_match('/(?<!\.inc)\.html$/', $current->getFilename());
272 # order alphabetically by link
273 $dir = iterator_to_array(new RecursiveIteratorIterator($this->iterator));
274 array_walk($dir, function (&$row, $name) {
275 # prepare values for sorting (directory index first)
276 $row = preg_replace('{/index\.html$}', '', $name);