nieuws: inline code to format article overviews
[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[2]), $parts[1] > 0 ? $monthname[intval($parts[1])] : '', $parts[0],
13                 count($parts) > 5 ? "$parts[3]:$parts[4]" : '',
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                 array_shift($ymd);
65                 return $ymd;
66         }
67
68         function dateiso()
69         {
70                 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
71         }
72
73         function date()
74         {
75                 return showdate($this->dateparts);
76         }
77
78         function body()
79         {
80                 $this->title;
81                 $rest = fread($this->file, filesize($this->page));
82                 if ( preg_match('{
83                         \n (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
84                 }x', $rest, $img, PREG_OFFSET_CAPTURE) ) {
85                         if (isset($img[1])) {
86                                 $this->img = $img[1][0];
87                         }
88                         return substr($rest, 0, $img[0][1]);
89                 }
90                 return $rest;
91         }
92
93         function teaser()
94         {
95                 if (preg_match('{<p>(.*?)</p>}s', $this->body, $bodyp)) {
96                         return $bodyp[1];
97                 }
98         }
99
100         function img()
101         {
102                 $this->img = NULL;
103                 $this->body;
104                 return $this->img;
105         }
106
107         function image()
108         {
109                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
110                         return $src[1];
111                 }
112         }
113
114         function thumb($size = '300x')
115         {
116                 if (!$this->image or $this->image[0] !== '/') return;
117                 return preg_replace(
118                         ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
119                         $this->image
120                 );
121         }
122 }