-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.php
More file actions
91 lines (84 loc) · 2.96 KB
/
config.php
File metadata and controls
91 lines (84 loc) · 2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
/**
* @author PhileCMS
* @link https://github.com/PhileCMS/phileTwigFilters
* @license http://opensource.org/licenses/MIT
* @package Phile\Plugin\Phile\TwigFilters
*/
use Phile\Repository\PageCollection;
$config = [];
/**
* Excerpt: grab the first paragraph and remove all the html code
*/
$config['excerpt']['_callback'] = function (\Twig_Environment $environment, array $config) {
$filter = new \Twig_SimpleFilter('excerpt', function ($string) {
return strip_tags(substr($string, 0, strpos($string, '</p>') + 4));
});
$environment->addFilter($filter);
};
/**
* limit words function -- very rough limit due to HTML input
*
* If you want to remove the HTML markup when using the limit_words
* filter, you can use the striptags Twig filter:
*
* {{ page.content|striptags|limit_words }}
*/
$config['limit_words']['limit'] = 58;
$config['limit_words']['_callback'] = function (\Twig_Environment $environment, array $config) {
$filter = new \Twig_SimpleFilter('limit_words', function ($string) use ($config) {
$limit = $config['limit'];
$string = trim($string);
$words = str_word_count($string, 2);
$nbwords = count($words);
if ($limit < $nbwords) {
$pos = array_keys($words);
$string = substr($string, 0, $pos[$limit]) . '…';
}
return $string;
});
$environment->addFilter($filter);
};
/**
* Slugify filter
*
* {{ current_page.title | slugify }}
*/
$config['slugify']['delimiter'] = '–';
$config['slugify']['_callback'] = function (\Twig_Environment $environment, array $config) {
$filter = new \Twig_Filter('slugify', function ($string) use ($config) {
// https://github.com/phalcon/incubator/blob/master/Library/Phalcon/Utils/Slug.php
if (!extension_loaded('iconv')) {
throw new PluginException('iconv module not loaded', 0);
}
// Save the old locale and set the new locale to UTF-8
$oldLocale = setlocale(LC_ALL, '0');
setlocale(LC_ALL, 'en_US.UTF-8');
$clean = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
$clean = preg_replace("/[^a-zA-Z0-9\/_|+ -]/", '', $clean);
$clean = strtolower($clean);
$clean = preg_replace('/[\/_|+ -]+/', $config['delimiter'], $clean);
$clean = trim($clean, $config['delimiter']);
// Revert back to the old locale
setlocale(LC_ALL, $oldLocale);
return $clean;
});
$environment->addFilter($filter);
};
/**
* Shuffle pages
*/
$config['shuffle']['_callback'] = function (\Twig_Environment $environment, array $config) {
$filter = new \Twig_SimpleFilter('shuffle', function ($array) use ($config) {
if ($array instanceof PageCollection) {
$array = $array->toArray();
}
$keys = array_keys($array);
shuffle($keys);
$shuffled = array_combine($keys, $array);
ksort($shuffled);
return $shuffled;
});
$environment->addFilter($filter);
};
return $config;