nieuws: first paragraph teaser as page description
[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>}', $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 teaser()
90         {
91                 if (preg_match('{<p>(.*?)</p>}s', $this->body, $bodyp)) {
92                         return $bodyp[1];
93                 }
94         }
95
96         function img()
97         {
98                 $this->body;
99                 return $this->img;
100         }
101
102         function image()
103         {
104                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
105                         return $src[1];
106                 }
107         }
108
109         function thumb($size = '300x')
110         {
111                 if (!$this->image or $this->image[0] !== '/') return;
112                 return preg_replace('{^(?:/thumb/[^/]*)?}', "thumb/$size", $this->image);
113         }
114 }
115
116 function shownews($input, $limit = 1000)
117 {
118         if (!is_array($input)) $input = glob("$input/*.html");
119         foreach (array_reverse($input) as $filename) {
120                 $article = new ArchiveArticle($filename);
121                 print '<article class="left">';
122                 if ($article->thumb) {
123                         $img = preg_replace('{(?<= \b src="/) [^"]* }x', $article->thumb, $article->img);
124                         $img = preg_replace('{(?= />$)}', ' class="left"', $img);
125                         print $img;
126                 }
127                 print '<div>';
128                 printf(
129                         '<h3><a href="/%s">%s <small class="date">%s</small></a></h3>',
130                         $article->link, $article->title, $article->date
131                 );
132                 print $article->body;
133                 print '</div>';
134                 print "</article>\n\n";
135
136                 if (--$limit <= 0) break;
137         }
138 }
139
140 function printtoc($input, $class = FALSE)
141 {
142         if (!is_array($input)) $input = glob("$input/*.html");
143         print '<ul';
144         if ($class) printf(' class="%s"', $class);
145         print '>';
146         foreach (array_reverse($input) as $page) {
147                 $article = new ArchiveArticle($page);
148                 $html = $article->safetitle;
149                 $dateparts = $article->dateparts;
150                 if ($class) {
151                         $dateparts[1] = NULL;  # omit year
152                 }
153                 $html .= sprintf(' <small class="date">%s</small>', showdate($dateparts));
154                 if ($class == 'gallery' and $article->img) {
155                         $html = "<div>$html</div>";
156                         $html = sprintf('<img src="%s" />', $article->thumb(200)) . $html;
157                 }
158                 $html = sprintf('<a href="/%s">%s</a>', $article->link, $html);
159                 print "<li><article>$html</article></li>\n";
160         }
161         print "</ul>\n";
162 }