-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathParsedownCheckbox.php
113 lines (94 loc) · 3.02 KB
/
ParsedownCheckbox.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
<?php
/**
* This file is part of the ParsedownCheckbox package.
*
* (c) Simon Leblanc <contact@leblanc-simon.eu>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
class ParsedownCheckbox extends ParsedownExtra
{
const VERSION = '0.2.0';
public function __construct()
{
parent::__construct();
array_unshift($this->BlockTypes['['], 'Checkbox');
}
protected function blockCheckbox($line)
{
$text = trim($line['text']);
$begin_line = substr($text, 0, 4);
if ('[ ] ' === $begin_line) {
return [
'handler' => 'checkboxUnchecked',
'text' => substr(trim($text), 4),
];
}
if ('[x] ' === $begin_line) {
return [
'handler' => 'checkboxChecked',
'text' => substr(trim($text), 4),
];
}
}
protected function blockListComplete(array $block)
{
foreach ($block['element']['elements'] as &$li_element) {
foreach ($li_element['handler']['argument'] as $text) {
$begin_line = substr(trim($text), 0, 4);
if ('[ ] ' === $begin_line) {
$li_element['attributes'] = ['class' => 'parsedown-task-list parsedown-task-list-open'];
} elseif ('[x] ' === $begin_line) {
$li_element['attributes'] = ['class' => 'parsedown-task-list parsedown-task-list-close'];
}
}
}
return $block;
}
protected function blockCheckboxContinue(array $block)
{
}
protected function blockCheckboxComplete(array $block)
{
$block['element'] = [
'rawHtml' => $this->{$block['handler']}($block['text']),
'allowRawHtmlInSafeMode' => true,
];
return $block;
}
protected function checkboxUnchecked($text)
{
if ($this->markupEscaped || $this->safeMode) {
$text = self::escape($text);
}
return '<input type="checkbox" disabled /> ' . $this->format($text);
}
protected function checkboxChecked($text)
{
if ($this->markupEscaped || $this->safeMode) {
$text = self::escape($text);
}
return '<input type="checkbox" checked disabled /> ' . $this->format($text);
}
/**
* Formats the checkbox label without double escaping.
* @param string $text the string to format
* @return string the formatted text
*/
protected function format($text)
{
// backup settings
$markup_escaped = $this->markupEscaped;
$safe_mode = $this->safeMode;
// disable rules to prevent double escaping.
$this->setMarkupEscaped(false);
$this->setSafeMode(false);
// format line
$text = $this->line($text);
// reset old values
$this->setMarkupEscaped($markup_escaped);
$this->setSafeMode($safe_mode);
return $text;
}
}