forked from getgrav/grav-plugin-markdown-notices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmarkdown-notices.php
92 lines (76 loc) · 2.58 KB
/
markdown-notices.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Grav\Plugin;
use Composer\Autoload\ClassLoader;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
class MarkdownNoticesPlugin extends Plugin
{
protected $base_classes;
protected $level_classes;
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onPluginsInitialized' => [
['autoload', 100001],
],
'onMarkdownInitialized' => ['onMarkdownInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
];
}
/**
* [onPluginsInitialized:100000] Composer autoload.
*
* @return ClassLoader
*/
public function autoload()
{
return require __DIR__ . '/vendor/autoload.php';
}
public function onMarkdownInitialized(Event $event)
{
$markdown = $event['markdown'];
$markdown->addBlockType('!', 'Notices', true, false);
$markdown->blockNotices = function($Line) {
$this->level_classes = $this->config->get('plugins.markdown-notices.level_classes');
$this->base_classes = $this->config->get('plugins.markdown-notices.base_classes');
if (preg_match('/^(!{1,'.count($this->level_classes).'}) (.*)/', $Line['text'], $matches))
{
$level = strlen($matches[1]) - 1;
$text = $matches[2];
$base_classes = (empty($this->base_classes)) ? '' : str_replace(',', ' ', $this->base_classes) . ' ';
$Block = [
'element' => [
'name' => 'div',
'handler' => 'lines',
'attributes' => [
'class' => $base_classes . $this->level_classes[$level],
],
'text' => (array) $text,
],
];
return $Block;
}
};
$markdown->blockNoticesContinue = function($Line, array $Block) {
if (isset($Block['interrupted']))
{
return;
}
if (preg_match('/^(!{1,'.count($this->level_classes).'}) ?(.*)/', $Line['text'], $matches))
{
$Block['element']['text'] []= $matches[2];
return $Block;
}
};
}
public function onTwigSiteVariables()
{
if ($this->config->get('plugins.markdown-notices.built_in_css')) {
$this->grav['assets']
->add('plugin://markdown-notices/assets/notices.css');
}
}
}