nieuws: retain image attributes in overview thumbs
[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('{\n<p>(<img [^>]*>)</p>\s*\z}', $rest, $img, PREG_OFFSET_CAPTURE) ) {
82                         $this->img = $img[1][0];
83                         return substr($rest, 0, $img[0][1]);
84                 }
85                 $this->img = NULL;
86                 return $rest;
87         }
88
89         function img()
90         {
91                 $this->body;
92                 return $this->img;
93         }
94
95         function image()
96         {
97                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
98                         return $src[1];
99                 }
100         }
101
102         function thumb($size = '300x')
103         {
104                 if (!$this->image or $this->image[0] !== '/') return;
105                 return preg_replace('{^(?:/thumb/[^/]*)?}', "thumb/$size", $this->image);
106         }
107 }
108
109 function shownews($input, $limit = 1000)
110 {
111         if (!is_array($input)) $input = glob("$input/*.html");
112         print '<ul class="left">'."\n\n";
113         foreach (array_reverse($input) as $filename) {
114                 $article = new ArchiveArticle($filename);
115                 print '<li>';
116                 if ($article->thumb) {
117                         $img = preg_replace('{(?<= \b src="/) [^"]* }x', $article->thumb, $article->img);
118                         $img = preg_replace('{(?= />$)}', ' class="left"', $img);
119                         print $img;
120                 }
121                 print '<article>';
122                 printf(
123                         '<h3><a href="/%s">%s <small class="date">%s</small></a></h3>',
124                         $article->link, $article->title, $article->date
125                 );
126                 print $article->body;
127                 print "</article></li>\n\n";
128
129                 if (--$limit <= 0) break;
130         }
131         print "</ul>\n\n";
132 }
133
134 function printtoc($input)
135 {
136         if (!is_array($input)) $input = glob("$input/*.html");
137         print '<ul>';
138         foreach (array_reverse($input) as $page) {
139                 $article = new ArchiveArticle($page);
140                 printf('<li><a href="/%s">%s <small class="date">%s</small></a></li>',
141                         $article->link, $article->safetitle, $article->date);
142         }
143         print "</ul>\n";
144 }