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