nieuws: support zero month for unknown dates
[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         if (!preg_match('</(\d{4})/(\d{2})/(\d{2})->', $url, $parts)) return;
10         global $monthname;
11         return implode(' ', array_filter([
12                 intval($parts[3]), $parts[2] > 0 ? $monthname[intval($parts[2])] : '', $parts[1],
13         ]));
14 }
15
16 function shownewsarticle($url, $link = TRUE, $title = NULL)
17 {
18         $html = ob_get_clean();
19         $date = shownewsdate($url);
20         ob_start();
21         print $title ?: '\1';
22         print '[[1]]';
23
24         $title = sprintf(
25                 $link ? '<h3><a href="/%2$s">%s</a></h3>' : '<h2>%s</h2>',
26                 getoutput([1 => ' <small class="date">'.$date.'</small>']),
27                 preg_replace('/\.html$/', '', $url)
28         );
29         return preg_replace('{<h2>(.*?)</h2>}', $title, $html);
30 }
31
32 function shownews($root, $limit = 5)
33 {
34         if (strpos($root, '/') === FALSE) $root .= '/*';
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 }
45
46 function printtoc($root)
47 {
48         print '<ul>';
49         foreach (array_reverse(glob("$root/*.html")) as $page) {
50                 $title = fgets(fopen($page, 'r'));
51                 $title = strip_tags($title);
52                 $linkurl = preg_replace('/\.html$/', '', $page);
53                 printf('<li><a href="/%s">%s <small class="date">%s</small></a></li>',
54                         $linkurl, $title, shownewsdate($linkurl));
55         }
56         print "</ul>\n";
57 }