nieuws: alternate cover image for .groot file name
[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 showdate($parts)
9 {
10         global $monthname;
11         return implode(' ', array_filter([
12                 intval(@$parts[3]), $parts[2] > 0 ? $monthname[intval($parts[2])] : '', $parts[1],
13                 count($parts) > 6 ? "$parts[4]:$parts[5]" : '',
14         ]));
15 }
16
17 class ArchiveArticle
18 {
19         function __construct($path)
20         {
21                 $this->page = $path;
22                 $this->link = preg_replace('{(?:/index)?\.html$}', '', $path);
23         }
24
25         function __get($col)
26         {
27                 return $this->$col = $this->$col();  # run method and cache
28         }
29
30         function file()
31         {
32                 if (!file_exists($this->page)) return;
33                 return fopen($this->page, 'r');
34         }
35
36         function title()
37         {
38                 return preg_replace('{<h2>(.*)</h2>\s*}', '\1', fgets($this->file));
39         }
40
41         function safetitle()
42         {
43                 return trim(strip_tags($this->title));
44         }
45
46         function name()
47         {
48                 return $this->safetitle ?: $this->link;
49         }
50
51         function last()
52         {
53                 return filemtime($this->page);
54         }
55
56         function lastiso()
57         {
58                 return date(DATE_ATOM, $this->last);
59         }
60
61         function dateparts()
62         {
63                 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->page, $ymd);
64                 return $ymd;
65         }
66
67         function dateiso()
68         {
69                 return implode('-', $this->dateparts());
70         }
71
72         function date()
73         {
74                 return showdate($this->dateparts);
75         }
76
77         function body()
78         {
79                 $this->title;
80                 $rest = fread($this->file, filesize($this->page));
81                 if ( preg_match('{
82                         \n (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
83                 }x', $rest, $img, PREG_OFFSET_CAPTURE) ) {
84                         if (isset($img[1])) {
85                                 $this->img = $img[1][0];
86                         }
87                         return substr($rest, 0, $img[0][1]);
88                 }
89                 return $rest;
90         }
91
92         function teaser()
93         {
94                 if (preg_match('{<p>(.*?)</p>}s', $this->body, $bodyp)) {
95                         return $bodyp[1];
96                 }
97         }
98
99         function img()
100         {
101                 $this->img = NULL;
102                 $this->body;
103                 return $this->img;
104         }
105
106         function image()
107         {
108                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
109                         return $src[1];
110                 }
111         }
112
113         function thumb($size = '300x')
114         {
115                 if (!$this->image or $this->image[0] !== '/') return;
116                 return preg_replace(
117                         ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
118                         $this->image
119                 );
120         }
121 }
122
123 function shownews($input, $limit = 1000)
124 {
125         if (!is_array($input)) $input = glob("$input/*.html");
126         foreach (array_reverse($input) as $filename) {
127                 $article = new ArchiveArticle($filename);
128                 print '<article class="left">';
129                 if ($article->thumb) {
130                         $imgattr = ' class="left"';
131                         if (preg_match('{ (\s alt="[^"]+") }x', $article->img, $img)) {
132                                 $imgattr .= $img[0];  # preserve alt value
133                         }
134                         printf('<img src="/%s"%s />', $article->thumb, $imgattr);
135                 }
136                 print '<div>';
137                 printf(
138                         '<h3><a href="/%s">%s <small class="date">%s</small></a></h3>',
139                         $article->link, $article->title, $article->date
140                 );
141                 print $article->body;
142                 print '</div>';
143                 print "</article>\n\n";
144
145                 if (--$limit <= 0) break;
146         }
147 }
148
149 function printtoc($input, $class = FALSE)
150 {
151         if (!is_array($input)) $input = glob("$input/*.html");
152         print '<ul';
153         if ($class) printf(' class="%s"', $class);
154         print '>';
155         foreach (array_reverse($input) as $page) {
156                 $article = new ArchiveArticle($page);
157                 $html = $article->safetitle;
158                 $dateparts = $article->dateparts;
159                 if ($class) {
160                         $dateparts[1] = NULL;  # omit year
161                 }
162                 $html .= sprintf(' <small class="date">%s</small>', showdate($dateparts));
163                 if ($class == 'gallery' and $article->img) {
164                         $html = "<div>$html</div>";
165                         $html = sprintf('<img src="%s" />', $article->thumb(200)) . $html;
166                 }
167                 $html = sprintf('<a href="/%s">%s</a>', $article->link, $html);
168                 print "<li><article>$html</article></li>\n";
169         }
170         print "</ul>\n";
171 }