-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsmartypants.php
More file actions
125 lines (111 loc) · 3.52 KB
/
smartypants.php
File metadata and controls
125 lines (111 loc) · 3.52 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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
<?php
namespace Grav\Plugin;
use Grav\Common\Data;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
class SmartypantsPlugin extends Plugin
{
/**
* @var SmartypantsPlugin
*/
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0],
'onBlueprintCreated' => ['onBlueprintCreated', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
if ($this->isAdmin() && !$this->config->get('plugins.smartypants.enabled_in_admin', false)) {
$this->active = false;
return;
}
require_once(__DIR__.'/vendor/Michelf/SmartyPants.php');
require_once(__DIR__.'/vendor/Michelf/SmartyPantsTypographer.php');
$this->enable([
'onTwigExtensions' => ['onTwigExtensions', 0],
'onPageProcessed' => ['onPageProcessed', 0],
'onPageContentProcessed' => ['onPageContentProcessed', 0]
]);
}
/**
* Add Twig Extensions
*/
public function onTwigExtensions()
{
if (!$this->config->get('plugins.smartypants.twig_filter')) {
return;
}
require_once(__DIR__.'/twig/SmartyPantsTwigExtension.php');
$this->grav['twig']->twig->addExtension(new SmartyPantsTwigExtension());
}
/**
* Apply smartypants to title
*/
public function onPageProcessed(Event $event)
{
$page = $event['page'];
$config = $this->mergeConfig($page);
if ($config->get('process_title')) {
$page->title($this->transform(
$page->title(),
$config
));
}
}
/**
* Apply smartypants to content
*/
public function onPageContentProcessed(Event $event)
{
$page = $event['page'];
$config = $this->mergeConfig($page);
if ($config->get('process_content')) {
$page->setRawContent($this->transform(
$page->getRawContent(),
$config
));
}
}
/**
* Extend page blueprints with feed configuration options.
*
* @param Event $event
*/
public function onBlueprintCreated(Event $event)
{
static $inEvent = false;
/** @var Data\Blueprint $blueprint */
$blueprint = $event['blueprint'];
if (!$inEvent && $blueprint->get('form.fields.tabs')) {
$inEvent = true;
$blueprints = new Data\Blueprints(__DIR__ . '/blueprints/');
$extends = $blueprints->get('smartypants');
$blueprint->extend($extends, true);
$inEvent = false;
}
}
/**
* Apply SmartyPants transformation on raw text.
*
* @param string $text Raw text
* @param object $config Configuration object
* @return string Transformed text
*/
protected function transform($text, $config)
{
$smartypants = new \Michelf\SmartyPantsTypographer($config->get('options'));
if ($config->get('dq_open')) $smartypants->smart_doublequote_open = $config->get('dq_open');
if ($config->get('dq_close')) $smartypants->smart_doublequote_close = $config->get('dq_close');
if ($config->get('sq_open')) $smartypants->smart_singlequote_open = $config->get('sq_open');
if ($config->get('sq_close')) $smartypants->smart_singlequote_close = $config->get('sq_close');
return $smartypants->transform($text);
}
}