1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Creates a excerpt for the contents of each page (as of Pico v0.9 and older) |
5
|
|
|
* |
6
|
|
|
* This plugin exists for backward compatibility and is disabled by default. |
7
|
|
|
* It gets automatically enabled when {@link PicoDeprecated} is enabled. You |
8
|
|
|
* can avoid this by calling {@link PicoExcerpt::setEnabled()}. |
9
|
|
|
* |
10
|
|
|
* This plugin doesn't do its job very well and depends on |
11
|
|
|
* {@link PicoParsePagesContent}, what heavily impacts Pico's performance. You |
12
|
|
|
* should either use the Description meta header field or write something own. |
13
|
|
|
* Best solution seems to be a filter for twig, see e.g. |
14
|
|
|
* {@link https://gist.github.com/james2doyle/6629712}. |
15
|
|
|
* |
16
|
|
|
* @author Daniel Rudolf |
17
|
|
|
* @link http://picocms.org |
18
|
|
|
* @license http://opensource.org/licenses/MIT |
19
|
|
|
* @version 1.0 |
20
|
|
|
*/ |
21
|
|
|
class PicoExcerpt extends AbstractPicoPlugin |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* This plugin is disabled by default |
25
|
|
|
* |
26
|
|
|
* @see AbstractPicoPlugin::$enabled |
27
|
|
|
*/ |
28
|
|
|
protected $enabled = false; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* This plugin depends on PicoParsePagesContent |
32
|
|
|
* |
33
|
|
|
* @see PicoParsePagesContent |
34
|
|
|
* @see AbstractPicoPlugin::$dependsOn |
35
|
|
|
*/ |
36
|
|
|
protected $dependsOn = array('PicoParsePagesContent'); |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Adds the default excerpt length of 50 words to the config |
40
|
|
|
* |
41
|
|
|
* @see DummyPlugin::onConfigLoaded() |
42
|
|
|
*/ |
43
|
|
|
public function onConfigLoaded(array &$config) |
44
|
|
|
{ |
45
|
|
|
if (!isset($config['excerpt_length'])) { |
46
|
|
|
$config['excerpt_length'] = 50; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Creates a excerpt for the contents of each page |
52
|
|
|
* |
53
|
|
|
* @see PicoExcerpt::createExcerpt() |
54
|
|
|
* @see DummyPlugin::onSinglePageLoaded() |
55
|
|
|
*/ |
56
|
|
|
public function onSinglePageLoaded(array &$pageData) |
57
|
|
|
{ |
58
|
|
|
if (!isset($pageData['excerpt'])) { |
59
|
|
|
$pageData['excerpt'] = $this->createExcerpt( |
60
|
|
|
strip_tags($pageData['content']), |
61
|
|
|
$this->getConfig('excerpt_length') |
62
|
|
|
); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Helper function to create a excerpt of a string |
68
|
|
|
* |
69
|
|
|
* @param string $string the string to create a excerpt from |
70
|
|
|
* @param int $wordLimit the maximum number of words the excerpt should be long |
71
|
|
|
* @return string excerpt of $string |
72
|
|
|
*/ |
73
|
|
|
protected function createExcerpt($string, $wordLimit) |
74
|
|
|
{ |
75
|
|
|
$words = explode(' ', $string); |
76
|
|
|
if (count($words) > $wordLimit) { |
77
|
|
|
return trim(implode(' ', array_slice($words, 0, $wordLimit))) . '…'; |
78
|
|
|
} |
79
|
|
|
return $string; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|