nieuws: dynamic article system
[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         foreach (array_reverse(glob("$root/*/*.html")) as $url) {
36                 print "<article>";
37                 ob_start();
38                 include $url;
39                 print shownewsarticle($url);
40                 print "</article>\n\n";
41
42                 if (--$limit <= 0) break;
43         }
44 }