-
Notifications
You must be signed in to change notification settings - Fork 0
/
markdown-boxouts.php
112 lines (90 loc) · 3.4 KB
/
markdown-boxouts.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Grav\Plugin;
use \Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
class MarkdownBoxoutsPlugin extends Plugin
{
/**
* @return array
*/
public static function getSubscribedEvents()
{
return [
'onMarkdownInitialized' => ['onMarkdownInitialized', 0],
'onTwigSiteVariables' => ['onTwigSiteVariables', 0]
];
}
public function onMarkdownInitialized(Event $event)
{
$markdown = $event['markdown'];
$markdown->addBlockType('!', 'Boxout', true, false);
$markdown->blockBoxout = function($Line) {
$ConfigClasses = $this->config->get('plugins.markdown-boxouts.boxout_classes');
$Classes = array();
foreach($ConfigClasses as $Class) {
if (!isset($Class['classname'])) continue;
$Classes[$Class['classname']] = $Class;
}
if (empty($Classes)) return;
if (preg_match('/^(!)(\(([\w\-_]*)\))[ ]+(.*)/', $Line['text'], $matches))
{
$classname = $matches[3];
if (!isset($Classes[$classname])) return;
$BoxoutTitle = $Classes[$classname]['heading'];
$BoxoutIcon = $Classes[$classname]['fontawesome'];
$text = $matches[4];
$BodyElement = array(
'name' => 'div',
'attributes' => array(
'class' => 'boxout-body',
),
'handler' => 'lines',
'text' => array($text),
);
$HeaderElement = array(
'name' => 'div',
'allowRawHtmlInSafeMode' => true,
'attributes' => array(
'class' => 'boxout-header',
),
'rawHtml' => '<i class="fa fa-fw '.htmlspecialchars($BoxoutIcon).'"></i> '.$BoxoutTitle,
);
$BoxoutElements[] = $HeaderElement;
$BoxoutElements[] = $BodyElement;
$Block = array(
'element' => array(
'name' => 'div',
'attributes' => array(
'class' => 'boxout '.$classname,
),
'handler' => 'elements',
'text' => array($HeaderElement,$BodyElement),
),
);
return $Block;
};
};
$markdown->blockBoxoutContinue = function($Line, array $Block) {
if (isset($Block['interrupted']))
{
return;
}
if ($Line['text'][0] === '!' and preg_match('/^(!)(.*)/', $Line['text'], $matches))
{
$Block['element']['text'][1]['text'][]= ltrim($matches[2]);
return $Block;
}
};
$markdown->blockBoxoutComplete = function($Block)
{
return $Block;
};
}
public function onTwigSiteVariables()
{
if ($this->config->get('plugins.markdown-boxouts.built_in_css')) {
$this->grav['assets']
->add('plugin://markdown-boxouts/assets/boxouts.css');
}
}
}