-
Notifications
You must be signed in to change notification settings - Fork 3
/
markdown-spoilers.php
151 lines (128 loc) · 4.41 KB
/
markdown-spoilers.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
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
<?php
namespace Grav\Plugin;
use Grav\Common\Plugin;
use RocketTheme\Toolbox\Event\Event;
/**
* Class MarkdownSpoilersPlugin
* @package Grav\Plugin
*/
class MarkdownSpoilersPlugin 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('>', 'Spoilers', true, false, 0);
$markdown->blockSpoilers = function($Line) {
// Matches spoiler syntax:
// >? [optional label] some spoiler message
if (preg_match('/^(>\?)(?:\s?\[([^\]]*)\]\s?)?(.*)/', $Line['text'], $matches))
{
$label = ltrim($matches[2]);
$text = ltrim($matches[3]);
$Element = [
'name' => 'p',
'handler' => 'line',
'attributes' => [
'class' => 'md-spoiler__line'
],
'text' => $text,
];
$Attributes = [
'class' => 'md-spoiler'
];
// ignore the label if empty
if (0 < strlen($label)) {
$Attributes['data-label'] = $label;
}
$Block = [
'element' => [
'name' => 'div',
'handler' => 'elements',
'attributes' => $Attributes,
'text' => [ $Element ],
]
];
return $Block;
}
};
$markdown->blockSpoilersContinue = function($Line, array $Block) {
if (isset($Block['interrupted']))
{
return;
}
// We don't support labels for continued lines
// Matches spoiler syntax:
// >? this is a spoiler
if ($Line['text'][0] === '>' and preg_match('/^(>\?)(.*)/', $Line['text'], $matches))
{
$text = ltrim($matches[2]);
$Element = [
'name' => 'p',
'handler' => 'line',
'attributes' => [
'class' => 'md-spoiler__line'
],
'text' => $text,
];
// we ignore empty content lines
if (0 < strlen($Element['text'])) {
$Block['element']['text'] []= $Element;
}
return $Block;
}
};
$markdown->addInlineType('?', 'Spoilers');
$markdown->inlineSpoilers = function($Excerpt) {
// Matches spoiler syntax:
// ??[optional label] some spoiler message??
if (preg_match('/^(\?{2})(?:\[([^\]]*)\]\s*)?(\S[^\?]*\S)(\?{2})/', $Excerpt['text'], $matches))
{
$extent = strlen($matches[0]);
$label = ltrim($matches[2]);
$text = ltrim($matches[3]);
$Attributes = [
'class' => 'md-spoiler md-spoiler--inline'
];
// ignore the label if empty
if (0 < strlen($label)) {
$Attributes['data-label'] = $label;
}
$Span = [
'name' => 'span',
'handler' => 'line',
'attributes' => [
'class' => 'md-spoiler__line'
],
'text' => $text,
];
$SpoilerTag = [
'extent' => $extent,
'element' => [
'name' => 'span',
'handler' => 'element',
'attributes' => $Attributes,
'text' => $Span,
]
];
return $SpoilerTag;
}
};
}
public function onTwigSiteVariables()
{
if ($this->config->get('plugins.markdown-spoilers.include_css')) {
$this->grav['assets']
->add('plugin://markdown-spoilers/assets/spoilers.css');
}
}
}