nieuws: placeholder script to show articles
[minimedit.git] / nieuws.inc.php
1 <?php
2 global $monthname;
3 $monthname = ['?',
4         'januari', 'februari', 'maart', 'april', 'mei', 'juni',
5         'juli', 'augustus', 'september', 'oktober', 'november', 'december',
6 ];
7
8 function shownewsdate($url)
9 {
10         if (!preg_match('</(\d{4})/(\d{2})/(\d{2})->', $url, $parts)) return;
11         global $monthname;
12         return implode(' ', array_filter([
13                 intval($parts[3]), $parts[2] > 0 ? $monthname[intval($parts[2])] : '', $parts[1],
14         ]));
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 = 1000)
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($input)
48 {
49         if (!is_array($input)) $input = glob("$input/*.html");
50         print '<ul>';
51         foreach (array_reverse($input) as $page) {
52                 $title = fgets(fopen($page, 'r'));
53                 $title = strip_tags($title);
54                 $linkurl = preg_replace('/\.html$/', '', $page);
55                 printf('<li><a href="/%s">%s <small class="date">%s</small></a></li>',
56                         $linkurl, $title, shownewsdate($linkurl));
57         }
58         print "</ul>\n";
59 }