|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the league/commonmark package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Colin O'Dell <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* Original code based on the CommonMark JS reference parser (https://bitly.com/commonmark-js) |
|
9
|
|
|
* - (c) John MacFarlane |
|
10
|
|
|
* |
|
11
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
12
|
|
|
* file that was distributed with this source code. |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace League\CommonMark\Block\Element; |
|
16
|
|
|
|
|
17
|
|
|
use League\CommonMark\ContextInterface; |
|
18
|
|
|
use League\CommonMark\Cursor; |
|
19
|
|
|
use League\CommonMark\Util\RegexHelper; |
|
20
|
|
|
|
|
21
|
|
|
class HtmlBlock extends AbstractBlock |
|
22
|
|
|
{ |
|
23
|
|
|
const TYPE_1_CODE_CONTAINER = 1; |
|
24
|
|
|
const TYPE_2_COMMENT = 2; |
|
25
|
|
|
const TYPE_3 = 3; |
|
26
|
|
|
const TYPE_4 = 4; |
|
27
|
|
|
const TYPE_5_CDATA = 5; |
|
28
|
|
|
const TYPE_6_BLOCK_ELEMENT = 6; |
|
29
|
|
|
const TYPE_7_MISC_ELEMENT = 7; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @var int |
|
33
|
|
|
*/ |
|
34
|
|
|
protected $type; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param int $type |
|
38
|
|
|
*/ |
|
39
|
174 |
|
public function __construct($type) |
|
40
|
|
|
{ |
|
41
|
174 |
|
parent::__construct(); |
|
42
|
|
|
|
|
43
|
174 |
|
$this->type = $type; |
|
44
|
174 |
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return int |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getType() |
|
50
|
|
|
{ |
|
51
|
|
|
return $this->type; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param int $type |
|
56
|
|
|
*/ |
|
57
|
|
|
public function setType($type) |
|
58
|
|
|
{ |
|
59
|
|
|
$this->type = $type; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Returns true if this block can contain the given block as a child node |
|
64
|
|
|
* |
|
65
|
|
|
* @param AbstractBlock $block |
|
66
|
|
|
* |
|
67
|
|
|
* @return bool |
|
68
|
|
|
*/ |
|
69
|
|
|
public function canContain(AbstractBlock $block) |
|
70
|
|
|
{ |
|
71
|
|
|
return false; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns true if block type can accept lines of text |
|
76
|
|
|
* |
|
77
|
|
|
* @return bool |
|
78
|
|
|
*/ |
|
79
|
159 |
|
public function acceptsLines() |
|
80
|
|
|
{ |
|
81
|
159 |
|
return true; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* Whether this is a code block |
|
86
|
|
|
* |
|
87
|
|
|
* @return bool |
|
88
|
|
|
*/ |
|
89
|
111 |
|
public function isCode() |
|
90
|
|
|
{ |
|
91
|
111 |
|
return true; |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
129 |
|
public function matchesNextLine(Cursor $cursor) |
|
95
|
|
|
{ |
|
96
|
129 |
|
if ($cursor->isBlank() && ($this->type === self::TYPE_6_BLOCK_ELEMENT || $this->type === self::TYPE_7_MISC_ELEMENT)) { |
|
97
|
48 |
|
return false; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
111 |
|
return true; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
159 |
|
public function finalize(ContextInterface $context, $endLineNumber) |
|
104
|
|
|
{ |
|
105
|
159 |
|
parent::finalize($context, $endLineNumber); |
|
106
|
|
|
|
|
107
|
159 |
|
$this->finalStringContents = implode("\n", $this->getStrings()); |
|
108
|
159 |
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* @param ContextInterface $context |
|
112
|
|
|
* @param Cursor $cursor |
|
113
|
|
|
*/ |
|
114
|
159 |
|
public function handleRemainingContents(ContextInterface $context, Cursor $cursor) |
|
115
|
|
|
{ |
|
116
|
159 |
|
$context->getTip()->addLine($cursor->getRemainder()); |
|
117
|
|
|
|
|
118
|
|
|
// Check for end condition |
|
119
|
159 |
|
if ($this->type >= self::TYPE_1_CODE_CONTAINER && $this->type <= self::TYPE_5_CDATA) { |
|
120
|
60 |
|
if ($cursor->match(RegexHelper::getHtmlBlockCloseRegex($this->type)) !== null) { |
|
121
|
57 |
|
$this->finalize($context, $context->getLineNumber()); |
|
122
|
19 |
|
} |
|
123
|
20 |
|
} |
|
124
|
159 |
|
} |
|
125
|
|
|
} |
|
126
|
|
|
|