widget/sitemap: order pages hierarchically and alphabetically
[minimedit.git] / widget / sitemap.php
1 <?php
2 # find **/*.html
3 $search = new RecursiveCallbackFilterIterator(
4         new RecursiveDirectoryIterator(ltrim($Args, '/') ?: '.'),
5         function ($current) {
6                 if ($current->getFilename()[0] === '.') {
7                         # skip hidden files and directories
8                         return FALSE;
9                 }
10                 if ($current->isLink()) {
11                         # ignore symlinks, original contents only
12                         return FALSE;
13                 }
14                 return $current->isDir()
15                         || preg_match('/\.html$/', $current->getFilename());
16         }
17 );
18
19 # order alphabetically by link
20 $dir = iterator_to_array(new RecursiveIteratorIterator($search));
21 array_walk($dir, function (&$row, $name) {
22         # prepare values for sorting (directory index first)
23         $row = preg_replace('{/index\.html$}', '', $name);
24 });
25 asort($dir);
26
27 # list article details
28 print '<ul class="replies">'."\n";
29 foreach ($dir as $filename => $sorted) {
30         $article = new ArchiveArticle($filename);
31         printf('<li><a href="%s">%s</a>', $article->link, $article->name);
32         if ($article->image) {
33                 printf("\n\t".'<img class="right" src="/%s" />', $article->thumb('100x100'));
34         }
35         if ($article->teaser) {
36                 printf("\n\t<blockquote>%s</blockquote>",
37                         preg_replace('/\n(.*)/s', ' <small>\1</small>', $article->teaser)
38                 );
39         }
40         print "</li>\n";
41 }
42 print "</ul>\n";