nieuws: port excelsior toc and year filtering
[minimedit.git] / nieuws.inc.php
1 <?php
2 $monthname = ['?',
3         'januari', 'februari', 'maart', 'april', 'mei', 'juni',
4         'juli', 'augustus', 'september', 'oktober', 'november', 'december',
5 ];
6
7 function shownewsdate($url)
8 {
9         $parts = pathinfo($url);
10         $year = pathinfo($parts['dirname'], PATHINFO_FILENAME);
11         @list ($month, $day) = explode('-', $parts['filename'], 3);
12         global $monthname;
13         return sprintf('<small class="date">%s %s %s</small>',
14                 intval($day), $monthname[intval($month)], $year);
15 }
16
17 function shownewsarticle($url, $link = TRUE, $title = NULL)
18 {
19         $html = ob_get_clean();
20         $date = shownewsdate($url);
21         ob_start();
22         print $title ?: '\1';
23         print '[[1]]';
24
25         $title = sprintf(
26                 $link ? '<h3><a href="/%2$s">%s</a></h3>' : '<h2>%s</h2>',
27                 getoutput([1 => ' <small class="date">'.$date.'</small>']),
28                 preg_replace('/\.html$/', '', $url)
29         );
30         return preg_replace('{<h2>(.*?)</h2>}', $title, $html);
31 }
32
33 function shownews($root, $limit = 5)
34 {
35         if (strpos($root, '/') === FALSE) $root .= '/*';
36         foreach (array_reverse(glob("$root/*.html")) as $url) {
37                 print "<article>";
38                 ob_start();
39                 include $url;
40                 print shownewsarticle($url);
41                 print "</article>\n\n";
42
43                 if (--$limit <= 0) break;
44         }
45 }
46
47 function printtoc($root)
48 {
49         print '<ul>';
50         foreach (array_reverse(glob("$root/*.html")) as $page) {
51                 $title = fgets(fopen($page, 'r'));
52                 $title = strip_tags($title);
53                 $linkurl = preg_replace('/\.html$/', '', $page);
54                 printf('<li><a href="/%s">%s <small class="date">%s</small></a></li>',
55                         $linkurl, $title, shownewsdate($linkurl));
56         }
57         print "</ul>\n";
58 }