1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the HTMLUP package. |
5
|
|
|
* |
6
|
|
|
* (c) Jitendra Adhikari <[email protected]> |
7
|
|
|
* <https://github.com/adhocore> |
8
|
|
|
* |
9
|
|
|
* Licensed under MIT license. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ahc; |
13
|
|
|
|
14
|
|
|
abstract class BlockElementParser |
15
|
|
|
{ |
16
|
|
|
use HtmlHelper; |
17
|
|
|
|
18
|
|
|
const RE_MD_QUOTE = '~^\s*(>+)\s+~'; |
19
|
|
|
const RE_RAW = '/^<\/?\w.*?\/?>/'; |
20
|
|
|
const RE_MD_SETEXT = '~^\s*(={3,}|-{3,})\s*$~'; |
21
|
|
|
const RE_MD_CODE = '/^```\s*([\w-]+)?/'; |
22
|
|
|
const RE_MD_RULE = '~^(_{3,}|\*{3,}|\-{3,})$~'; |
23
|
|
|
const RE_MD_TCOL = '~(\|\s*\:)?\s*\-{3,}\s*(\:\s*\|)?~'; |
24
|
|
|
const RE_MD_OL = '/^\d+\. /'; |
25
|
|
|
|
26
|
|
|
protected $lines = []; |
27
|
|
|
protected $stackList = []; |
28
|
|
|
protected $stackBlock = []; |
29
|
|
|
protected $stackTable = []; |
30
|
|
|
|
31
|
|
|
protected $pointer = -1; |
32
|
|
|
protected $listLevel = 0; |
33
|
|
|
protected $quoteLevel = 0; |
34
|
|
|
protected $indent = 0; |
35
|
|
|
protected $nextIndent = 0; |
36
|
|
|
protected $indentLen = 4; |
37
|
|
|
|
38
|
|
|
protected $indentStr = ' '; |
39
|
|
|
protected $line = ''; |
40
|
|
|
protected $trimmedLine = ''; |
41
|
|
|
protected $prevLine = ''; |
42
|
|
|
protected $trimmedPrevLine = ''; |
43
|
|
|
protected $nextLine = ''; |
44
|
|
|
protected $trimmedNextLine = ''; |
45
|
|
|
protected $markup = ''; |
46
|
|
|
|
47
|
|
|
protected $inList = \false; |
48
|
|
|
protected $inQuote = \false; |
49
|
|
|
protected $inPara = \false; |
50
|
|
|
protected $inHtml = \false; |
51
|
|
|
protected $inTable = \false; |
52
|
|
|
|
53
|
|
|
public function codeInternal($codeBlock) |
54
|
|
|
{ |
55
|
|
|
while (isset($this->lines[$this->pointer + 1])) { |
56
|
|
|
$this->line = $this->escape($this->lines[$this->pointer + 1]); |
57
|
|
|
|
58
|
|
|
if (($codeBlock && \substr(\ltrim($this->line), 0, 3) !== '```') |
59
|
|
|
|| \strpos($this->line, $this->indentStr) === 0 |
60
|
|
|
) { |
61
|
|
|
$this->markup .= "\n"; // @todo: donot use \n for first line |
62
|
|
|
$this->markup .= $codeBlock ? $this->line : \substr($this->line, $this->indentLen); |
63
|
|
|
|
64
|
|
|
$this->pointer++; |
65
|
|
|
} else { |
66
|
|
|
break; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
protected function listInternal() |
72
|
|
|
{ |
73
|
|
|
$isUl = \in_array(\substr($this->trimmedNextLine, 0, 2), ['- ', '* ', '+ ']); |
74
|
|
|
|
75
|
|
|
if ($isUl || \preg_match(static::RE_MD_OL, $this->trimmedNextLine)) { |
76
|
|
|
$wrapper = $isUl ? 'ul' : 'ol'; |
77
|
|
|
if ($this->nextIndent > $this->indent) { |
78
|
|
|
$this->stackList[] = "</li>\n"; |
79
|
|
|
$this->stackList[] = "</$wrapper>"; |
80
|
|
|
$this->markup .= "\n<$wrapper>\n"; |
81
|
|
|
|
82
|
|
|
$this->listLevel++; |
83
|
|
|
} else { |
84
|
|
|
$this->markup .= "</li>\n"; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
if ($this->nextIndent < $this->indent) { |
88
|
|
|
$shift = \intval(($this->indent - $this->nextIndent) / $this->indentLen); |
89
|
|
|
|
90
|
|
|
while ($shift--) { |
91
|
|
|
$this->markup .= \array_pop($this->stackList); |
92
|
|
|
|
93
|
|
|
if ($this->listLevel > 2) { |
94
|
|
|
$this->markup .= \array_pop($this->stackList); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} else { |
99
|
|
|
$this->markup .= "</li>\n"; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
protected function tableInternal($headerCount) |
104
|
|
|
{ |
105
|
|
|
$columnCount = \preg_match_all(static::RE_MD_TCOL, \trim($this->trimmedNextLine, '|')); |
106
|
|
|
|
107
|
|
|
if ($headerCount > 0 && $headerCount <= $columnCount) { |
108
|
|
|
$this->pointer++; |
109
|
|
|
|
110
|
|
|
$this->inTable = \true; |
111
|
|
|
$this->markup .= "<table>\n<thead>\n<tr>\n"; |
112
|
|
|
$this->trimmedLine = \trim($this->trimmedLine, '|'); |
113
|
|
|
|
114
|
|
|
foreach (\explode('|', $this->trimmedLine) as $hdr) { |
115
|
|
|
$this->markup .= '<th>' . \trim($hdr) . "</th>\n"; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
$this->markup .= "</tr>\n</thead>\n<tbody>\n"; |
119
|
|
|
|
120
|
|
|
return \true; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|