-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtextformatter.php
More file actions
239 lines (209 loc) · 7.48 KB
/
textformatter.php
File metadata and controls
239 lines (209 loc) · 7.48 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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
<?php
/**
* TextFormatter v1.1.1-beta.1
*
* This plugin is a wrapper for TextFormatter, a library that supports
* BBCode, HTML and other markup via plugin. Handles emoticons, censors
* words, automatically embeds media and more.
*
* Dual licensed under the MIT or GPL Version 3 licenses, see LICENSE.
* http://benjamin-regler.de/license/
*
* @package TextFormatter
* @version 1.1.1-beta.1
* @link <https://github.com/sommerregen/grav-plugin-textformatter>
* @author Benjamin Regler <sommerregen@benjamin-regler.de>
* @copyright 2016, Benjamin Regler
* @license <http://opensource.org/licenses/MIT> MIT
* @license <http://opensource.org/licenses/GPL-3.0> GPLv3
*/
namespace Grav\Plugin;
use Grav\Common\Grav;
use Grav\Common\Utils;
use Grav\Common\Plugin;
use Grav\Common\Page\Page;
use Grav\Common\Data\Blueprints;
use RocketTheme\Toolbox\Event\Event;
/**
* TextFormatterPlugin
* @package Grav\Plugin\TextFormatter
*/
class TextFormatterPlugin extends Plugin
{
/**
* Instance of TextFormatter class
*
* @var \Grav\Plugin\TextFormatter
*/
protected $textformatter;
/**
* Return a list of subscribed events.
*
* @return array The list of events of the plugin of the form
* 'name' => ['method_name', priority].
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => ['onPluginsInitialized', 0]
];
}
/**
* Initialize configuration
*/
public function onPluginsInitialized()
{
$events = [
'onPageContentRaw' => ['onPageContentRaw', 0],
'onTwigInitialized' => ['onTwigInitialized', 0]
];
if ($this->isAdmin()) {
$this->active = false;
$events = [
'onBlueprintCreated' => ['onBlueprintCreated', 0]
];
}
$this->enable($events);
}
/**
* Extend page blueprints with textformatter configuration options.
*
* @param Event $event
*/
public function onBlueprintCreated(Event $event)
{
/** @var Blueprints $blueprint */
$blueprint = $event['blueprint'];
if ($blueprint->get('form/fields/tabs')) {
$blueprints = new Blueprints(__DIR__ . '/blueprints');
$extends = $blueprints->get($this->name);
$blueprint->extend($extends, true);
}
}
/**
* Add content after page content was read into the system.
*
* @param Event $event An event object, when `onPageContentRaw` is
* fired.
*/
public function onPageContentRaw(Event $event)
{
/** @var Page $page */
$page = $event['page'];
/** @var Cache $cache */
$cache = $this->grav['cache'];
$header = $page->header();
$config = $this->mergeConfig($page);
// Process contents with TextFormatter(?)
if (isset($header->process['textformatter'])) {
$process = (bool) $header->process['textformatter'];
} else {
$process = ($config->get('active') ? true : false);
if (isset($header->textformatter) && is_bool($header->textformatter)) {
$process = (bool) $header->textformatter;
}
}
// Process contents
if ($config->get('enabled')) {
$raw = $page->getRawContent();
// Build an anonymous function to pass to `parseLinks()`
$function = function ($matches) use (&$page, &$config) {
$content = $matches[1];
return $this->textFormatterFilter($content, $config->toArray(), $page);
};
// Only process whole page, if plugin is really active
if ($process) {
$raw = $function(['',
// Parse links (strip markup from content)
$this->parseLinks($raw, function($matches) {
return $matches[1];
})
]);
} else {
$raw = $this->parseLinks($raw, $function);
}
// Set the parsed content back into as raw content
$page->setRawContent($raw);
}
}
/**
* Initialize Twig configuration and filters.
*/
public function onTwigInitialized()
{
// Expose function
$this->grav['twig']->twig()->addFilter(
new \Twig_SimpleFilter('textformatter', [$this, 'textFormatterFilter'], ['is_safe' => ['html']])
);
}
/**
* Filter to parse content using TextFormatter class.
*
* @param string $content The content to be filtered.
* @param array $options Array of options for the textformatter filter.
*
* @return string The filtered content.
*/
public function textFormatterFilter($content, $params = [])
{
// Get custom user configuration
$page = func_num_args() > 2 ? func_get_arg(2) : $this->grav['page'];
$config = $this->mergeConfig($page, true, $params);
// Render
return $this->init()->render($content, $config, $page);
}
/**
* Get emoticons.
*
* @return array Return an associative list with available emotions.
*/
static public function getEmoticons()
{
/** @var UniformResourceLocator $locator */
$locator = Grav::instance()['locator'];
// Resolve path of default emoticons path
$path = Grav::instance()['config']->get('plugins.textformatter.emoticons.path');
// Check path configuration (backward-compatibility)
$path = $path ?: 'user://assets/emoticons';
$path = $locator->findResource($path, true);
if (!$path || !is_dir($path)) {
return [];
}
$emoticons = [];
$prefix = strlen($path) + 1;
$iterator = new \RecursiveIteratorIterator(
new \RecursiveDirectoryIterator($path, \RecursiveDirectoryIterator::SKIP_DOTS),
\RecursiveIteratorIterator::SELF_FIRST,
\RecursiveIteratorIterator::CATCH_GET_CHILD // Ignore "Permission denied"
);
foreach ($iterator as $object) {
if ($object->isFile()) {
$ext = pathinfo($object->getFilename(), PATHINFO_EXTENSION);
$mime = Utils::getMimeType($ext);
if (Utils::startsWith($mime, 'image/')) {
$marker = sprintf(':%s:', $object->getBasename($ext));
$emoticons[$marker] = substr($object->getPathname(), $prefix);
}
}
}
uksort($emoticons, 'strnatcasecmp');
return $emoticons;
}
/**
* Initialize plugin and all dependencies.
*
* @return \Grav\Plugin\TextFormattter Returns a TextFormattter instance.
*/
protected function init()
{
if (!$this->textformatter) {
// Use built-in autoloader
$this->grav['loader']->addPsr4('s9e\\TextFormatter\\',
[__DIR__ . '/vendor/s9e/text-formatter/src']);
// Initialize TextFormatter class
require_once(__DIR__ . '/classes/TextFormatter.php');
$this->textformatter = new TextFormatter();
}
return $this->textformatter;
}
}