| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
declare(strict_types=1);
|
|
|
4 |
|
|
|
5 |
/*
|
|
|
6 |
* This file is part of the league/commonmark package.
|
|
|
7 |
*
|
|
|
8 |
* (c) Colin O'Dell <colinodell@gmail.com> and uAfrica.com (http://uafrica.com)
|
|
|
9 |
*
|
|
|
10 |
* For the full copyright and license information, please view the LICENSE
|
|
|
11 |
* file that was distributed with this source code.
|
|
|
12 |
*/
|
|
|
13 |
|
|
|
14 |
namespace League\CommonMark\Extension\Strikethrough;
|
|
|
15 |
|
|
|
16 |
use League\CommonMark\Delimiter\DelimiterInterface;
|
|
|
17 |
use League\CommonMark\Delimiter\Processor\DelimiterProcessorInterface;
|
|
|
18 |
use League\CommonMark\Node\Inline\AbstractStringContainer;
|
|
|
19 |
|
|
|
20 |
final class StrikethroughDelimiterProcessor implements DelimiterProcessorInterface
|
|
|
21 |
{
|
|
|
22 |
public function getOpeningCharacter(): string
|
|
|
23 |
{
|
|
|
24 |
return '~';
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
public function getClosingCharacter(): string
|
|
|
28 |
{
|
|
|
29 |
return '~';
|
|
|
30 |
}
|
|
|
31 |
|
|
|
32 |
public function getMinLength(): int
|
|
|
33 |
{
|
|
|
34 |
return 2;
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
public function getDelimiterUse(DelimiterInterface $opener, DelimiterInterface $closer): int
|
|
|
38 |
{
|
|
|
39 |
$min = \min($opener->getLength(), $closer->getLength());
|
|
|
40 |
|
|
|
41 |
return $min >= 2 ? $min : 0;
|
|
|
42 |
}
|
|
|
43 |
|
|
|
44 |
public function process(AbstractStringContainer $opener, AbstractStringContainer $closer, int $delimiterUse): void
|
|
|
45 |
{
|
|
|
46 |
$strikethrough = new Strikethrough(\str_repeat('~', $delimiterUse));
|
|
|
47 |
|
|
|
48 |
$tmp = $opener->next();
|
|
|
49 |
while ($tmp !== null && $tmp !== $closer) {
|
|
|
50 |
$next = $tmp->next();
|
|
|
51 |
$strikethrough->appendChild($tmp);
|
|
|
52 |
$tmp = $next;
|
|
|
53 |
}
|
|
|
54 |
|
|
|
55 |
$opener->insertAfter($strikethrough);
|
|
|
56 |
}
|
|
|
57 |
}
|