page: teaser fallback to untitled paragraph
[minimedit.git] / article.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         public $raw, $preface, $title, $body;
20
21         function __construct($path)
22         {
23                 $this->page = $path;
24                 $this->link = preg_replace('{(?:/index)?\.html$}', '', $path);
25                 if (file_exists($this->page)) {
26                         $this->raw = file_get_contents($this->page);
27                         @list ($this->preface, $this->title, $this->body) =
28                                 preg_split('{<h2>(.*?)</h2>\s*}', $this->raw, 2, PREG_SPLIT_DELIM_CAPTURE);
29                 }
30         }
31
32         function __get($col)
33         {
34                 return $this->$col = $this->$col();  # run method and cache
35         }
36
37         function safetitle()
38         {
39                 return trim(strip_tags($this->title));
40         }
41         function name()
42         {
43                 return $this->safetitle ?: $this->link;
44         }
45
46         function last()
47         {
48                 return filemtime($this->page);
49         }
50         function lastiso()
51         {
52                 return date(DATE_ATOM, $this->last);
53         }
54
55         function dateparts()
56         {
57                 preg_match('< / (\d{4}) [/-] (\d{2}) (?:- (\d{2}) )? - >x', $this->page, $ymd);
58                 array_shift($ymd);
59                 return $ymd;
60         }
61         function dateiso()
62         {
63                 return implode('-', $this->dateparts()) . 'T12:00:00+02:00';
64         }
65         function date()
66         {
67                 return showdate($this->dateparts);
68         }
69
70         function story()
71         {
72                 if ( preg_match('{
73                         \n (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
74                 }x', $this->body, $img, PREG_OFFSET_CAPTURE) ) {
75                         # strip part after matching divider (image)
76                         if (isset($img[1])) {
77                                 $this->img = $img[1][0];
78                         }
79                         return substr($this->body, 0, $img[0][1]);
80                 }
81                 return $this->body;
82         }
83         function teaser()
84         {
85                 if (preg_match('{
86                         <meta \s+ name="description" [^>]* content="([^">]*)"
87                 }x', $this->preface, $meta)) {
88                         # prefer specific page description if found (assume before title)
89                         #TODO: strip from body contents
90                         return $meta[1];
91                 }
92
93                 if (preg_match('{
94                         </h2> (?: \s+ | <p\sclass="nav\b.*?</p> | <div[^>]*> )* <p> \s* (.*?) </p>
95                 }sx', $this->raw, $bodyp, PREG_OFFSET_CAPTURE)) {
96                         # fallback paragraph contents following the page header
97                         if ($bodyp[1][1] < 256) {
98                                 return $bodyp[1][0];
99                         }
100                 }
101
102                 # starting paragraph for documents without title (assumed simple/partial)
103                 if (strpos($this->raw, '<h2') === FALSE and preg_match('{
104                         \A <p> \s* (.*?) </p>
105                 }sx', $this->raw, $bodyp)) {
106                         return $bodyp[1];
107                 }
108         }
109
110         function img()
111         {
112                 $this->img = NULL;
113                 $this->story;
114                 return $this->img;
115         }
116         function image()
117         {
118                 if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
119                         return $src[1];
120                 }
121         }
122         function thumb($size = '300x')
123         {
124                 if (!$this->image or $this->image[0] !== '/') return;
125                 return preg_replace(
126                         ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
127                         $this->image
128                 );
129         }
130 }