-
Notifications
You must be signed in to change notification settings - Fork 0
/
smartypants.php
72 lines (60 loc) · 2.2 KB
/
smartypants.php
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
<?php
use Herbie\DI;
use Herbie\Hook;
use michelf\Smartypants;
include_once (__DIR__ . '/vendor/Michelf/SmartyPants.php');
class SmartypantsPlugin
{
/** @var array */
protected static $config;
public static function install()
{
// default config
$defaults = DI::get('Config')->get('plugins.config.smartypants', []);
static::$config = array_merge([
'twig_filter' => 1,
'process_title' => 1,
'process_content' => true,
'options' => 'qDew'
], $defaults);
if (!empty(static::$config['twig_filter'])) {
Hook::attach('twigInitialized', ['SmartypantsPlugin', 'addTwigFilter']);
}
if (!empty(static::$config['process_title'])) {
Hook::attach('pageLoaded', ['SmartypantsPlugin', 'filterPageTitle']);
}
if (!empty(static::$config['process_content'])) {
Hook::attach('renderContent', ['SmartypantsPlugin', 'renderContent']);
}
}
public static function addTwigFilter($twig)
{
$filter = new Twig_SimpleFilter('smartypants', ['SmartypantsPlugin', 'smartypantsFilter'], ['is_safe' => ['html']]);
$twig->addFilter($filter);
}
public static function filterPageTitle($page)
{
// overwrite config with page config
if (!empty($page->smartypants) && is_array($page->smartypants)) {
static::$config = array_merge(static::$config, $page->smartypants);
}
// check again because page config
if (!empty(static::$config['process_title'])) {
$page->title = SmartyPants::defaultTransform($page->title, static::$config['options']);
}
}
public static function smartypantsFilter($content, $options = null)
{
$options = empty(static::$config['options']) ? : $options;
return SmartyPants::defaultTransform($content, $options);
}
public static function renderContent($content)
{
// check again because page config
if (!empty(static::$config['process_content'])) {
return SmartyPants::defaultTransform($content, static::$config['options']);
}
return $content;
}
}
SmartypantsPlugin::install();